forked from sjh37/Effortless-.Net-Encryption
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DigitalSignature.cs
161 lines (145 loc) · 6.64 KB
/
DigitalSignature.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
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
/* The MIT License (MIT)
Copyright © Simon J Hughes 2012.
* Homepage: http://www.hicrest.net/
* Blog: http://simon-hughes.blogspot.co.uk/
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System.Security.Cryptography;
namespace Effortless.Net.Encryption
{
/// <summary>
/// Digital sign and verify a hash using RSACryptoServiceProvider
/// </summary>
public class DigitalSignature
{
private RSAParameters _publicKey;
private RSAParameters _privateKey;
private readonly int _keySize;
private readonly string _hashAlgorithm;
/// <summary>
/// Uses a key length of 2048
/// Uses a hash algorithm of SHA256
/// </summary>
public DigitalSignature()
{
_keySize = 2048;
_hashAlgorithm = "SHA256";
}
/// <summary>
/// Create a key of a custom length.
/// Uses a custom hash algorithm
/// </summary>
/// <param name="keySize">The RSACryptoServiceProvider supports key sizes from 384 bits to 16384 bits in increments of 8 bits
/// if you have the Microsoft Enhanced Cryptographic Provider installed. It supports key sizes from 384 bits to 512 bits in
/// increments of 8 bits if you have the Microsoft Base Cryptographic Provider installed. </param>
/// <param name="hashAlgorithm">Specify the hash algorithm for RSAPKCS1SignatureFormatter. SHA1, SHA256, SHA384, SHA512</param>
public DigitalSignature(int keySize, string hashAlgorithm)
{
_keySize = keySize;
_hashAlgorithm = hashAlgorithm;
}
/// <summary>
/// Generate new public and private keys
/// </summary>
public void AssignNewKey()
{
using (var rsa = new RSACryptoServiceProvider(_keySize))
{
rsa.PersistKeyInCsp = false;
_publicKey = rsa.ExportParameters(false);
_privateKey = rsa.ExportParameters(true);
}
}
/// <summary>
/// Loads a previously generated public key
/// </summary>
public void LoadPublicKey(string exponent, string modulus)
{
_publicKey = new RSAParameters
{
Exponent = Bytes.HexToByteArray(exponent),
Modulus = Bytes.HexToByteArray(modulus)
};
}
public void SavePublicKey(out string exponent, out string modulus)
{
exponent = Bytes.ByteArrayToHex(_publicKey.Exponent);
modulus = Bytes.ByteArrayToHex(_publicKey.Modulus);
}
/// <summary>
/// Loads a previously generated private key
/// </summary>
public void LoadPrivateKey(string exponent, string modulus, string p, string q, string dp, string dq, string inverseQ, string d)
{
_privateKey = new RSAParameters
{
Exponent = Bytes.HexToByteArray(exponent),
Modulus = Bytes.HexToByteArray(modulus),
P = Bytes.HexToByteArray(p),
Q = Bytes.HexToByteArray(q),
DP = Bytes.HexToByteArray(dp),
DQ = Bytes.HexToByteArray(dq),
InverseQ = Bytes.HexToByteArray(inverseQ),
D = Bytes.HexToByteArray(d)
};
}
public void SavePrivateKey(out string exponent, out string modulus, out string p, out string q, out string dp, out string dq, out string inverseQ, out string d)
{
exponent = _publicKey.Exponent == null ? string.Empty : Bytes.ByteArrayToHex(_publicKey.Exponent);
modulus = _publicKey.Modulus == null ? string.Empty : Bytes.ByteArrayToHex(_publicKey.Modulus);
p = _publicKey.P == null ? string.Empty : Bytes.ByteArrayToHex(_publicKey.P);
q = _publicKey.Q == null ? string.Empty : Bytes.ByteArrayToHex(_publicKey.Q);
dp = _publicKey.DP == null ? string.Empty : Bytes.ByteArrayToHex(_publicKey.DP);
dq = _publicKey.DQ == null ? string.Empty : Bytes.ByteArrayToHex(_publicKey.DQ);
inverseQ = _publicKey.InverseQ == null ? string.Empty : Bytes.ByteArrayToHex(_publicKey.InverseQ);
d = _publicKey.D == null ? string.Empty : Bytes.ByteArrayToHex(_publicKey.D);
}
/// <summary>
/// Sign a hash
/// </summary>
/// <param name="hashOfDataToSign">Data to sign</param>
/// <returns>Signature</returns>
public byte[] SignData(byte[] hashOfDataToSign)
{
using (var rsa = new RSACryptoServiceProvider(_keySize))
{
rsa.PersistKeyInCsp = false;
rsa.ImportParameters(_privateKey);
var rsaFormatter = new RSAPKCS1SignatureFormatter(rsa);
rsaFormatter.SetHashAlgorithm(_hashAlgorithm);
return rsaFormatter.CreateSignature(hashOfDataToSign);
}
}
/// <summary>
/// Verify a signature
/// </summary>
/// <param name="hashOfDataToSign">Data to verify</param>
/// <param name="signature">Signature to verify</param>
/// <returns>True if the signature matches</returns>
public bool VerifySignature(byte[] hashOfDataToSign, byte[] signature)
{
using (var rsa = new RSACryptoServiceProvider(_keySize))
{
rsa.ImportParameters(_publicKey);
var rsaDeformatter = new RSAPKCS1SignatureDeformatter(rsa);
rsaDeformatter.SetHashAlgorithm(_hashAlgorithm);
return rsaDeformatter.VerifySignature(hashOfDataToSign, signature);
}
}
}
}