-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathLicense.cs
64 lines (54 loc) · 1.82 KB
/
License.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace DotNetLicensing
{
public class License
{
public bool VerifyXmlDocument(string publicKey, string licenseContent)
{
RSA key = RSA.Create();
key.FromXmlString(publicKey);
XmlDocument doc = new XmlDocument();
doc.LoadXml(licenseContent);
SignedXml sxml = new SignedXml(doc);
try
{
// Find signature node
XmlNode sig = doc.GetElementsByTagName("Signature")[0];
sxml.LoadXml((XmlElement)sig);
}
catch (Exception ex)
{
// Not signed!
return false;
}
return sxml.CheckSignature(key);
}
public XmlDocument SignXmlDocument(string licenseContent, string privateKey)
{
RSA key = RSA.Create();
key.FromXmlString(privateKey);
XmlDocument doc = new XmlDocument();
doc.LoadXml(licenseContent);
SignedXml sxml = new SignedXml(doc);
sxml.SigningKey = key;
sxml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigCanonicalizationUrl;
// Add reference to XML data
Reference r = new Reference("");
r.AddTransform(new XmlDsigEnvelopedSignatureTransform(false));
sxml.AddReference(r);
// Build signature
sxml.ComputeSignature();
// Attach signature to XML Document
XmlElement sig = sxml.GetXml();
doc.DocumentElement.AppendChild(sig);
return doc;
}
}
}