Rhino Licensing

De Banane Atomic
Révision datée du 10 décembre 2014 à 16:16 par Nicolas (discussion | contributions) (→‎Vérifier la licence)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)
Aller à la navigationAller à la recherche

Vérifier la licence

Csharp.svg
var publicKey = File.ReadAllText("publicKey.xml");

var licenseValidator = new LicenseValidator(publicKey, "license.xml");
// desactivate le Timer qui vérifie la licence
LicenseValidator.DisableFutureChecks();
// l'Action LicenseInvalidated doit être branchée 
// sinon une InvalidOperationException est lancée dans AbstractLicenseValidator.RaiseLicenseInvalidated()
LicenseValidator.LicenseInvalidated += LicenseValidator_LicenseInvalidated;

try
{
    // throw LicenceNotFoundException
    licenseValidator.AssertValidLicense();
}
catch (Exception ex)
{ }
finally
{
    LicenseValidator.LicenseInvalidated -= LicenseValidator_LicenseInvalidated;
}

private void LicenseValidator_LicenseInvalidated(InvalidationType obj)
{
    log.ErrorFormat("License was invalidated. {0}", 
        obj == InvalidationType.CannotGetNewLicense ? "Can not create a new license" : "License is expired");
}

Créer une licence

Csharp.svg
var id = Guid.NewGuid();
var name = "Mon Nom";
var expirationDate = new DateTime(2014, 12, 25);
var licenseType = LicenseType.Standard;

var attributes = new Dictionary<string, string>();
attributes.Add("Country", "France");

var privateKey = File.ReadAllText("privateKey.xml");
var generator = new LicenseGenerator(privateKey);
var license = generator.Generate(name, id, expirationDate, attributes, licenseType);

File.WriteAllText("license.xml", license);

Créer les clés privée et publique

Csharp.svg
var rsa = new RSACryptoServiceProvider(1024);

File.WriteAllText("publicKey.xml", rsa.ToXmlString(false));
File.WriteAllText("privateKey.xml", rsa.ToXmlString(true));

Fonctionnement

Rhino Licensing utilise un chiffrement asymétrique.
La licence est au format XML, elle contient:

  • le nom de l'utilisateur
  • la date d'expiration
  • le type de licence
  • une signature cryptographique: un hashage (SHA1) chiffré avec la clé privé du contenu du fichier, ceci afin d'éviter toutes modifications
  • autant d'autres information que nécessaire


La vérification de la licence se fait par:

  1. déchiffrement de la signature avec la clé publique
  2. création d'un hashage de la licence
  3. comparaison du hashage obtenu avec la signature déchiffrée