C#
Файл Program.cs
using Microsoft.AspNetCore.Cryptography.KeyDerivation;
using System;
using System.Text;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
string password = "Hello127";
var encryptedPassword = CryptographyUtils.Encrypt(password);
// encryptedPassword = gWW2Takb9wlIEkzELv1mEpsXyex3uzhDcNN4CuKfOAk=
bool same = CryptographyUtils.IsSame("Hello127", encryptedPassword);
// same = true
Console.WriteLine("Hello World!");
}
}
public static class CryptographyUtils
{
private static readonly string Secret = "This is my secret key, but it can be any string.";
public static byte[] GetSalt()
{
return Encoding.ASCII.GetBytes(Secret);
}
public static string Encrypt(string enteredPassword)
{
byte[] salt = GetSalt();
string encrypted = Convert.ToBase64String(KeyDerivation.Pbkdf2(
password: enteredPassword,
salt: salt,
prf: KeyDerivationPrf.HMACSHA1,
iterationCount: 10000,
numBytesRequested: 256 / 8
));
return encrypted;
}
public static bool IsSame(string enteredPassword, string encryptedPassword)
{
return encryptedPassword == Encrypt(enteredPassword);
}
}
}