aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Common/Cryptography/Pbkdf2.cs20
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);
}
}
}