using System;
using System.Security.Cryptography;
using Microsoft.AspNetCore.Cryptography.KeyDerivation;
namespace GZTW.Pecklist.Util
{
    //Authentication controller
    public static class Hasher
    {

        public static string hash(string Salt, string Password)
        {

            //adapted from here:
            //https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/consumer-apis/password-hashing
            string hashed = Convert.ToBase64String(KeyDerivation.Pbkdf2(
                       password: Password,
                       salt: Base64StringToByteArray(Salt),
                       prf: KeyDerivationPrf.HMACSHA512,
                       iterationCount: 10000,
                       numBytesRequested: 512 / 8));
            return hashed;
        }


        ///////////////////////////////////
        // convert the salt to a byte array
        public static byte[] Base64StringToByteArray(string b64)
        {
            return Convert.FromBase64String(b64);
        }


        ///////////////////////////
        // Generate a random salt
        //
        public static string GenerateSalt()
        {
            // generate a 128-bit salt using a secure PRNG
            byte[] salt = new byte[128 / 8];
            using (var rng = RandomNumberGenerator.Create())
            {
                rng.GetBytes(salt);
            }
            return Convert.ToBase64String(salt);

        }

    }//eoc

}//eons