-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
43 lines (32 loc) · 1.24 KB
/
Program.cs
File metadata and controls
43 lines (32 loc) · 1.24 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System.Security.Cryptography;
using System.Text;
const int keySize = 64;
const int iterations = 350000;
HashAlgorithmName hashAlgorithm = HashAlgorithmName.SHA512;
if (Environment.GetCommandLineArgs().Length != 2)
{
Console.WriteLine("Usage: HashingAndSaltingPasswords <password>\nYou must specify the password for encryption.\n");
return;
}
var password = Environment.GetCommandLineArgs()[1];
var hash = HashPasword(password, out var salt);
Console.WriteLine($"Password hash: {hash}");
Console.WriteLine($"Generated salt: {Convert.ToHexString(salt)}");
var verificationResult = VerifyPassword(password, hash, salt);
Console.WriteLine($"Password verification: {verificationResult}");
string HashPasword(string password, out byte[] salt)
{
salt = RandomNumberGenerator.GetBytes(keySize);
var hash = Rfc2898DeriveBytes.Pbkdf2(
Encoding.UTF8.GetBytes(password),
salt,
iterations,
hashAlgorithm,
keySize);
return Convert.ToHexString(hash);
}
bool VerifyPassword(string password, string hash, byte[] salt)
{
var hashToCompare = Rfc2898DeriveBytes.Pbkdf2(password, salt, iterations, hashAlgorithm, keySize);
return hashToCompare.SequenceEqual(Convert.FromHexString(hash));
}