项目作者: JanDotNet

项目描述 :
Simple library with fluent API for creating and verifying signed licenses
高级语言: C#
项目地址: git://github.com/JanDotNet/ThinkSharp.Licensing.git
创建时间: 2017-08-25T18:59:37Z
项目社区:https://github.com/JanDotNet/ThinkSharp.Licensing

开源协议:MIT License

下载


ThinkSharp.Licensing

Build status
NuGet License
Donate

Introduction

ThinkSharp.Licensing is a simple library with fluent API for creating and verifying signed licenses. It provides the following functionallities:

  • Creation / verification of hardware identifiers (Windows only)
  • Creation / verification of serial numbers
  • Creation / verification of signed licenses

Installation

ThinkSharp.Licensing can be installed via Nuget

  1. Install-Package ThinkSharp.Licensing

API Reference

Singed License

The class SignedLicense encapsulates some license related information and a signature for verifying it. The license can be serialized / deserialized for storing it on the client. It has following public properties:

  • IssueDate: date of the issuing (when the license was created)
  • ExpirationDate: date of the expiration (may be DateTime.MaxValue for licenses without expiration)
  • SerialNumber Optional: a serial number (See class SerialNumber below)
  • Properties IDictionary<string, string> with custom key value pairs

The static Lic class is the entry point for the fluent API that allows to work with signed licenses. It has the following static properties:

  • Lic.Builder: Object for creating signed license objects
  • Lic.Verifyer: Object for verifiying serialized signed licenses and deserialize it.
  • Lic.KeyGenerator: Object for creating private/public key pairs to use for signing

Usage

Create signed licenses

  1. SignedLicense license = Lic.Builder
  2. .WithRsaPrivateKey(pk) // .WithSigner(ISigner)
  3. .WithHardwareIdentifier(HardwareIdentifier.ForCurrentComputer()) // .WithoutHardwareIdentifier()
  4. .WithSerialNumber(SerialNumber.Create("ABC")) // .WithoutSerialNumber()
  5. .WithoutExpiration() // .ExpiresIn(TimeSpan), .ExpiresOn(DateTime)
  6. .WithProperty("Name", "Bill Gates")
  7. .WithProperty("Company", "Microsoft") //... other key value pairs
  8. .SignAndCreate();

Serialize License

The SignedLicense can be serialized as encrypted base64 encoded string (default):

  1. var encryptedText = license.Serialize();
  2. // FW9JbxVRYW8hcWBVZ3VHbGosEBxfYhlMZmshHHUxGBVbHBksc3
  3. // 9EHywLc2NNaWIsE39YaAxFbXo7BhhSYxwhZmBJYSAGGxkuEhUj
  4. // GREwFw08GxsxEBc8GywLER8jGBAuGRQ1EgEzExc5Ehs0GSAGZU
  5. // BsRRdOQk1tAGptX0RyLSdPRExxQUN1EWxoQ19jWE5nVCAGahJm
  6. // Ek8/GhFwSwY7ehk3Sm4+cRk4EFh4GVkydFh0U0NURAZUWBVnbW
  7. // 9eXQ5JTWtgElI4cHxaBFtEQ2ZBGlFiSmR5bWsuEHRfAENAYx8+
  8. // U09vQmM+Tg5SakFmcmxKFWM9YQ4yR2NVSVdidUwnE1BuS0BLeX
  9. // tbU0tifnNDQ25teVZjcXl2H2pQVnk7QEBTC19FXFRGeGs6T1FX
  10. // SUR0YmprFmknHRA5VBpOeUdYHQ==

or as plain text string:

  1. var plainText = license.SerializePlainText();
  2. // 5BED5GAB-E5TGXKGK-01SI8MFF-7T099W78-SRH4
  3. // SNABC-3RTC-DMW7-9SC1-MAHA
  4. // 08/28/2017 00:00:00
  5. // 12/31/9999 23:59:59
  6. // Name:Bill Gates
  7. // Company:Microsoft
  8. // A3g2b310qk+7Q86jC2Z890ut2x3TuxxbUd+Xs4fMBRv/HmFl9s
  9. // 9PQV/zEcKM1pcjIuFJ/0YS+bAC22xnnbN2e/SJljYMK5N1J/3g
  10. // NYbvcUa+8qokmGRZZsfnURBcCaRwbQTz4KQvT7kaR+rIwuGXF6
  11. // dpViixIKj6D+618t7BRfY=

Verify License

For deserializing the license, the Lic.Verifier has to be used. If the license can not be deserialized hor has no valid signature, an exception is thrown.

  1. SignedLicense license = Lic.Verifier
  2. .WithRsaPublicKey(publicKey) // .WithSigner(ISigner)
  3. .WithApplicationCode("ABC") // .WithoutApplicationCode
  4. .LoadAndVerify(licenseText);

Create public/private key Pair

A public and private key pair can be generated using the Lic.KeyGenerator object:

  1. SigningKeyPair pair = Lic.KeyGenerator.GenerateRsaKeyPair();
  2. Console.WriteLine(pair.PrivateKey);
  3. Console.WriteLine(pair.PublicKey);

Hardware Identifier

The hardware identifier is an identifier that derives from 4 characteristics of the computer’s hardware (processor ID, serial number of BIOS and so on). The identifier may look like:

  1. 5BED5GAB-E5TGXKGK-01SI8MFF-7T099W78-SRH4

Each characteristic is encoded in one of first 4 parts (8 charachters). The hardware identifier will be accepted if at least 2 of the 4 characteristics are equal. That ensures, that the license doesn’t become invalid if e.g. the processor of the computer changed. The last part (4 characters) is a check sum that can be used to detect errors in the the hardware identifier.

Usage

  1. // Create:
  2. string hardwareIdentifier = HardwareIdentifier.ForCurrentComputer();
  3. // Validate Checksum
  4. if (!HardwareIdentifier.IsCheckSumValid(hardwareIdentifier))
  5. {
  6. Console.WriteLine("Entered hardware identifier has errors.");
  7. }
  8. // Validate for current computer
  9. if (!HardwareIdentifier.IsValidForCurrentComputer(hardwareIdentifier))
  10. {
  11. Console.WriteLine("Entered license is not valid for this computer.");
  12. }

Serial Number

A serial number is an identifier with an alpha-numeric application code (3 character), some random characters and a check sum. It looks like SNXXX-YYYY-YYYY-YYYY-ZZZ where XXX is the application code, YYYY is the random part and ZZZ is the check sum. E.g.:

  1. SNABC-D156-KYJF-C4M5-1H96

Usage

  1. // ABC = application code
  2. string serialNumber = SerialNumber.Create("ABC");
  3. // Validate CheckSum
  4. if (!SerialNumber.IsCheckSumValid(serialNumber))
  5. {
  6. Console.WriteLine("Entered serial number is not valid.");
  7. }

License

ThinkSharp.Licensing is released under The MIT license (MIT)

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Credits

Thanks to Peter-B- for simplifying the project structure and improving compatibility to .Net 5.0.

Donation

If you like ThinkSharp.Licensing and use it in your project(s), feel free to give me a cup of coffee :)

paypal