-
Notifications
You must be signed in to change notification settings - Fork 0
/
program.cs
53 lines (46 loc) · 1.75 KB
/
program.cs
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
44
45
46
47
48
49
50
51
52
53
using System;
using System.Security.Cryptography;
using Microsoft.AspNetCore.Cryptography.KeyDerivation;
using System.Text;
namespace ConsoleApp1
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("*******************************");
Console.WriteLine(" Password Hasher ");
Console.WriteLine("*******************************");
Console.Write(Environment.NewLine);
Console.WriteLine($"Random Salt: {GenerateSalt()}");
Console.Write(Environment.NewLine);
Console.Write("Password: ");
var password = Console.ReadLine();
Console.Write("Salt: ");
var salt = Console.ReadLine();
var hashedPassword = HashPassword(password, salt);
Console.Write(Environment.NewLine);
Console.WriteLine($"Hashed Password: {hashedPassword}");
}
public static string GenerateSalt()
{
byte[] salt = new byte[128 / 8];
using (var rng = RandomNumberGenerator.Create())
{
rng.GetBytes(salt);
}
return Convert.ToBase64String(salt);
}
public static string HashPassword(string password, string salt)
{
// derive a 256-bit subkey (use HMACSHA1 with 10,000 iterations)
string hashed = Convert.ToBase64String(KeyDerivation.Pbkdf2(
password: password,
salt: Encoding.Unicode.GetBytes(salt),
prf: KeyDerivationPrf.HMACSHA1,
iterationCount: 10000,
numBytesRequested: 256 / 8));
return hashed;
}
}
}