snippetcsharpMinor
Create/Verify signed license files
Viewed 0 times
createlicenseverifyfilessigned
Problem
The purpose of the class is to provide an easy way for create and verify a signed license file (or key). I am interested in any improvments / thoughts about the class.
Usage:
LicenseFile.cs
```
public class LicenseFile
{
public LicenseFile(string licensee, string company, IDictionary properties)
: this(licensee, company, DateTime.UtcNow.Date, properties, null)
{
}
private LicenseFile(string licensee, string company, DateTime issueDate, IDictionary properties, string signature)
{
if (string.IsNullOrWhiteSpace(licensee))
throw new ArgumentException("licensee");
IssueDate = issueDate;
Licensee = licensee;
Company = string.IsNullOrWhiteSpace(company) ? "Private License" : company;
Signature = signature;
var dict = properties ?? new Dictionary();
if (dict.Keys.Any(key => key.Contains(":")))
throw new FormatException("Character ':' is not allowed as property key.");
Properties = new ReadOnlyDictionary(dict);
}
private static byte[] ConfusingBytes = new byte[] { 34, 34, 2, 4, 54, 2, 100, 3 };
public DateTime IssueDate { get; private set; }
public string Licensee { get; private set; }
public string Company { get; private set; }
private string Signature {
Usage:
var signer = new LengthSigner();
// creation
var properties = new Dictionary();
properties.Add("Key1", "Value1");
properties.Add("Key2", "Value2");
var licenseFile = new LicenseFile("Me", "My Company", properties);
licenseFile.Sign(new LengthSigner());
var content = licenseFile.Serialize();
Console.WriteLine(content);
// actual content:
// ExcsNAEsVjMTFCI0BjhUMxgSMgk8TwEOKG97JHVtCXNDTHsJPEkBehMYVGVadwEyLyhJYU8wXlVDTndhBA9uNREvCA==
// unconfused content:
// 15.07.2016 00:00:00
// Me
// My Company
// Key1:Value1
// Key2:Value2
// 63
// verification
var deserializedLicenseFile = LicenseFile.Deserialize(content);
deserializedLicenseFile.Verify(new LengthSigner()); // trueLicenseFile.cs
```
public class LicenseFile
{
public LicenseFile(string licensee, string company, IDictionary properties)
: this(licensee, company, DateTime.UtcNow.Date, properties, null)
{
}
private LicenseFile(string licensee, string company, DateTime issueDate, IDictionary properties, string signature)
{
if (string.IsNullOrWhiteSpace(licensee))
throw new ArgumentException("licensee");
IssueDate = issueDate;
Licensee = licensee;
Company = string.IsNullOrWhiteSpace(company) ? "Private License" : company;
Signature = signature;
var dict = properties ?? new Dictionary();
if (dict.Keys.Any(key => key.Contains(":")))
throw new FormatException("Character ':' is not allowed as property key.");
Properties = new ReadOnlyDictionary(dict);
}
private static byte[] ConfusingBytes = new byte[] { 34, 34, 2, 4, 54, 2, 100, 3 };
public DateTime IssueDate { get; private set; }
public string Licensee { get; private set; }
public string Company { get; private set; }
private string Signature {
Solution
Just scratching on the surface - SOLID Principle
-
Interface Segregation : Why is your ISigner responsible for Signing and Verifying. From the name I would imagine it's responsibility is to Sign and not Verify.
-
Dependency Injection : In your second constructor, you create this
why not
-
Arrays are always declared with the first letter as Lowercase
should be
Note you might as well use DateTime.TryParse as opposed to Try..Catch
-
Interface Segregation : Why is your ISigner responsible for Signing and Verifying. From the name I would imagine it's responsibility is to Sign and not Verify.
-
Dependency Injection : In your second constructor, you create this
Properties = new ReadOnlyDictionary(dict);why not
Properties=properties-
Arrays are always declared with the first letter as Lowercase
private static byte[] ConfusingBytes = new byte[] { 34, 34, 2, 4, 54, 2, 100, 3 };should be
private static byte[] confusingBytes = new byte[] { 34, 34, 2, 4, 54, 2, 100, 3 };Note you might as well use DateTime.TryParse as opposed to Try..Catch
Context
StackExchange Code Review Q#134948, answer score: 2
Revisions (0)
No revisions yet.