using System.Text;
using System.Security.Cryptography;
using Microsoft.AspNetCore.Cryptography.KeyDerivation;

namespace CryptoProvider {

  public static class HashService {
    private static string CreateSalt() {
      byte[] randomBytes = new byte[128 / 8];
      using (var generator = RandomNumberGenerator.Create()) {
        generator.GetBytes(randomBytes);
        return Convert.ToBase64String(randomBytes);
      }
    }

    private static string CreateHash(string value, string salt) {
      var valueBytes = KeyDerivation.Pbkdf2(
        password: value,
        salt: Encoding.UTF8.GetBytes(salt),
        prf: KeyDerivationPrf.HMACSHA512,
        iterationCount: 10000,
        numBytesRequested: 256 / 8);

      return Convert.ToBase64String(valueBytes);
    }

    public static (string, string) CreateSaltedHash(string value) {
      var salt = CreateSalt();
      var hash = CreateHash(value, salt);

      return (salt, hash);
    }

    public static bool ValidateHash(string value, string salt, string hash) =>
      CreateHash(value, salt) == hash;
  }
}
