Skip to content

Commit 1f58f78

Browse files
authored
Merge pull request #10 from alexinea/master
Refactor RSA encryption provider
2 parents a383254 + e0eaefd commit 1f58f78

23 files changed

+693
-325
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# COSMOSLOOPS/Cosmos.Encryption <a href="https://www.nuget.org/packages/Cosmos.Encryption/" rel="nofollow"><img src="https://img.shields.io/nuget/v/Cosmos.Encryption.svg?style=flat" alt="NuGet Version" data-canonical-src="https://img.shields.io/nuget/v/Cosmos.Encryption.svg?style=flat" style="max-width:100%;"></a>
22

3-
[Cosmos.Encryption](https://github.com/cosmos-loops/Cosmos.Encryption) is an inline project of [COSMOS LOOPS PROGRAMME](https://github.com/cosmos-loops).
3+
[Cosmos.Encryption](https://github.com/cosmos-loops/Cosmos.Encryption) is an inline project of [Cosmosloops labs.](https://github.com/cosmos-loops).
44

55
## Install
66

@@ -17,6 +17,9 @@ Install-Package Cosmos.Encryption
1717
- SHA1/256/384/512
1818
- SM3
1919
- HMAC
20+
- MurmurHash2
21+
- MurmurHash3
22+
- Time33/DBJ33A
2023
- AES
2124
- DES/TripleDES
2225
- RC4
@@ -78,9 +81,11 @@ People or projects that have made a great contribbution to this project:
7881
- [Portable.BouncyCastle](https://github.com/onovotny/bc-csharp)
7982
- [ToolGood.RCX](https://github.com/toolgood/RCX)
8083
- [xxtea/xxtea-dotnet](https://github.com/xxtea/xxtea-dotnet)
84+
- [murmurhash-net](https://github.com/darrenkopp/murmurhash-net/)
85+
- [odinmillion/MurmurHash.Net](https://github.com/odinmillion/MurmurHash.Net)
8186

8287
# License
8388

84-
Member project of [COSMOS LOOPS PROGRAMME](https://github.com/cosmos-loops).
89+
Member project of [Cosmosloops labs.](https://github.com/cosmos-loops).
8590

86-
[Apache 2.0 License](/LICENSE)
91+
[Apache License 2.0](/LICENSE)

build/version.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<VersionMajor>0</VersionMajor>
44
<VersionMinor>1</VersionMinor>
5-
<VersionPatch>2-alpha1-100001</VersionPatch>
5+
<VersionPatch>2-alpha1-100004</VersionPatch>
66
<VersionQuality></VersionQuality>
77
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
88
</PropertyGroup>

src/Cosmos.Encryption/Cosmos/Encryption/Asymmetric/DSAEncryptionProvider.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ public static class DSAEncryptionProvider {
1717
/// <param name="keySize"></param>
1818
/// <returns></returns>
1919
public static DSAKey CreateKey(int keySize = 1024) {
20-
using (var provider = new DSACryptoServiceProvider(keySize)) {
21-
var key = new DSAKey();
22-
var pa = provider.ExportParameters(true);
23-
key.PrivateKey = provider.ToXmlString(true);
24-
key.PublicKey = provider.ToXmlString(false);
25-
return key;
26-
}
20+
using var provider = new DSACryptoServiceProvider(keySize);
21+
var key = new DSAKey();
22+
//var pa = provider.ExportParameters(true);
23+
key.PrivateKey = provider.ToXmlString(true);
24+
key.PublicKey = provider.ToXmlString(false);
25+
return key;
2726
}
2827

2928
/// <summary>
@@ -33,10 +32,9 @@ public static DSAKey CreateKey(int keySize = 1024) {
3332
/// <param name="privateKey"></param>
3433
/// <returns></returns>
3534
public static byte[] Signature(byte[] buffer, string privateKey) {
36-
using (var provider = new DSACryptoServiceProvider()) {
37-
provider.FromXmlString(privateKey);
38-
return provider.SignData(buffer);
39-
}
35+
using var provider = new DSACryptoServiceProvider();
36+
provider.FromXmlString(privateKey);
37+
return provider.SignData(buffer);
4038
}
4139

4240
/// <summary>
@@ -82,10 +80,9 @@ public static byte[] Signature(string data, DSAKey key, Encoding encoding = null
8280
/// <param name="rgbSignature"></param>
8381
/// <returns></returns>
8482
public static bool Verify(byte[] buffer, string publicKey, byte[] rgbSignature) {
85-
using (var provider = new DSACryptoServiceProvider()) {
86-
provider.FromXmlString(publicKey);
87-
return provider.VerifyData(buffer, rgbSignature);
88-
}
83+
using var provider = new DSACryptoServiceProvider();
84+
provider.FromXmlString(publicKey);
85+
return provider.VerifyData(buffer, rgbSignature);
8986
}
9087

9188
/// <summary>
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
using System;
2+
using System.Security.Cryptography;
3+
using System.Text;
4+
using Cosmos.Encryption.Core;
5+
using Cosmos.Encryption.Core.Internals.Extensions;
6+
7+
// ReSharper disable once CheckNamespace
8+
namespace Cosmos.Encryption {
9+
// ReSharper disable once InconsistentNaming
10+
public static partial class RSAEncryptionProvider {
11+
12+
#region Extensions for export and import
13+
14+
/// <summary>
15+
/// Export RSA private key
16+
/// </summary>
17+
/// <param name="rsa"></param>
18+
/// <param name="type"></param>
19+
/// <param name="usePemFormat"></param>
20+
/// <returns></returns>
21+
public static string ExportPrivateKey(this RSA rsa, RSAKeyTypes type, bool usePemFormat = false) {
22+
var key = type switch {
23+
RSAKeyTypes.XML => rsa.ToLvccXmlString(true),
24+
RSAKeyTypes.JSON => rsa.ToJsonString(true),
25+
#if NETCOREAPP3_1 || NETSTANDARD2_1
26+
RSAKeyTypes.Pkcs1 => Base64ConvertProvider.ToBase64String(rsa.ExportRSAPrivateKey()),
27+
RSAKeyTypes.Pkcs8 => Base64ConvertProvider.ToBase64String(rsa.ExportPkcs8PrivateKey()),
28+
#else
29+
RSAKeyTypes.Pkcs1 => rsa.ToPkcs1PrivateString(),
30+
RSAKeyTypes.Pkcs8 => rsa.ToPkcs8PrivateString(),
31+
#endif
32+
_ => throw new NotSupportedException("Unknown RSA key type.")
33+
};
34+
35+
if (usePemFormat) {
36+
key = type switch {
37+
RSAKeyTypes.XML => key,
38+
RSAKeyTypes.JSON => key,
39+
RSAKeyTypes.Pkcs1 => RSAPemFormatHelper.Pkcs1PrivateKeyFormat(key),
40+
RSAKeyTypes.Pkcs8 => RSAPemFormatHelper.Pkcs8PrivateKeyFormat(key),
41+
_ => throw new NotSupportedException("Unknown RSA key type.")
42+
};
43+
}
44+
45+
return key;
46+
}
47+
48+
/// <summary>
49+
/// Export RSA public key
50+
/// </summary>
51+
/// <param name="rsa"></param>
52+
/// <param name="type"></param>
53+
/// <param name="usePemFormat"></param>
54+
/// <returns></returns>
55+
public static string ExportPublicKey(this RSA rsa, RSAKeyTypes type, bool usePemFormat = false) {
56+
var key = type switch {
57+
RSAKeyTypes.XML => rsa.ToLvccXmlString(false),
58+
RSAKeyTypes.JSON => rsa.ToJsonString(false),
59+
#if NETCOREAPP3_1 || NETSTANDARD2_1
60+
RSAKeyTypes.Pkcs1 => Base64ConvertProvider.ToBase64String(rsa.ExportRSAPublicKey()),
61+
RSAKeyTypes.Pkcs8 => Base64ConvertProvider.ToBase64String(rsa.ExportRSAPublicKey()),
62+
#else
63+
RSAKeyTypes.Pkcs1 => rsa.ToPkcs1PublicString(),
64+
RSAKeyTypes.Pkcs8 => rsa.ToPkcs8PublicString(),
65+
#endif
66+
_ => throw new NotSupportedException("Unknown RSA key type.")
67+
};
68+
69+
if (usePemFormat) {
70+
key = type switch {
71+
RSAKeyTypes.XML => key,
72+
RSAKeyTypes.JSON => key,
73+
RSAKeyTypes.Pkcs1 => RSAPemFormatHelper.Pkcs1PublicKeyFormat(key),
74+
RSAKeyTypes.Pkcs8 => RSAPemFormatHelper.Pkcs8PublicKeyFormat(key),
75+
_ => throw new NotSupportedException("Unknown RSA key type.")
76+
};
77+
}
78+
79+
return key;
80+
}
81+
82+
/// <summary>
83+
/// Import RSA private key
84+
/// </summary>
85+
/// <param name="rsa"></param>
86+
/// <param name="type"></param>
87+
/// <param name="privateKey"></param>
88+
/// <param name="isPem"></param>
89+
public static void ImportPrivateKey(this RSA rsa, RSAKeyTypes type, string privateKey, bool isPem = false) {
90+
if (isPem) {
91+
privateKey = type switch {
92+
RSAKeyTypes.XML => privateKey,
93+
RSAKeyTypes.JSON => privateKey,
94+
RSAKeyTypes.Pkcs1 => RSAPemFormatHelper.Pkcs1PrivateKeyFormatRemove(privateKey),
95+
RSAKeyTypes.Pkcs8 => RSAPemFormatHelper.Pkcs8PrivateKeyFormatRemove(privateKey),
96+
_ => throw new NotSupportedException("Unknown RSA key type.")
97+
};
98+
}
99+
100+
switch (type) {
101+
case RSAKeyTypes.XML:
102+
rsa.FromLvccXmlString(privateKey);
103+
break;
104+
105+
case RSAKeyTypes.JSON:
106+
rsa.FromJsonString(privateKey);
107+
break;
108+
109+
case RSAKeyTypes.Pkcs1:
110+
#if NETCOREAPP3_1 || NETSTANDARD2_1
111+
rsa.ImportRSAPrivateKey(Convert.FromBase64String(privateKey), out _);
112+
#else
113+
rsa.FromPkcs1PrivateString(privateKey, out _);
114+
#endif
115+
break;
116+
117+
case RSAKeyTypes.Pkcs8:
118+
#if NETCOREAPP3_1 || NETSTANDARD2_1
119+
rsa.ImportPkcs8PrivateKey(Convert.FromBase64String(privateKey), out _);
120+
#else
121+
rsa.FromPkcs8PrivateString(privateKey, out _);
122+
#endif
123+
break;
124+
}
125+
}
126+
127+
/// <summary>
128+
/// Import RSA public key
129+
/// </summary>
130+
/// <param name="rsa"></param>
131+
/// <param name="type"></param>
132+
/// <param name="publicKey"></param>
133+
/// <param name="isPem"></param>
134+
public static void ImportPublicKey(this RSA rsa, RSAKeyTypes type, string publicKey, bool isPem = false) {
135+
if (isPem) {
136+
publicKey = type switch {
137+
RSAKeyTypes.XML => publicKey,
138+
RSAKeyTypes.JSON => publicKey,
139+
RSAKeyTypes.Pkcs1 => RSAPemFormatHelper.Pkcs1PublicKeyFormatRemove(publicKey),
140+
RSAKeyTypes.Pkcs8 => RSAPemFormatHelper.Pkcs8PublicKeyFormatRemove(publicKey),
141+
_ => throw new NotSupportedException("Unknown RSA key type.")
142+
};
143+
}
144+
145+
switch (type) {
146+
case RSAKeyTypes.XML:
147+
rsa.FromLvccXmlString(publicKey);
148+
break;
149+
150+
case RSAKeyTypes.JSON:
151+
rsa.FromJsonString(publicKey);
152+
break;
153+
154+
case RSAKeyTypes.Pkcs1:
155+
#if NETCOREAPP3_1 || NETSTANDARD2_1
156+
rsa.ImportRSAPublicKey(Convert.FromBase64String(publicKey), out _);
157+
break;
158+
#else
159+
rsa.FromPkcs1PublicString(publicKey, out _);
160+
break;
161+
#endif
162+
163+
case RSAKeyTypes.Pkcs8:
164+
#if NETCOREAPP3_1 || NETSTANDARD2_1
165+
rsa.ImportRSAPublicKey(Convert.FromBase64String(publicKey), out _);
166+
break;
167+
#else
168+
rsa.FromPkcs8PublicString(publicKey, out _);
169+
break;
170+
#endif
171+
}
172+
}
173+
174+
#endregion
175+
176+
#region Extensions for touching RSA utils.
177+
178+
private static RSABase TouchRsaUtilFromPublicKey(RSAKeyTypes keyType, Encoding encoding, string publicKey, RSAKeySizeTypes sizeType) {
179+
RSABase rsa = keyType switch {
180+
RSAKeyTypes.XML => new RSAXmlUtil(encoding, publicKey, keySize: (int) sizeType),
181+
RSAKeyTypes.JSON => new RSAJsonUtil(encoding, publicKey, keySize: (int) sizeType),
182+
RSAKeyTypes.Pkcs1 => new RSAPkcs1Util(encoding, publicKey, keySize: (int) sizeType),
183+
RSAKeyTypes.Pkcs8 => new RSAPkcs8Util(encoding, publicKey, keySize: (int) sizeType),
184+
_ => throw new NotSupportedException("Unknown RSA key type.")
185+
};
186+
187+
return rsa;
188+
}
189+
190+
private static RSABase TouchRsaUtilFromPrivateKey(RSAKeyTypes keyType, Encoding encoding, string privateKey, RSAKeySizeTypes sizeType) {
191+
RSABase rsa = keyType switch {
192+
RSAKeyTypes.XML => new RSAXmlUtil(encoding, null, privateKey, (int) sizeType),
193+
RSAKeyTypes.JSON => new RSAXmlUtil(encoding, null, privateKey, (int) sizeType),
194+
RSAKeyTypes.Pkcs1 => new RSAPkcs1Util(encoding, null, privateKey, (int) sizeType),
195+
RSAKeyTypes.Pkcs8 => new RSAPkcs8Util(encoding, null, privateKey, (int) sizeType),
196+
_ => throw new NotSupportedException("Unknown RSA key type."),
197+
};
198+
199+
return rsa;
200+
}
201+
202+
#endregion
203+
204+
}
205+
}

src/Cosmos.Encryption/Cosmos/Encryption/Asymmetric/RSAEncryptionProvider.Factory.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,10 @@ public static RSAKey CreateKey(RSAKeySizeTypes size = RSAKeySizeTypes.R2048, RSA
4646
/// </summary>
4747
/// <param name="key"></param>
4848
/// <returns></returns>
49-
public static RSA CreateFromXmlKey(string key) {
49+
public static RSA CreateKeyFromXml(string key) {
5050
if (string.IsNullOrWhiteSpace(key)) {
5151
throw new ArgumentNullException(nameof(key));
5252
}
53-
5453
var rsa = RSA.Create();
5554
rsa.FromLvccXmlString(key);
5655
return rsa;
@@ -61,9 +60,8 @@ public static RSA CreateFromXmlKey(string key) {
6160
/// </summary>
6261
/// <param name="key"></param>
6362
/// <returns></returns>
64-
public static RSA CreateFromJsonKey(string key) {
63+
public static RSA CreateKeyFromJson(string key) {
6564
Checker.Key(key);
66-
6765
var rsa = RSA.Create();
6866
rsa.FromJsonString(key);
6967
return rsa;
@@ -74,9 +72,8 @@ public static RSA CreateFromJsonKey(string key) {
7472
/// </summary>
7573
/// <param name="key"></param>
7674
/// <returns></returns>
77-
public static RSA CreateFromPkcs1PublicKey(string key) {
75+
public static RSA CreatePublicKeyFromPkcs1(string key) {
7876
Checker.Key(key);
79-
8077
var rsa = RSA.Create();
8178
rsa.FromPkcs1PublicString(key, out _);
8279
return rsa;
@@ -87,9 +84,8 @@ public static RSA CreateFromPkcs1PublicKey(string key) {
8784
/// </summary>
8885
/// <param name="key"></param>
8986
/// <returns></returns>
90-
public static RSA CreateFromPkcs1PrivateKey(string key) {
87+
public static RSA CreatePrivateKeyFromPkcs1(string key) {
9188
Checker.Key(key);
92-
9389
var rsa = RSA.Create();
9490
rsa.FromPkcs1PrivateString(key, out _);
9591
return rsa;
@@ -100,9 +96,8 @@ public static RSA CreateFromPkcs1PrivateKey(string key) {
10096
/// </summary>
10197
/// <param name="key"></param>
10298
/// <returns></returns>
103-
public static RSA CreateFromPkcs8PublicKey(string key) {
99+
public static RSA CreatePublicKeyFromPkcs8(string key) {
104100
Checker.Key(key);
105-
106101
var rsa = RSA.Create();
107102
rsa.FromPkcs8PublicString(key, out _);
108103
return rsa;
@@ -113,7 +108,8 @@ public static RSA CreateFromPkcs8PublicKey(string key) {
113108
/// </summary>
114109
/// <param name="key"></param>
115110
/// <returns></returns>
116-
public static RSA CreateFromPkcs8PrivateKey(string key) {
111+
public static RSA CreatePrivateKeyFromPkcs8(string key) {
112+
Checker.Key(key);
117113
var rsa = RSA.Create();
118114
rsa.FromPkcs8PrivateString(key, out _);
119115
return rsa;

0 commit comments

Comments
 (0)