diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-10-28 17:48:30 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2022-10-28 17:48:30 -0500 |
| commit | 2fd9253cedb5cbb871783f30921232e500aa6459 (patch) | |
| tree | 30c24e0629072f3df98eca388f18a65049f90007 /src | |
| parent | 923627328c8e40a97f0d66be7c7d5dbaba949e6e (diff) | |
| download | slskd-2fd9253cedb5cbb871783f30921232e500aa6459.tar.gz | |
add variable length key gen
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/Cryptography/Pbkdf2.cs | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/src/slskd/Common/Cryptography/Pbkdf2.cs b/src/slskd/Common/Cryptography/Pbkdf2.cs index eea5c966..15b4a7ed 100644 --- a/src/slskd/Common/Cryptography/Pbkdf2.cs +++ b/src/slskd/Common/Cryptography/Pbkdf2.cs @@ -18,6 +18,7 @@ namespace slskd.Cryptography { using System.Security.Cryptography; + using System.Text; using Microsoft.AspNetCore.Cryptography.KeyDerivation; /// <summary> @@ -29,16 +30,29 @@ namespace slskd.Cryptography /// Gets a 256 bit (32 byte) key derived from the specified <paramref name="password"/> using PBKDF2/RFC 2898. /// </summary> /// <param name="password">The password from which to derive the key.</param> - /// <param name="length">The desired length of the key, in bytes.</param> /// <returns>The derived key.</returns> - public static byte[] GetKey(string password, int length = 32) + public static byte[] GetKey(string password) { byte[] salt = new byte[16]; int iterations = 1000; using var rng = RandomNumberGenerator.Create(); rng.GetBytes(salt); - return KeyDerivation.Pbkdf2(password, salt, KeyDerivationPrf.HMACSHA256, iterations, length); + return KeyDerivation.Pbkdf2(password, salt, KeyDerivationPrf.HMACSHA256, iterations, 32); + } + + /// <summary> + /// Gets a variable <paramref name="length"/> key derived from the specified <paramref name="password"/> and <paramref name="salt"/> using PBKDF2/RFC 2898. + /// </summary> + /// <param name="password">The password from which to derive the key.</param> + /// <param name="salt">The value with which to salt the key.</param> + /// <param name="length">The desired length of the key, in bytes.</param> + /// <returns>The derived key.</returns> + public static byte[] GetKey(string password, string salt, int length) + { + int iterations = 1000; + + return KeyDerivation.Pbkdf2(password, Encoding.UTF8.GetBytes(salt), KeyDerivationPrf.HMACSHA256, iterations, length); } } } |
