ThinkSharp.Licensing 1.1.0

dotnet add package ThinkSharp.Licensing --version 1.1.0
NuGet\Install-Package ThinkSharp.Licensing -Version 1.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ThinkSharp.Licensing" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ThinkSharp.Licensing --version 1.1.0
#r "nuget: ThinkSharp.Licensing, 1.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ThinkSharp.Licensing as a Cake Addin
#addin nuget:?package=ThinkSharp.Licensing&version=1.1.0

// Install ThinkSharp.Licensing as a Cake Tool
#tool nuget:?package=ThinkSharp.Licensing&version=1.1.0

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

  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

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

Serialize License

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

var encryptedText = license.Serialize();

// FW9JbxVRYW8hcWBVZ3VHbGosEBxfYhlMZmshHHUxGBVbHBksc3
// 9EHywLc2NNaWIsE39YaAxFbXo7BhhSYxwhZmBJYSAGGxkuEhUj
// GREwFw08GxsxEBc8GywLER8jGBAuGRQ1EgEzExc5Ehs0GSAGZU
// BsRRdOQk1tAGptX0RyLSdPRExxQUN1EWxoQ19jWE5nVCAGahJm
// Ek8/GhFwSwY7ehk3Sm4+cRk4EFh4GVkydFh0U0NURAZUWBVnbW
// 9eXQ5JTWtgElI4cHxaBFtEQ2ZBGlFiSmR5bWsuEHRfAENAYx8+
// U09vQmM+Tg5SakFmcmxKFWM9YQ4yR2NVSVdidUwnE1BuS0BLeX
// tbU0tifnNDQ25teVZjcXl2H2pQVnk7QEBTC19FXFRGeGs6T1FX
// SUR0YmprFmknHRA5VBpOeUdYHQ==

or as plain text string:

var plainText = license.SerializePlainText();

// 5BED5GAB-E5TGXKGK-01SI8MFF-7T099W78-SRH4
// SNABC-3RTC-DMW7-9SC1-MAHA
// 08/28/2017 00:00:00
// 12/31/9999 23:59:59
// Name:Bill Gates
// Company:Microsoft
// A3g2b310qk+7Q86jC2Z890ut2x3TuxxbUd+Xs4fMBRv/HmFl9s
// 9PQV/zEcKM1pcjIuFJ/0YS+bAC22xnnbN2e/SJljYMK5N1J/3g
// NYbvcUa+8qokmGRZZsfnURBcCaRwbQTz4KQvT7kaR+rIwuGXF6
// 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.

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

Create public/private key Pair

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

SigningKeyPair pair = Lic.KeyGenerator.GenerateRsaKeyPair();
Console.WriteLine(pair.PrivateKey);
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:

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
// Create:
string hardwareIdentifier = HardwareIdentifier.ForCurrentComputer();

// Validate Checksum
if (!HardwareIdentifier.IsCheckSumValid(hardwareIdentifier))
{
    Console.WriteLine("Entered hardware identifier has errors.");
}
        
// Validate for current computer
if (!HardwareIdentifier.IsValidForCurrentComputer(hardwareIdentifier))
{
    Console.WriteLine("Entered license is not valid for this computer.");
}

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.:

SNABC-D156-KYJF-C4M5-1H96    
Usage
// ABC = application code
string serialNumber = SerialNumber.Create("ABC");
    
// Validate CheckSum
if (!SerialNumber.IsCheckSumValid(serialNumber))
{
    Console.WriteLine("Entered serial number is not valid.");
}

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

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ThinkSharp.Licensing:

Package Downloads
MdlIsoReaderDotNetSDK

The SDK allows an app to perform mDL read and verification according to ISO 18013-5 standard.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 95 4/14/2024
1.0.0 20,001 5/16/2021
0.2.1 3,289 9/26/2017
0.2.0 964 9/3/2017
0.1.1 949 8/29/2017
0.1.0 931 8/28/2017

1.1.0
* fixed issue with cross-plattform licence verification (#10)
* HasExpirationDate does not work as expected (#12)
* SignedLicense does not accept custom issue dates (#13)
* some non-functional improvements / refactorings
1.0,0
* Improved project structure (some non-compatible changes in namespaces)
* Added support for .net 5.0
0.2.1
* Added properties: SignedLicense.HasSerialNumber, SignedLicense.HasHardwareIdentifier, SignedLicense.HasExpirationDate