aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Security/PBKDF2.cs
blob: 4a857135298308e089a90473183699733581837c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
namespace slskd.Security
{
    using Microsoft.AspNetCore.Cryptography.KeyDerivation;
    using System.Security.Cryptography;

    /// <summary>
    ///     PBKDF2/RFC 2898 implementation 
    /// </summary>
    public static class PBKDF2
    {
        /// <summary>
        ///     Gets a 256 bit (32 byte) key derived from the specified <paramref name="password"/> using PBKDF2/RFC 2898
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        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, 32);
            }
        }
    }
}