-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathArgon2.cs
More file actions
57 lines (42 loc) · 1.58 KB
/
Copy pathArgon2.cs
File metadata and controls
57 lines (42 loc) · 1.58 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Text;
using System.Runtime.InteropServices;
using Isopoh.Cryptography.Argon2;
using Isopoh.Cryptography.SecureArray;
namespace A2
{
[ProgId("ClassicASP.Argon2")]
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("048DFFB1-A0FE-4E8F-802A-A1F5820D068A")]
[ComVisible(true)]
public class A2
{
[ComVisible(true)]
public string Hash(string password, int timeCost = 4, int memoryCost = 2048, int lanes = 4, int threads = 0, int saltBytes = 16)
{
if (threads <= 0) threads = Environment.ProcessorCount;
if (saltBytes < 8) saltBytes = 8;
byte[] salt = new byte[saltBytes];
var Rng = System.Security.Cryptography.RandomNumberGenerator.Create();
Rng.GetBytes(salt);
var config = new Argon2Config
{
Type = Argon2Type.DataIndependentAddressing,
Version = Argon2Version.Nineteen,
Password = Encoding.UTF8.GetBytes(password),
Salt = salt,
TimeCost = timeCost,
MemoryCost = memoryCost,
Lanes = lanes,
Threads = threads
};
var argon2 = new Argon2(config);
SecureArray<byte> hash = argon2.Hash();
return config.EncodeString(hash.Buffer);
}
public Boolean Verify(string password, string passwordHash)
{
return Argon2.Verify(passwordHash, Encoding.UTF8.GetBytes(password));
}
}
}