Skip to content

Commit d77bdf7

Browse files
committed
v 6.2.0
1 parent e6f6459 commit d77bdf7

30 files changed

+461
-206
lines changed

Benchmarks.md

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
// * Summary *
22

3-
BenchmarkDotNet=v0.12.1, OS=Windows 10.0.18363.1016 (1909/November2018Update/19H2)
4-
AMD Ryzen 7 2700X, 1 CPU, 16 logical and 8 physical cores
5-
.NET Core SDK=5.0.100-preview.7.20366.6
6-
[Host] : .NET Core 3.1.6 (CoreCLR 4.700.20.26901, CoreFX 4.700.20.31603), X64 RyuJIT
7-
DefaultJob : .NET Core 3.1.6 (CoreCLR 4.700.20.26901, CoreFX 4.700.20.31603), X64 RyuJIT
3+
BenchmarkDotNet v0.13.11, Windows 11 (10.0.22621.2861/22H2/2022Update/SunValley2)
4+
AMD Ryzen 5 5600X, 1 CPU, 12 logical and 6 physical cores
5+
.NET SDK 8.0.100
6+
[Host] : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2
7+
DefaultJob : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2
88

9-
| Method | Mean | Error | StdDev |
10-
|-------------------- |---------:|---------:|---------:|
11-
| RandomIntNoBuffer | 80.37 ns | 1.498 ns | 1.251 ns |
12-
| RandomIntWithBuffer | 60.88 ns | 1.064 ns | 0.995 ns |
9+
10+
| Method | Mean | Error | StdDev |
11+
|---------------- |--------------:|-----------:|-----------:|
12+
| AesEncrypt | 11,780.431 us | 26.9828 us | 25.2397 us |
13+
| AesDecrypt | 23,168.337 us | 52.8699 us | 46.8678 us |
14+
| AesEncryptQuick | 3.837 us | 0.0280 us | 0.0248 us |
15+
| AesDecryptQuick | 4.181 us | 0.0331 us | 0.0310 us |
1316

1417
// * Hints *
1518
Outliers
16-
CryptoRandomBanchmarks.RandomIntNoBuffer: Default -> 3 outliers were removed (88.08 ns..91.41 ns)
19+
EncryptionBenchmark.AesDecrypt: Default -> 1 outlier was removed (23.32 ms)
20+
EncryptionBenchmark.AesEncryptQuick: Default -> 1 outlier was removed (3.92 us)
1721

1822
// * Legends *
23+
Mean : Arithmetic mean of all measurements
24+
Error : Half of 99.9% confidence interval
25+
StdDev : Standard deviation of all measurements
26+
1 us : 1 Microsecond (0.000001 sec)
1927

20-
- Mean : Arithmetic mean of all measurements
21-
- Error : Half of 99.9% confidence interval
22-
- StdDev : Standard deviation of all measurements
23-
- 1 ns : 1 Nanosecond (0.000000001 sec)
28+
// ***** BenchmarkRunner: End *****
29+
Run time: 00:01:17 (77.51 sec), executed benchmarks: 4

EasyCrypto.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
2323
.github\workflows\dotnet-core.yml = .github\workflows\dotnet-core.yml
2424
EndProjectSection
2525
EndProject
26-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCrypto", "src\EasyCrypto\EasyCrypto.csproj", "{8FC6F2A1-EA44-4D4A-94E5-7CC8A83CDA53}"
26+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyCrypto", "src\EasyCrypto\EasyCrypto.csproj", "{8FC6F2A1-EA44-4D4A-94E5-7CC8A83CDA53}"
27+
EndProject
28+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCrypto.Benchmarks", "tests\EasyCrypto.Benchmarks\EasyCrypto.Benchmarks.csproj", "{AB334D8B-C2F7-4CC0-813D-6C46B3F4FE6C}"
2729
EndProject
2830
Global
2931
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -39,13 +41,18 @@ Global
3941
{8FC6F2A1-EA44-4D4A-94E5-7CC8A83CDA53}.Debug|Any CPU.Build.0 = Debug|Any CPU
4042
{8FC6F2A1-EA44-4D4A-94E5-7CC8A83CDA53}.Release|Any CPU.ActiveCfg = Release|Any CPU
4143
{8FC6F2A1-EA44-4D4A-94E5-7CC8A83CDA53}.Release|Any CPU.Build.0 = Release|Any CPU
44+
{AB334D8B-C2F7-4CC0-813D-6C46B3F4FE6C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
45+
{AB334D8B-C2F7-4CC0-813D-6C46B3F4FE6C}.Debug|Any CPU.Build.0 = Debug|Any CPU
46+
{AB334D8B-C2F7-4CC0-813D-6C46B3F4FE6C}.Release|Any CPU.ActiveCfg = Release|Any CPU
47+
{AB334D8B-C2F7-4CC0-813D-6C46B3F4FE6C}.Release|Any CPU.Build.0 = Release|Any CPU
4248
EndGlobalSection
4349
GlobalSection(SolutionProperties) = preSolution
4450
HideSolutionNode = FALSE
4551
EndGlobalSection
4652
GlobalSection(NestedProjects) = preSolution
4753
{3C82DF35-3ADB-4D8D-A8FF-75C4B28755CE} = {87B2E37E-B472-41A3-8051-EAA0BDFCB958}
4854
{8FC6F2A1-EA44-4D4A-94E5-7CC8A83CDA53} = {EE9C0B15-A82A-40D4-B4EB-DF463FF0E446}
55+
{AB334D8B-C2F7-4CC0-813D-6C46B3F4FE6C} = {87B2E37E-B472-41A3-8051-EAA0BDFCB958}
4956
EndGlobalSection
5057
GlobalSection(ExtensibilityGlobals) = postSolution
5158
SolutionGuid = {9402F085-2E7C-4504-9298-DC12E594AF74}

HISTORY.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
## v6.1
1+
## v6.2
2+
- Added QuickEncryption (~3000x faster encryption and ~5500x faster decryption for small data)
3+
4+
## v6.1
25
- Added target framework 8 (6 and 7 are still supported)
36

47
## v6.0

README.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Table of contents:
6161
- [Class TokenGenerator](#class-tokengenerator)
6262
- [Class IdGenerator](#class-idgenerator)
6363
- [Static Class RsaEncryption](#static-class-rsaencryption)
64-
64+
- [Class QuickEncryption](#class-quickencryption)
6565
---
6666

6767
### Static class AesEncryption
@@ -463,3 +463,50 @@ public void String_EncryptDecrypt_GivesEqualString()
463463
Assert.Equal(plainText, decrypted);
464464
}
465465
```
466+
467+
### Class QuickEncryption
468+
469+
When encrypting short data with password this class provides 3000x-5000x faster encryption and decryption compared
470+
to `AesEncryption` password methods. It must be used with `QuickEncryptionKey` which is randomly generated.
471+
`QuickEncryptionKey` can be stored as plain text using `ToString()` method which returns string representation
472+
of the key. String representation can be parsed with `Parse(string)` method.
473+
474+
`QuickEncryptionKey` has static methods:
475+
476+
```csharp
477+
QuickEncryptionKey CreateNew() // creates new random key
478+
QuickEncryptionKey Parse(string s) // parses existing key
479+
ToString() // converts key to string which can be parsed
480+
```
481+
482+
QuickEncryption constructor:
483+
484+
```csharp
485+
public QuickEncryption(QuickEncryptionKey key)
486+
```
487+
488+
QuickEncryption methods:
489+
490+
```csharp
491+
byte[] Encrypt(byte[] plainTextData)
492+
string Encrypt(string plainText)
493+
byte[] Decrypt(byte[] data)
494+
string Decrypt(string encryptedData)
495+
496+
static byte[] Encrypt(byte[] plainTextData, QuickEncryptionKey key)
497+
static string Encrypt(string plainText, QuickEncryptionKey key)
498+
static byte[] Decrypt(byte[] data, QuickEncryptionKey key)
499+
static string Decrypt(string encryptedData, QuickEncryptionKey key)
500+
```
501+
502+
Static methods can be used with any key, instance methods are used with key provided in constructor.
503+
504+
Example:
505+
506+
```csharp
507+
QuickEncryptionKey key = QuickEncryptionKey.CreateNew();
508+
// you can call `key.ToString()` to save key and later call `QuickEncryptionKey.Parse(string)` to get it back
509+
510+
string encrypted = QuickEncryption.Encrypt("some text to encrypt", key);
511+
string decrypted = QuickEncryption.Decrypt(encrypted, key);
512+
```

nuget-README.md

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# EasyCrypto
22

3+
34
Primary goal of this library is to enable users with little or no cryptography knowledge to encrypt and decrypt data in an easy and
45
safe manner as well work with passwords and random values.
56

6-
EasyCrypto is .NET 6, 7, 8 library that helps with
7+
EasyCrypto is .NET 6, 7, 8+ library that helps with
78
- Encryption and decryption of streams, byte arrays, strings and files
89
- Password generating, hashing and validating
910
- Generating crypto secure random bytes, integers and doubles
@@ -55,7 +56,7 @@ Table of contents:
5556
- [Class TokenGenerator](#class-tokengenerator)
5657
- [Class IdGenerator](#class-idgenerator)
5758
- [Static Class RsaEncryption](#static-class-rsaencryption)
58-
59+
- [Class QuickEncryption](#class-quickencryption)
5960
---
6061

6162
### Static class AesEncryption
@@ -457,3 +458,50 @@ public void String_EncryptDecrypt_GivesEqualString()
457458
Assert.Equal(plainText, decrypted);
458459
}
459460
```
461+
462+
### Class QuickEncryption
463+
464+
When encrypting short data with password this class provides 3000x-5000x faster encryption and decryption compared
465+
to `AesEncryption` password methods. It must be used with `QuickEncryptionKey` which is randomly generated.
466+
`QuickEncryptionKey` can be stored as plain text using `ToString()` method which returns string representation
467+
of the key. String representation can be parsed with `Parse(string)` method.
468+
469+
`QuickEncryptionKey` has static methods:
470+
471+
```csharp
472+
QuickEncryptionKey CreateNew() // creates new random key
473+
QuickEncryptionKey Parse(string s) // parses existing key
474+
ToString() // converts key to string which can be parsed
475+
```
476+
477+
QuickEncryption constructor:
478+
479+
```csharp
480+
public QuickEncryption(QuickEncryptionKey key)
481+
```
482+
483+
QuickEncryption methods:
484+
485+
```csharp
486+
byte[] Encrypt(byte[] plainTextData)
487+
string Encrypt(string plainText)
488+
byte[] Decrypt(byte[] data)
489+
string Decrypt(string encryptedData)
490+
491+
static byte[] Encrypt(byte[] plainTextData, QuickEncryptionKey key)
492+
static string Encrypt(string plainText, QuickEncryptionKey key)
493+
static byte[] Decrypt(byte[] data, QuickEncryptionKey key)
494+
static string Decrypt(string encryptedData, QuickEncryptionKey key)
495+
```
496+
497+
Static methods can be used with any key, instance methods are used with key provided in constructor.
498+
499+
Example:
500+
501+
```csharp
502+
QuickEncryptionKey key = QuickEncryptionKey.CreateNew();
503+
// you can call `key.ToString()` to save key and later call `QuickEncryptionKey.Parse(string)` to get it back
504+
505+
string encrypted = QuickEncryption.Encrypt("some text to encrypt", key);
506+
string decrypted = QuickEncryption.Decrypt(encrypted, key);
507+
```

src/EasyCrypto/EasyCrypto.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>disable</Nullable>
77
<LangVersion>11</LangVersion>
8-
<Version>6.1.0</Version>
8+
<Version>6.2.0</Version>
99
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1010

1111
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>

src/EasyCrypto/QuickEncryption.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
namespace EasyCrypto;
2+
3+
/// <summary>
4+
/// Faster AES-256 to be used only with generated keys, not passwords, use <see cref="QuickEncryptionKey"/>
5+
/// </summary>
6+
public class QuickEncryption
7+
{
8+
private readonly QuickEncryptionKey _key;
9+
10+
public QuickEncryption(QuickEncryptionKey key)
11+
{
12+
_key = key;
13+
}
14+
15+
public byte[] Encrypt(byte[] plainTextData) => Encrypt(plainTextData, _key);
16+
17+
public string Encrypt(string plainText) => Encrypt(plainText, _key);
18+
19+
public byte[] Decrypt(byte[] data) => Decrypt(data, _key);
20+
21+
public string Decrypt(string encryptedData) => Decrypt(encryptedData, _key);
22+
23+
public static string Encrypt(string plainText, QuickEncryptionKey key)
24+
{
25+
byte[] value = Encrypt(Encoding.UTF8.GetBytes(plainText), key);
26+
return Convert.ToBase64String(value);
27+
}
28+
29+
public static string Decrypt(string encryptedData, QuickEncryptionKey key)
30+
{
31+
byte[] data = Decrypt(Convert.FromBase64String(encryptedData), key);
32+
return Encoding.UTF8.GetString(data);
33+
}
34+
35+
public static byte[] Encrypt(byte[] plainTextData, QuickEncryptionKey key)
36+
{
37+
return AesEncryption.EncryptAndEmbedIv(plainTextData, key.Key);
38+
}
39+
40+
public static byte[] Decrypt(byte[] data, QuickEncryptionKey key)
41+
{
42+
return AesEncryption.DecryptWithEmbeddedIv(data, key.Key);
43+
}
44+
}

src/EasyCrypto/QuickEncryptionKey.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
namespace EasyCrypto;
2+
3+
/// <summary>
4+
/// Key to be used with <see cref="QuickEncryption"/>
5+
/// </summary>
6+
public class QuickEncryptionKey
7+
{
8+
internal string Value { get; }
9+
internal byte[] Key { get; }
10+
11+
private const int KeyLength = 32;
12+
13+
private QuickEncryptionKey(string value)
14+
{
15+
Value = value;
16+
SHA256 sha = SHA256.Create();
17+
Key = sha.ComputeHash(Encoding.UTF8.GetBytes(value));
18+
}
19+
20+
/// <summary>
21+
/// Creates new randomly generated key
22+
/// </summary>
23+
/// <returns></returns>
24+
public static QuickEncryptionKey CreateNew()
25+
{
26+
string token = TokenGenerator.Default.GenerateToken(KeyLength);
27+
return new QuickEncryptionKey(token);
28+
}
29+
30+
/// <summary>
31+
/// Parses key from string
32+
/// </summary>
33+
/// <param name="value">Value to parse</param>
34+
/// <returns><see cref="QuickEncryptionKey"/></returns>
35+
/// <exception cref="ArgumentException">Throw in case of not valid argument</exception>
36+
public static QuickEncryptionKey Parse(string value)
37+
{
38+
if (value.Length != KeyLength)
39+
{
40+
throw new ArgumentException("Not valid value");
41+
}
42+
43+
return new QuickEncryptionKey(value);
44+
}
45+
46+
/// <summary>
47+
/// Returns key represented as string
48+
/// </summary>
49+
/// <returns>String value of the key</returns>
50+
public override string ToString() => Value;
51+
}

tests/EasyCrypto.Banchmarks/BenchmarkDotNet.Artifacts/results/EasyCrypto.Benchmarks.CryptoRandomBenchmarks-report-github.md

Lines changed: 0 additions & 14 deletions
This file was deleted.

tests/EasyCrypto.Banchmarks/BenchmarkDotNet.Artifacts/results/EasyCrypto.Benchmarks.CryptoRandomBenchmarks-report.csv

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/EasyCrypto.Banchmarks/BenchmarkDotNet.Artifacts/results/EasyCrypto.Benchmarks.CryptoRandomBenchmarks-report.html

Lines changed: 0 additions & 31 deletions
This file was deleted.

tests/EasyCrypto.Banchmarks/BenchmarkDotNet.Artifacts/results/EasyCrypto.Benchmarks.PasswordHasherAndValidatorBenchmarks-report-github.md

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/EasyCrypto.Banchmarks/BenchmarkDotNet.Artifacts/results/EasyCrypto.Benchmarks.PasswordHasherAndValidatorBenchmarks-report.csv

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)