-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
175 lines (142 loc) · 5.65 KB
/
Program.cs
File metadata and controls
175 lines (142 loc) · 5.65 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace JereckNET.LicenseManager.Sample {
class Program {
public static void Main() {
#if NETFRAMEWORK
Console.WriteLine("Running as .NET Framework");
#elif NETCOREAPP
Console.WriteLine("Running as .NET Core");
#endif
Console.WriteLine();
Console.WriteLine("**** Use case 1 ****");
sample1();
Console.WriteLine();
Console.WriteLine("**** Use case 2 ****");
sample2();
Console.WriteLine();
Console.WriteLine("**** Use case 3 ****");
sample3();
Console.WriteLine();
Console.WriteLine("**** Use case 4 ****");
sample4();
Console.WriteLine();
}
/// <summary>
/// Use case 1 :
/// XML data
/// Signed with XML Public Key
/// Valid Signature
/// Saved as Base64
/// Read as byte[]
/// </summary>
private static void sample1() {
bool status;
License license;
LicenseData data;
Console.WriteLine("Loading \"License1.lic\" : ");
license = License.Load(@"Licenses\License1.lic");
status = license.Verify(Properties.Resources.LicensePublicKey);
Console.WriteLine("Signature validity : " + status);
if (status) {
using (MemoryStream ms = new MemoryStream(license.Content)) {
using (XmlReader xr = XmlReader.Create(ms)) {
data = (LicenseData)new XmlSerializer(typeof(LicenseData)).Deserialize(xr);
}
}
Console.WriteLine("Product licensed to :");
Console.WriteLine(data.ToString());
}
}
/// <summary>
/// Use case 2 :
/// XML data
/// Signed with XML Public Key
/// Valid Signature
/// Saved as Base64
/// Read from <see cref="License.GetContentFromXML{T}"/>
/// </summary>
private static void sample2() {
bool status;
License license;
LicenseData data;
Console.WriteLine("Loading \"License1.lic\" : ");
license = License.Load(@"Licenses\License1.lic");
status = license.Verify(Properties.Resources.LicensePublicKey);
Console.WriteLine("Signature validity : " + status);
if (status) {
data = license.GetContentFromXML<LicenseData>();
Console.WriteLine("Product licensed to :");
Console.WriteLine(data.ToString());
}
}
/// <summary>
/// Use case 3 :
/// JSON data
/// Signed with PFX Private Key
/// Binary-encoded Public Key
/// Valid Signature
/// Saved as Base64
/// Read as string
/// </summary>
private static void sample3() {
bool status;
License license;
Console.WriteLine("Loading \"License2.lic\" : ");
license = License.Load(@"Licenses\License2.lic");
using (X509Certificate2 certificate = new X509Certificate2(Properties.Resources.LicensePublicCertificateCRT)) {
status = license.Verify(certificate);
}
Console.WriteLine("Signature validity : " + status);
if (status) {
string s = Encoding.UTF8.GetString(license.Content);
Console.WriteLine("Product licensed to :");
Console.WriteLine(s.ToString());
}
}
/// <summary>
/// Use case 4 :
/// JSON data
/// Signed with PFX Private Key
/// Base64-encoded Public Key
/// Valid Signature
/// Saved as Base64
/// Read as string
/// </summary>
private static void sample4() {
bool status;
License license;
Console.WriteLine("Loading \"License2.lic\" : ");
license = License.Load(@"Licenses\License2.lic");
byte[] CERData = fromBase64Certificate(Properties.Resources.LicensePublicCertificateCER);
using (X509Certificate2 certificate = new X509Certificate2(CERData)) {
status = license.Verify(certificate);
}
Console.WriteLine("Signature validity : " + status);
if (status) {
string s = Encoding.UTF8.GetString(license.Content);
Console.WriteLine("Product licensed to :");
Console.WriteLine(s.ToString());
}
}
private static byte[] fromBase64Certificate(string content) {
StringBuilder base64Content = new StringBuilder();
using (StringReader sr = new StringReader(content)) {
string currentLine = sr.ReadLine();
if (currentLine.StartsWith("-")) // Skip "-----BEGIN CERTIFICATE-----" line
currentLine = sr.ReadLine();
while (currentLine != null && !currentLine.StartsWith("-")) { // Skip "-----END CERTIFICATE-----" line
base64Content.Append(currentLine);
currentLine = sr.ReadLine();
}
string base64License = base64Content.ToString();
byte[] data = Convert.FromBase64String(base64License);
return data;
}
}
}
}