Skip to content

Commit

Permalink
v 6.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
stanac committed Dec 23, 2023
1 parent e6f6459 commit d77bdf7
Show file tree
Hide file tree
Showing 30 changed files with 461 additions and 206 deletions.
34 changes: 20 additions & 14 deletions Benchmarks.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
// * Summary *

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

| Method | Mean | Error | StdDev |
|-------------------- |---------:|---------:|---------:|
| RandomIntNoBuffer | 80.37 ns | 1.498 ns | 1.251 ns |
| RandomIntWithBuffer | 60.88 ns | 1.064 ns | 0.995 ns |

| Method | Mean | Error | StdDev |
|---------------- |--------------:|-----------:|-----------:|
| AesEncrypt | 11,780.431 us | 26.9828 us | 25.2397 us |
| AesDecrypt | 23,168.337 us | 52.8699 us | 46.8678 us |
| AesEncryptQuick | 3.837 us | 0.0280 us | 0.0248 us |
| AesDecryptQuick | 4.181 us | 0.0331 us | 0.0310 us |

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

// * Legends *
Mean : Arithmetic mean of all measurements
Error : Half of 99.9% confidence interval
StdDev : Standard deviation of all measurements
1 us : 1 Microsecond (0.000001 sec)

- Mean : Arithmetic mean of all measurements
- Error : Half of 99.9% confidence interval
- StdDev : Standard deviation of all measurements
- 1 ns : 1 Nanosecond (0.000000001 sec)
// ***** BenchmarkRunner: End *****
Run time: 00:01:17 (77.51 sec), executed benchmarks: 4
9 changes: 8 additions & 1 deletion EasyCrypto.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.github\workflows\dotnet-core.yml = .github\workflows\dotnet-core.yml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCrypto", "src\EasyCrypto\EasyCrypto.csproj", "{8FC6F2A1-EA44-4D4A-94E5-7CC8A83CDA53}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyCrypto", "src\EasyCrypto\EasyCrypto.csproj", "{8FC6F2A1-EA44-4D4A-94E5-7CC8A83CDA53}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCrypto.Benchmarks", "tests\EasyCrypto.Benchmarks\EasyCrypto.Benchmarks.csproj", "{AB334D8B-C2F7-4CC0-813D-6C46B3F4FE6C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -39,13 +41,18 @@ Global
{8FC6F2A1-EA44-4D4A-94E5-7CC8A83CDA53}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8FC6F2A1-EA44-4D4A-94E5-7CC8A83CDA53}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8FC6F2A1-EA44-4D4A-94E5-7CC8A83CDA53}.Release|Any CPU.Build.0 = Release|Any CPU
{AB334D8B-C2F7-4CC0-813D-6C46B3F4FE6C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AB334D8B-C2F7-4CC0-813D-6C46B3F4FE6C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AB334D8B-C2F7-4CC0-813D-6C46B3F4FE6C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AB334D8B-C2F7-4CC0-813D-6C46B3F4FE6C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{3C82DF35-3ADB-4D8D-A8FF-75C4B28755CE} = {87B2E37E-B472-41A3-8051-EAA0BDFCB958}
{8FC6F2A1-EA44-4D4A-94E5-7CC8A83CDA53} = {EE9C0B15-A82A-40D4-B4EB-DF463FF0E446}
{AB334D8B-C2F7-4CC0-813D-6C46B3F4FE6C} = {87B2E37E-B472-41A3-8051-EAA0BDFCB958}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9402F085-2E7C-4504-9298-DC12E594AF74}
Expand Down
5 changes: 4 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## v6.1
## v6.2
- Added QuickEncryption (~3000x faster encryption and ~5500x faster decryption for small data)

## v6.1
- Added target framework 8 (6 and 7 are still supported)

## v6.0
Expand Down
49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Table of contents:
- [Class TokenGenerator](#class-tokengenerator)
- [Class IdGenerator](#class-idgenerator)
- [Static Class RsaEncryption](#static-class-rsaencryption)

- [Class QuickEncryption](#class-quickencryption)
---

### Static class AesEncryption
Expand Down Expand Up @@ -463,3 +463,50 @@ public void String_EncryptDecrypt_GivesEqualString()
Assert.Equal(plainText, decrypted);
}
```

### Class QuickEncryption

When encrypting short data with password this class provides 3000x-5000x faster encryption and decryption compared
to `AesEncryption` password methods. It must be used with `QuickEncryptionKey` which is randomly generated.
`QuickEncryptionKey` can be stored as plain text using `ToString()` method which returns string representation
of the key. String representation can be parsed with `Parse(string)` method.

`QuickEncryptionKey` has static methods:

```csharp
QuickEncryptionKey CreateNew() // creates new random key
QuickEncryptionKey Parse(string s) // parses existing key
ToString() // converts key to string which can be parsed
```

QuickEncryption constructor:

```csharp
public QuickEncryption(QuickEncryptionKey key)
```

QuickEncryption methods:

```csharp
byte[] Encrypt(byte[] plainTextData)
string Encrypt(string plainText)
byte[] Decrypt(byte[] data)
string Decrypt(string encryptedData)

static byte[] Encrypt(byte[] plainTextData, QuickEncryptionKey key)
static string Encrypt(string plainText, QuickEncryptionKey key)
static byte[] Decrypt(byte[] data, QuickEncryptionKey key)
static string Decrypt(string encryptedData, QuickEncryptionKey key)
```

Static methods can be used with any key, instance methods are used with key provided in constructor.

Example:

```csharp
QuickEncryptionKey key = QuickEncryptionKey.CreateNew();
// you can call `key.ToString()` to save key and later call `QuickEncryptionKey.Parse(string)` to get it back
string encrypted = QuickEncryption.Encrypt("some text to encrypt", key);
string decrypted = QuickEncryption.Decrypt(encrypted, key);
```
52 changes: 50 additions & 2 deletions nuget-README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# EasyCrypto


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

EasyCrypto is .NET 6, 7, 8 library that helps with
EasyCrypto is .NET 6, 7, 8+ library that helps with
- Encryption and decryption of streams, byte arrays, strings and files
- Password generating, hashing and validating
- Generating crypto secure random bytes, integers and doubles
Expand Down Expand Up @@ -55,7 +56,7 @@ Table of contents:
- [Class TokenGenerator](#class-tokengenerator)
- [Class IdGenerator](#class-idgenerator)
- [Static Class RsaEncryption](#static-class-rsaencryption)

- [Class QuickEncryption](#class-quickencryption)
---

### Static class AesEncryption
Expand Down Expand Up @@ -457,3 +458,50 @@ public void String_EncryptDecrypt_GivesEqualString()
Assert.Equal(plainText, decrypted);
}
```

### Class QuickEncryption

When encrypting short data with password this class provides 3000x-5000x faster encryption and decryption compared
to `AesEncryption` password methods. It must be used with `QuickEncryptionKey` which is randomly generated.
`QuickEncryptionKey` can be stored as plain text using `ToString()` method which returns string representation
of the key. String representation can be parsed with `Parse(string)` method.

`QuickEncryptionKey` has static methods:

```csharp
QuickEncryptionKey CreateNew() // creates new random key
QuickEncryptionKey Parse(string s) // parses existing key
ToString() // converts key to string which can be parsed
```

QuickEncryption constructor:

```csharp
public QuickEncryption(QuickEncryptionKey key)
```

QuickEncryption methods:

```csharp
byte[] Encrypt(byte[] plainTextData)
string Encrypt(string plainText)
byte[] Decrypt(byte[] data)
string Decrypt(string encryptedData)

static byte[] Encrypt(byte[] plainTextData, QuickEncryptionKey key)
static string Encrypt(string plainText, QuickEncryptionKey key)
static byte[] Decrypt(byte[] data, QuickEncryptionKey key)
static string Decrypt(string encryptedData, QuickEncryptionKey key)
```

Static methods can be used with any key, instance methods are used with key provided in constructor.

Example:

```csharp
QuickEncryptionKey key = QuickEncryptionKey.CreateNew();
// you can call `key.ToString()` to save key and later call `QuickEncryptionKey.Parse(string)` to get it back
string encrypted = QuickEncryption.Encrypt("some text to encrypt", key);
string decrypted = QuickEncryption.Decrypt(encrypted, key);
```
2 changes: 1 addition & 1 deletion src/EasyCrypto/EasyCrypto.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<LangVersion>11</LangVersion>
<Version>6.1.0</Version>
<Version>6.2.0</Version>
<PackageLicenseExpression>MIT</PackageLicenseExpression>

<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
Expand Down
44 changes: 44 additions & 0 deletions src/EasyCrypto/QuickEncryption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace EasyCrypto;

/// <summary>
/// Faster AES-256 to be used only with generated keys, not passwords, use <see cref="QuickEncryptionKey"/>
/// </summary>
public class QuickEncryption
{
private readonly QuickEncryptionKey _key;

public QuickEncryption(QuickEncryptionKey key)
{
_key = key;
}

public byte[] Encrypt(byte[] plainTextData) => Encrypt(plainTextData, _key);

public string Encrypt(string plainText) => Encrypt(plainText, _key);

public byte[] Decrypt(byte[] data) => Decrypt(data, _key);

public string Decrypt(string encryptedData) => Decrypt(encryptedData, _key);

public static string Encrypt(string plainText, QuickEncryptionKey key)
{
byte[] value = Encrypt(Encoding.UTF8.GetBytes(plainText), key);
return Convert.ToBase64String(value);
}

public static string Decrypt(string encryptedData, QuickEncryptionKey key)
{
byte[] data = Decrypt(Convert.FromBase64String(encryptedData), key);
return Encoding.UTF8.GetString(data);
}

public static byte[] Encrypt(byte[] plainTextData, QuickEncryptionKey key)
{
return AesEncryption.EncryptAndEmbedIv(plainTextData, key.Key);
}

public static byte[] Decrypt(byte[] data, QuickEncryptionKey key)
{
return AesEncryption.DecryptWithEmbeddedIv(data, key.Key);
}
}
51 changes: 51 additions & 0 deletions src/EasyCrypto/QuickEncryptionKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace EasyCrypto;

/// <summary>
/// Key to be used with <see cref="QuickEncryption"/>
/// </summary>
public class QuickEncryptionKey
{
internal string Value { get; }
internal byte[] Key { get; }

private const int KeyLength = 32;

private QuickEncryptionKey(string value)
{
Value = value;
SHA256 sha = SHA256.Create();
Key = sha.ComputeHash(Encoding.UTF8.GetBytes(value));
}

/// <summary>
/// Creates new randomly generated key
/// </summary>
/// <returns></returns>
public static QuickEncryptionKey CreateNew()
{
string token = TokenGenerator.Default.GenerateToken(KeyLength);
return new QuickEncryptionKey(token);
}

/// <summary>
/// Parses key from string
/// </summary>
/// <param name="value">Value to parse</param>
/// <returns><see cref="QuickEncryptionKey"/></returns>
/// <exception cref="ArgumentException">Throw in case of not valid argument</exception>
public static QuickEncryptionKey Parse(string value)
{
if (value.Length != KeyLength)
{
throw new ArgumentException("Not valid value");
}

return new QuickEncryptionKey(value);
}

/// <summary>
/// Returns key represented as string
/// </summary>
/// <returns>String value of the key</returns>
public override string ToString() => Value;
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit d77bdf7

Please sign in to comment.