Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4e5a3ee

Browse files
feat: add helper to get public key from certificate
fix: update namespaces for Enums
1 parent 301e15c commit 4e5a3ee

File tree

9 files changed

+87
-43
lines changed

9 files changed

+87
-43
lines changed

‎JwtManager/Helpers/Algorithm.cs‎ renamed to ‎JwtManager/Enums/Algorithm.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections.Generic;
33
using System.Text;
44

5-
namespace JwtManager.Helpers
5+
namespace JwtManager.Enums
66
{
77
public enum Algorithm
88
{

‎JwtManager/Helpers/KeySize.cs‎ renamed to ‎JwtManager/Enums/KeySize.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections.Generic;
33
using System.Text;
44

5-
namespace JwtManager.Helpers
5+
namespace JwtManager.Enums
66
{
77
public enum KeySize
88
{
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Security.Cryptography;
5+
using System.Security.Cryptography.X509Certificates;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace JwtManager.Helpers
10+
{
11+
public static class CertificateHelper
12+
{
13+
private static readonly string[] itemsToRemove = { "-----BEGIN CERTIFICATE-----", "-----END CERTIFICATE-----" };
14+
15+
public static string LoadFromFile(string path)
16+
{
17+
return RemoveHeaderFooterFromKey(System.IO.File.ReadAllText(path));
18+
}
19+
20+
public static string RemoveHeaderFooterFromKey(string key)
21+
{
22+
string tmp = key;
23+
foreach (string item in itemsToRemove)
24+
{
25+
tmp = tmp.Replace(item, string.Empty);
26+
}
27+
return tmp.Trim();
28+
}
29+
30+
public static string GetPublicKey(string certificate)
31+
{
32+
byte[] certificateKeyBytes = Convert.FromBase64String(certificate);
33+
X509Certificate2 cert = new X509Certificate2(certificateKeyBytes);
34+
RSA publicKey = cert.GetRSAPublicKey();
35+
36+
if(publicKey == null)
37+
{
38+
throw new Exception("Could not retrieve public key from certificate");
39+
}
40+
41+
return Encoding.Default.GetString(publicKey.ExportSubjectPublicKeyInfo());
42+
}
43+
}
44+
}

‎JwtManager/HsJwt.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace JwtManager
88
{
99
public class HsJwt : Jwt
1010
{
11-
public Helpers.KeySize KeySize { get; set; }
11+
public Enums.KeySize KeySize { get; set; }
1212
public string Secret { get; set; }
1313

1414
public override string Sign(string payload)
@@ -78,11 +78,11 @@ private HMAC GetHMAC(byte[] secret)
7878
{
7979
switch(KeySize)
8080
{
81-
case Helpers.KeySize.S256:
81+
case Enums.KeySize.S256:
8282
return new HMACSHA256(secret);
83-
case Helpers.KeySize.S384:
83+
case Enums.KeySize.S384:
8484
return new HMACSHA384(secret);
85-
case Helpers.KeySize.S512:
85+
case Enums.KeySize.S512:
8686
return new HMACSHA512(secret);
8787
default:
8888
throw new Exception("Non-valid key size.");

‎JwtManager/JwtHeader.cs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ public class JwtHeader
99
public string alg { get; set; }
1010
public string typ { get; set; }
1111

12-
public void Set(Helpers.Algorithm algorithm, Helpers.KeySize size)
12+
public void Set(Enums.Algorithm algorithm, Enums.KeySize size)
1313
{
14-
if(!Enum.IsDefined(typeof(Helpers.Algorithm), algorithm) || !Enum.IsDefined(typeof(Helpers.KeySize), size))
14+
if(!Enum.IsDefined(typeof(Enums.Algorithm), algorithm) || !Enum.IsDefined(typeof(Enums.KeySize), size))
1515
{
1616
throw new Exception("Invalid values for algorithm or size.");
1717
}
@@ -20,17 +20,17 @@ public void Set(Helpers.Algorithm algorithm, Helpers.KeySize size)
2020

2121
switch(algorithm)
2222
{
23-
case Helpers.Algorithm.RSA:
23+
case Enums.Algorithm.RSA:
2424
algstr = "RS";
2525
break;
26-
case Helpers.Algorithm.HMAC:
26+
case Enums.Algorithm.HMAC:
2727
algstr = "HS";
2828
break;
29-
case Helpers.Algorithm.ECDSA:
29+
case Enums.Algorithm.ECDSA:
3030
algstr = "ES";
3131
break;
32-
case Helpers.Algorithm.RSASSA:
33-
if(size == Helpers.KeySize.S512)
32+
case Enums.Algorithm.RSASSA:
33+
if(size == Enums.KeySize.S512)
3434
{
3535
throw new Exception("Invalid size for algorithm.");
3636
}

‎JwtManager/RsJwt.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class RsJwt : Jwt
1515
{
1616
public string PrivateKey { get; set; }
1717
public string PublicKey { get; set; }
18-
public Helpers.KeySize KeySize { get; set; }
18+
public Enums.KeySize KeySize { get; set; }
1919

2020
public override string Sign(string payload)
2121
{
@@ -93,7 +93,7 @@ private JwtHeader Header
9393
get
9494
{
9595
JwtHeader header = new JwtHeader();
96-
header.Set(Helpers.Algorithm.RSA, KeySize);
96+
header.Set(Enums.Algorithm.RSA, KeySize);
9797
return header;
9898
}
9999
}

‎JwtManagerTests/HsJwtTests.cs‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void SignDataWithInvalidHashAlgorithm()
107107
Exception e = null;
108108
JwtManager.HsJwt jwt = new JwtManager.HsJwt
109109
{
110-
KeySize = (JwtManager.Helpers.KeySize)555,
110+
KeySize = (JwtManager.Enums.KeySize)555,
111111
Secret = Secret
112112
};
113113

@@ -138,7 +138,7 @@ public void ValidateWithInvalidHashAlgorithm()
138138

139139
JwtManager.HsJwt vJwt = new JwtManager.HsJwt
140140
{
141-
KeySize = (JwtManager.Helpers.KeySize)555,
141+
KeySize = (JwtManager.Enums.KeySize)555,
142142
Secret = Secret
143143
};
144144

@@ -189,7 +189,7 @@ public void ValidateWithInvalidSecret()
189189
Assert.AreEqual(e.Message, "Invalid signature.");
190190
}
191191

192-
protected abstract JwtManager.Helpers.KeySize HashKeySize();
192+
protected abstract JwtManager.Enums.KeySize HashKeySize();
193193
}
194194

195195
[TestClass]
@@ -221,9 +221,9 @@ public static void ClassCleanup()
221221
}
222222
#endregion
223223

224-
protected override JwtManager.Helpers.KeySize HashKeySize()
224+
protected override JwtManager.Enums.KeySize HashKeySize()
225225
{
226-
return JwtManager.Helpers.KeySize.S256;
226+
return JwtManager.Enums.KeySize.S256;
227227
}
228228
}
229229

@@ -256,9 +256,9 @@ public static void ClassCleanup()
256256
}
257257
#endregion
258258

259-
protected override JwtManager.Helpers.KeySize HashKeySize()
259+
protected override JwtManager.Enums.KeySize HashKeySize()
260260
{
261-
return JwtManager.Helpers.KeySize.S384;
261+
return JwtManager.Enums.KeySize.S384;
262262
}
263263
}
264264

@@ -291,9 +291,9 @@ public static void ClassCleanup()
291291
}
292292
#endregion
293293

294-
protected override JwtManager.Helpers.KeySize HashKeySize()
294+
protected override JwtManager.Enums.KeySize HashKeySize()
295295
{
296-
return JwtManager.Helpers.KeySize.S512;
296+
return JwtManager.Enums.KeySize.S512;
297297
}
298298
}
299299

‎JwtManagerTests/JwtHeaderTests.cs‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public class JwtHeaderTests
1010
{
1111
class TestResult
1212
{
13-
public JwtManager.Helpers.Algorithm algorithm { get; set; }
14-
public JwtManager.Helpers.KeySize size { get; set; }
13+
public JwtManager.Enums.Algorithm algorithm { get; set; }
14+
public JwtManager.Enums.KeySize size { get; set; }
1515
public string alg { get; set; }
1616
public string typ { get; set; }
1717
}
@@ -22,12 +22,12 @@ public void ValidCases()
2222
JwtManager.JwtHeader header = new JwtManager.JwtHeader();
2323

2424
TestResult[] tests = {
25-
new TestResult { algorithm = JwtManager.Helpers.Algorithm.RSA, size = JwtManager.Helpers.KeySize.S256, alg = "RS256", typ = "JWT" },
26-
new TestResult { algorithm = JwtManager.Helpers.Algorithm.RSA, size = JwtManager.Helpers.KeySize.S384, alg = "RS384", typ = "JWT" },
27-
new TestResult { algorithm = JwtManager.Helpers.Algorithm.RSA, size = JwtManager.Helpers.KeySize.S512, alg = "RS512", typ = "JWT" },
28-
new TestResult { algorithm = JwtManager.Helpers.Algorithm.HMAC, size = JwtManager.Helpers.KeySize.S256, alg = "HS256", typ = "JWT" },
29-
new TestResult { algorithm = JwtManager.Helpers.Algorithm.HMAC, size = JwtManager.Helpers.KeySize.S384, alg = "HS384", typ = "JWT" },
30-
new TestResult { algorithm = JwtManager.Helpers.Algorithm.HMAC, size = JwtManager.Helpers.KeySize.S512, alg = "HS512", typ = "JWT" }
25+
new TestResult { algorithm = JwtManager.Enums.Algorithm.RSA, size = JwtManager.Enums.KeySize.S256, alg = "RS256", typ = "JWT" },
26+
new TestResult { algorithm = JwtManager.Enums.Algorithm.RSA, size = JwtManager.Enums.KeySize.S384, alg = "RS384", typ = "JWT" },
27+
new TestResult { algorithm = JwtManager.Enums.Algorithm.RSA, size = JwtManager.Enums.KeySize.S512, alg = "RS512", typ = "JWT" },
28+
new TestResult { algorithm = JwtManager.Enums.Algorithm.HMAC, size = JwtManager.Enums.KeySize.S256, alg = "HS256", typ = "JWT" },
29+
new TestResult { algorithm = JwtManager.Enums.Algorithm.HMAC, size = JwtManager.Enums.KeySize.S384, alg = "HS384", typ = "JWT" },
30+
new TestResult { algorithm = JwtManager.Enums.Algorithm.HMAC, size = JwtManager.Enums.KeySize.S512, alg = "HS512", typ = "JWT" }
3131
};
3232

3333
foreach(TestResult test in tests)
@@ -46,7 +46,7 @@ public void CheckEP512Exception()
4646

4747
try
4848
{
49-
header.Set(JwtManager.Helpers.Algorithm.RSASSA, JwtManager.Helpers.KeySize.S512);
49+
header.Set(JwtManager.Enums.Algorithm.RSASSA, JwtManager.Enums.KeySize.S512);
5050
}
5151
catch(Exception ex)
5252
{
@@ -65,7 +65,7 @@ public void CheckNonValidOptions()
6565

6666
try
6767
{
68-
header.Set(JwtManager.Helpers.Algorithm.RSASSA, (JwtManager.Helpers.KeySize)555);
68+
header.Set(JwtManager.Enums.Algorithm.RSASSA, (JwtManager.Enums.KeySize)555);
6969
}
7070
catch (Exception ex)
7171
{

‎JwtManagerTests/RsJwtTests.cs‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ public void SignDataWithInvalidHashAlgorithm()
244244
Exception e = null;
245245
JwtManager.RsJwt jwt = new JwtManager.RsJwt
246246
{
247-
KeySize = (JwtManager.Helpers.KeySize)555,
247+
KeySize = (JwtManager.Enums.KeySize)555,
248248
PrivateKey = PrivateKey
249249
};
250250

@@ -273,7 +273,7 @@ public void ValidateWithInvalidHashAlgorithm()
273273
};
274274
JwtManager.RsJwt vJwt = new JwtManager.RsJwt
275275
{
276-
KeySize = (JwtManager.Helpers.KeySize)555,
276+
KeySize = (JwtManager.Enums.KeySize)555,
277277
PublicKey = PublicKey
278278
};
279279

@@ -293,7 +293,7 @@ public void ValidateWithInvalidHashAlgorithm()
293293
Assert.AreEqual(e.Message, "Given key size is not valid.");
294294
}
295295

296-
protected abstract JwtManager.Helpers.KeySize HashKeySize();
296+
protected abstract JwtManager.Enums.KeySize HashKeySize();
297297
}
298298

299299
[TestClass]
@@ -325,9 +325,9 @@ public static void ClassCleanup()
325325
}
326326
#endregion
327327

328-
protected override JwtManager.Helpers.KeySize HashKeySize()
328+
protected override JwtManager.Enums.KeySize HashKeySize()
329329
{
330-
return JwtManager.Helpers.KeySize.S256;
330+
return JwtManager.Enums.KeySize.S256;
331331
}
332332
}
333333

@@ -360,9 +360,9 @@ public static void ClassCleanup()
360360
}
361361
#endregion
362362

363-
protected override JwtManager.Helpers.KeySize HashKeySize()
363+
protected override JwtManager.Enums.KeySize HashKeySize()
364364
{
365-
return JwtManager.Helpers.KeySize.S384;
365+
return JwtManager.Enums.KeySize.S384;
366366
}
367367
}
368368

@@ -395,9 +395,9 @@ public static void ClassCleanup()
395395
}
396396
#endregion
397397

398-
protected override JwtManager.Helpers.KeySize HashKeySize()
398+
protected override JwtManager.Enums.KeySize HashKeySize()
399399
{
400-
return JwtManager.Helpers.KeySize.S512;
400+
return JwtManager.Enums.KeySize.S512;
401401
}
402402
}
403403
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /