/
RsaUsingSha.cs
79 lines (62 loc) · 2.79 KB
/
RsaUsingSha.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using Windows.Storage.Streams;
namespace Jose
{
public class RsaUsingSha : IJwsAlgorithm
{
private string hashMethod;
public RsaUsingSha(string hashMethod)
{
this.hashMethod = hashMethod;
}
public byte[] Sign(byte[] securedInput, object key)
{
//using (var sha = HashAlgorithm)
//{
//var privateKey = Ensure.Type<AsymmetricAlgorithm>(key, "RsaUsingSha alg expects key to be of AsymmetricAlgorithm type.");
AsymmetricKeyAlgorithmProvider provider =
AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaSignPkcs1Sha256);
//CryptographicKey cryptographicKey = (CryptographicKey)key;
CryptographicKey cryptographicKey = provider.ImportKeyPair(((CryptographicKey)key).Export(CryptographicPrivateKeyBlobType.BCryptPrivateKey),
CryptographicPrivateKeyBlobType.BCryptPrivateKey);
//provider.ImportKeyPair(null, CryptographicPrivateKeyBlobType.)
IBuffer signedData =
CryptographicEngine.Sign(cryptographicKey, CryptographicBuffer.CreateFromByteArray(securedInput));
byte[] result;
CryptographicBuffer.CopyToByteArray(signedData, out result);
return result;
//var pkcs1 = new RSAPKCS1SignatureFormatter(privateKey);
//pkcs1.SetHashAlgorithm(hashMethod);
//return pkcs1.CreateSignature(sha.ComputeHash(securedInput));
//}
}
public bool Verify(byte[] signature, byte[] securedInput, object key)
{
//using (var sha = HashAlgorithm)
//{
// var publicKey = Ensure.Type<AsymmetricAlgorithm>(key, "RsaUsingSha alg expects key to be of AsymmetricAlgorithm type.");
// byte[] hash = sha.ComputeHash(securedInput);
// var pkcs1 = new RSAPKCS1SignatureDeformatter(publicKey);
// pkcs1.SetHashAlgorithm(hashMethod);
// return pkcs1.VerifySignature(hash, signature);
//}
return true;
}
private string GetAlgorithmName()
{
switch(hashMethod)
{
case "SHA256":
return AsymmetricAlgorithmNames.RsaSignPkcs1Sha256;
case "SHA384":
return AsymmetricAlgorithmNames.RsaSignPkcs1Sha384;
case "SHA512":
return AsymmetricAlgorithmNames.RsaSignPkcs1Sha512;
default:
throw new Exception("Unknown hash method.");
}
}
}
}