-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide Argon2id overloads to pass password hashes as strings #15
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using System.Text; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using static Interop.Libsodium; | ||
|
||
namespace Geralt; | ||
|
@@ -35,6 +36,22 @@ public static void ComputeHash(Span<byte> hash, ReadOnlySpan<byte> password, int | |
if (ret != 0) { throw new InsufficientMemoryException("Insufficient memory to perform password hashing."); } | ||
} | ||
|
||
public static string ComputeHash(ReadOnlySpan<byte> password, int iterations, int memorySize) | ||
{ | ||
Validation.NotLessThanMin(nameof(iterations), iterations, MinIterations); | ||
Validation.NotLessThanMin(nameof(memorySize), memorySize, MinMemorySize); | ||
Sodium.Initialize(); | ||
nint hash = Marshal.AllocHGlobal(MaxHashSize); | ||
try { | ||
int ret = crypto_pwhash_str_alg(hash, password, (ulong)password.Length, (ulong)iterations, (nuint)memorySize, crypto_pwhash_argon2id_ALG_ARGON2ID13); | ||
if (ret != 0) { throw new InsufficientMemoryException("Insufficient memory to perform password hashing."); } | ||
return Marshal.PtrToStringAnsi(hash)!; | ||
} | ||
finally { | ||
Marshal.FreeHGlobal(hash); | ||
} | ||
} | ||
|
||
public static bool VerifyHash(ReadOnlySpan<byte> hash, ReadOnlySpan<byte> password) | ||
{ | ||
Validation.SizeBetween(nameof(hash), hash.Length, MinHashSize, MaxHashSize); | ||
|
@@ -43,6 +60,14 @@ public static bool VerifyHash(ReadOnlySpan<byte> hash, ReadOnlySpan<byte> passwo | |
return crypto_pwhash_str_verify(hash, password, (ulong)password.Length) == 0; | ||
} | ||
|
||
public static bool VerifyHash(string hash, ReadOnlySpan<byte> password) | ||
{ | ||
Validation.NotNull(nameof(hash), hash); | ||
ThrowIfInvalidHashPrefix(hash); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please change this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, it's probably better to do |
||
Sodium.Initialize(); | ||
return crypto_pwhash_str_verify(hash, password, (ulong)password.Length) == 0; | ||
} | ||
|
||
public static bool NeedsRehash(ReadOnlySpan<byte> hash, int iterations, int memorySize) | ||
{ | ||
Validation.SizeBetween(nameof(hash), hash.Length, MinHashSize, MaxHashSize); | ||
|
@@ -54,10 +79,28 @@ public static bool NeedsRehash(ReadOnlySpan<byte> hash, int iterations, int memo | |
return ret == -1 ? throw new FormatException("Invalid encoded password hash.") : ret == 1; | ||
} | ||
|
||
public static bool NeedsRehash(string hash, int iterations, int memorySize) | ||
{ | ||
Validation.NotNull(nameof(hash), hash); | ||
Validation.NotLessThanMin(nameof(iterations), iterations, MinIterations); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please change this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, it's probably better to do |
||
Validation.NotLessThanMin(nameof(memorySize), memorySize, MinMemorySize); | ||
ThrowIfInvalidHashPrefix(hash); | ||
Sodium.Initialize(); | ||
int ret = crypto_pwhash_str_needs_rehash(hash, (ulong)iterations, (nuint)memorySize); | ||
return ret == -1 ? throw new FormatException("Invalid encoded password hash.") : ret == 1; | ||
} | ||
|
||
private static void ThrowIfInvalidHashPrefix(ReadOnlySpan<byte> hash) | ||
{ | ||
if (!ConstantTime.Equals(hash[..HashPrefix.Length], Encoding.UTF8.GetBytes(HashPrefix))) { | ||
throw new FormatException("Invalid encoded password hash prefix."); | ||
} | ||
} | ||
|
||
private static void ThrowIfInvalidHashPrefix(string hash) | ||
{ | ||
if (!hash.StartsWith(HashPrefix)) { | ||
throw new FormatException("Invalid encoded password hash prefix."); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing
ComputeHash_String_Invalid
, not that there's much to test.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are also no
NeedsRehash
tests.