Skip to content

Commit ae75f4b

Browse files
committed
v6.3.0 - quick token hash
1 parent d77bdf7 commit ae75f4b

File tree

5 files changed

+56
-10
lines changed

5 files changed

+56
-10
lines changed

HISTORY.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
## v6.2
2-
- Added QuickEncryption (~3000x faster encryption and ~5500x faster decryption for small data)
1+
## v6.3.0
2+
- Added overload for `HashToken` in `TokenGenerator` for quick hash
33

4-
## v6.1
4+
## v6.2.0
5+
- Added `QuickEncryption` (~3000x faster encryption and ~5500x faster decryption for small data)
6+
7+
## v6.1.0
58
- Added target framework 8 (6 and 7 are still supported)
69

7-
## v6.0
10+
## v6.0.0
811
- Changed framework dependency from netstandard1.6 to net6.0
912

10-
## v5.0
13+
## v5.0.0
1114
- Removed code marked as obsolete in previous versions
1215

1316
## v4.5.0

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,10 @@ string GenerateToken(int length)
317317
// Hashes token (with random salt) so you don't have to store plain text token
318318
string HashToken(string token)
319319

320-
// Validates token hash that is created by calling HashToken(string)
320+
// Hashes token with SHA256 if quickHash is true (suitable for long random tokens with short expiry time), if false it works the same as HashToken(string)
321+
string HashToken(string token, bool quickHash)
322+
323+
// Validates token hash that is created by calling HashToken(string) or HashToken(string, bool)
321324
bool ValidateTokenHash(string token, string hash)
322325
```
323326

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.2.0</Version>
8+
<Version>6.3.0</Version>
99
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1010

1111
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>

src/EasyCrypto/TokenGenerator.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ namespace EasyCrypto;
77
/// </summary>
88
public class TokenGenerator
99
{
10-
private static readonly CryptoRandom _rand = new CryptoRandom();
11-
private static readonly PasswordHasher _hasher = new PasswordHasher(16, 500);
12-
10+
private readonly CryptoRandom _rand = new();
11+
private readonly PasswordHasher _hasher = new(16, 500);
12+
1313
/// <summary>
1414
/// Characters allowed in generated token by default
1515
/// </summary>
@@ -88,6 +88,22 @@ public string HashToken(string token)
8888
return "00" + _hasher.HashPasswordAndGenerateEmbeddedSaltAsString(token).BeautifyBase64();
8989
}
9090

91+
/// <summary>
92+
/// Hashes token
93+
/// </summary>
94+
/// <param name="token">Token to hash</param>
95+
/// <param name="quickHash">True for quick hash (SHA256, suitable for long random token with short expiry time), false to use old method</param>
96+
/// <returns>Hashed token with embedded salt</returns>
97+
public string HashToken(string token, bool quickHash)
98+
{
99+
if (quickHash)
100+
{
101+
return "01" + HashSha256(token);
102+
}
103+
104+
return HashToken(token);
105+
}
106+
91107
/// <summary>
92108
/// Validates token hash
93109
/// </summary>
@@ -104,6 +120,19 @@ public bool ValidateTokenHash(string token, string hash)
104120
return _hasher.ValidatePasswordWithEmbeddedSalt(token, hash.UglifyBase64());
105121
}
106122

123+
if (version == "01")
124+
{
125+
string hash2 = HashSha256(token);
126+
return hash == hash2;
127+
}
128+
107129
throw new InvalidOperationException("Unknown hash version, please update reference of EasyCrypto.");
108130
}
131+
132+
private static string HashSha256(string token)
133+
{
134+
SHA256 sha = SHA256.Create();
135+
byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(token));
136+
return Convert.ToBase64String(hash);
137+
}
109138
}

tests/EasyCrypto.Tests/TokenGeneratorTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,15 @@ public void NotValidHashedToken_Validate_ReturnsFalse()
4646
bool isValid = tokenGen.ValidateTokenHash(token, hash);
4747
Assert.False(isValid);
4848
}
49+
50+
[Fact]
51+
public void QuickHash_Verify_ReturnsTrue()
52+
{
53+
var tokenGen = new TokenGenerator();
54+
string token = tokenGen.GenerateToken(30);
55+
string hash = tokenGen.HashToken(token, true);
56+
57+
bool isValid = tokenGen.ValidateTokenHash(token, hash);
58+
Assert.True(isValid);
59+
}
4960
}

0 commit comments

Comments
 (0)