From 2fd9253cedb5cbb871783f30921232e500aa6459 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 28 Oct 2022 17:48:30 -0500 Subject: add variable length key gen --- src/slskd/Common/Cryptography/Pbkdf2.cs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'src') 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; /// @@ -29,16 +30,29 @@ namespace slskd.Cryptography /// Gets a 256 bit (32 byte) key derived from the specified using PBKDF2/RFC 2898. /// /// The password from which to derive the key. - /// The desired length of the key, in bytes. /// The derived key. - 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); + } + + /// + /// Gets a variable key derived from the specified and using PBKDF2/RFC 2898. + /// + /// The password from which to derive the key. + /// The value with which to salt the key. + /// The desired length of the key, in bytes. + /// The derived key. + 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); } } } -- cgit v1.2.3