StringLicenseBuilder

Namespace: Babel.Licensing Assembly: Babel.Licensing.dll

This class is used to generate encoded license strings. This class cannot be inherited.

public sealed class StringLicenseBuilder : ILicenseBuilder, ICreateCustomRestriction, ICreateCustomSignatureProvider, IReadableLicenseString

Inheritance

objectStringLicenseBuilder

Implements

ILicenseBuilder, ICreateCustomRestriction, ICreateCustomSignatureProvider, IReadableLicenseString

Inherited Members

object.GetType(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

Extension Methods

Licenseable.AddLicense<StringLicenseBuilder>(StringLicenseBuilder, ILicense), Licenseable.Load(ILicenseBuilder, string), Licenseable.RemoveLicense<StringLicenseBuilder>(StringLicenseBuilder, ILicense), Licenseable.Save<StringLicenseBuilder>(StringLicenseBuilder, string), Licenseable.SignWith(ILicenseBuilder, ISignatureProvider), Licenseable.SignWithKeys(ILicenseBuilder, string, string), Licenseable.ToReadableString(ILicenseBuilder, string), Licenseable.ValidateSignature(ILicenseBuilder)

Constructors

StringLicenseBuilder()

Create a new instance of class.

public StringLicenseBuilder()

Examples

Dim builder As New StringLicenseBuilder()

' Create a signature provider to sign the license  
Dim signer As New ECDsaSignature()
signer.CreateKeyPair()

builder.SignatureProvider = signer

Dim license As New StringLicense()
license.Id = "1"
license.Type = "Standard"

builder.AddOrUpdate(license)
builder.Sign()

' License Key: ND5ZWSZHZADGQEQB7ICGGANQADLXDJQLFP6CH23FQEEA
Dim licenseKey As String = builder.ToReadableString()

StringLicenseBuilder(ISignatureProvider)

Create a new instance of class with the given signature provider.

public StringLicenseBuilder(ISignatureProvider signer)

Parameters

NameDescription

The license signature provider .

Exceptions

NameDescription

Thrown when one or more required arguments are null.

Properties

Format

Gets or sets the used to generate the license.

public StringFormat Format { get; set; }

Property Value

StringFormat

SignatureProvider

Gets or sets the signature provider. The signature provider is used to sign and verify the integrity of the license data.

public ISignatureProvider SignatureProvider { get; set; }

Property Value

ISignatureProvider

Examples

StringLicenseBuilder builder = new StringLicenseBuilder();
builder.SignatureProvider = RSASignature.CreateFromKeyFile("KeyPair.snk");

Methods

AddOrUpdate(ILicense)

Adds a with a given key to the if the license does not already exists, or updates the if the key already exists.

public void AddOrUpdate(ILicense license)

Parameters

NameDescription

license ILicense

The object.

Exceptions

NameDescription

Thrown when the license argument is null.

Load(Stream)

Loads the license from the specified file.

public void Load(Stream stream)

Parameters

NameDescription

stream Stream

Specify the input license stream.

Exceptions

NameDescription

Thrown when the fileName argument is null.

Thrown when the file contains bad data.

Load(TextReader)

Loads the license from the given text reader.

public void Load(TextReader reader)

Parameters

NameDescription

reader TextReader

The used to read the license.

Exceptions

NameDescription

Thrown when the reader argument is null.

Thrown when the reads bad data.

Parse(string)

Parse the input license string. If the string is not a valid encoded license string, it throws a .

public void Parse(string license)

Parameters

NameDescription

license string

The encoded license string to parse.

Remove(ILicense)

Removes a with a given key from the .

public bool Remove(ILicense license)

Parameters

NameDescription

license ILicense

The object to be removed.

Returns

NameDescription

true if the license is successfully removed, false if it fails to remove the license.

Exceptions

NameDescription

Thrown when the license argument is null.

Save(Stream)

Saves the current license state to the specified file.

public void Save(Stream stream)

Parameters

NameDescription

stream Stream

The output license stream.

Examples

This sample shows how to generate and save an encoded license string to a file.

StringLicense license = new StringLicense();

license.Id = IdGenerator.Create("lic", 5);
license.Product = new Product();
license.Product.Id = IdGenerator.Create("prd", 5);

// Create an encoded string license
StringLicenseBuilder builder = new StringLicenseBuilder();
builder.AddOrUpdate(license);

// Save the license to memory
MemoryStream stream = new MemoryStream();
builder.Save(stream);

Exceptions

NameDescription

Thrown when the fileName argument is null.

Save(TextWriter)

Saves the current license using the given .

public void Save(TextWriter writer)

Parameters

NameDescription

writer TextWriter

The object used to save the license.

Exceptions

NameDescription

Thrown when the writer argument is null.

SetMask(byte[])

Sets the mask used to generate the license.

public void SetMask(byte[] mask)

Parameters

NameDescription

mask byte[]

An array of bytes.

Exceptions

NameDescription

Thrown when one or more required arguments are null.

SetMaskSize(int)

Sets the mask size used to randomize the generated license.

public void SetMaskSize(int size)

Parameters

NameDescription

size int

The size represent the number of bytes used to randomize the license.

Examples

StringLicenseBuilder builder = new StringLicenseBuilder();

// Generates one of the possible 256 random licenses
string license = builder.ToString();

Exceptions

NameDescription

Thrown when the size argument is a negative number.

Sign()

Add a signature to the current license object.

public void Sign()

Exceptions

NameDescription

Thrown if there is the signature cannot be generated.

ToLicenses()

Converts this object to a object.

public IEnumerable<ILicense> ToLicenses()

Returns

NameDescription

This object as a .

ToReadableString(string)

Converts the current license state to a readable string.

public string ToReadableString(string format)

Parameters

NameDescription

format string

Describes the format to use.

Returns

NameDescription

The current license state as a string.

ToString(string)

Returns an encoded license string according to the format specified.

public string ToString(string format)

Parameters

NameDescription

format string

Describes the format to use. This can be ASCII or Base32.

Returns

NameDescription

A string that represents the current license in the given format.

Exceptions

NameDescription

Thrown if the format parameter is null.

Thrown when the format parameter specified is not valid.

ToString()

Returns an encoded string that represents the current license.

public override string ToString()

Returns

NameDescription

A string that represents the current license in the format specified for .

Validate()

Validates the current license signature. This method doesn't validate license restrictions. Is not meant to be used to validate the license in client applications. To validate the license in client applications use the class.

public bool Validate()

Returns

NameDescription

true if the signature id valid, false if the signature is not present or not valid.

CreateCustomRestriction

Event queue for all listeners interested in CreateCustomRestriction events.

public event EventHandler<CreateCustomRestrictionEventArgs> CreateCustomRestriction

Event Type

EventHandler<CreateCustomRestrictionEventArgs>

CreateCustomSignatureProvider

Event queue for all listeners interested in CreateCustomSignatureProvider events.

public event EventHandler<CreateCustomSignatureProviderEventArgs> CreateCustomSignatureProvider

Event Type

EventHandler<CreateCustomSignatureProviderEventArgs>

Examples

This example shows how to implement a custom MD5 signature provider.

class MD5Signature : ISignatureProvider
{
    public string AlgorithmName
    {
        get
        {
            return "MD5";
        }
    }

    #region ISignatureProvider
    public byte[] SignData(byte[] data)
    {
        using (MD5 md5Hash = MD5.Create())
        {
            return md5Hash.ComputeHash(data);
        }
    }

    public bool VerifyData(byte[] data, byte[] signature)
    {
        using (MD5 md5Hash = MD5.Create())
        {
            byte[] computed = md5Hash.ComputeHash(data);

            if (computed.Length != signature.Length)
                return false;

            for (int i = 0; i < computed.Length; i++)
            {
                if (computed[i] != signature[i])
                    return false;
            }

            return true;
        }
    }
    #endregion
}

The MD5 signature can be used to create and validate the license key.

MD5Signature md5 = new MD5Signature();
string licenseKey = new StringLicense().WithTrialDays(30)
                                       .SignWith(md5)
                                       .ToReadableString();

// licenseKey
// 74FMINW556KX2OHQP2KXEO7Z74H7YVIILUSNLNDWGZMTKNGX6G3PRQB

StringLicenseManager licenseManager = new StringLicenseManager();
licenseManager.CreateCustomSignatureProvider += (s, e) => {
    if (e.AlgorithmName == "MD5")
        e.SignatureProvider = md5;
};

StringLicense license = licenseManager.Validate(licenseKey);

Last updated