-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArgon2.cs
92 lines (80 loc) · 3.1 KB
/
Argon2.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace System.Security.Cryptography;
public static class Argon2
{
public const string WindowsAssemblyName = "libargon2.dll";
// Unix adds the word lib in front as one of the resolutions
public const string UnixAssemblyName = "libargon2";
static Argon2() => NativeLibrary.SetDllImportResolver(Assembly.GetExecutingAssembly(), DllImportResolver);
public static string GetLibraryName(string libraryName) => Environment.OSVersion.Platform switch
{
PlatformID.Win32NT => WindowsAssemblyName,
_ => UnixAssemblyName,
};
public static IntPtr DllImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
var platformDependentName = GetLibraryName(libraryName);
if (NativeLibrary.TryLoad(platformDependentName, assembly, searchPath, out var handle))
{
return handle;
}
throw new BadImageFormatException("Could not load the libargon2 native library.");
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Argon2Error Hash(uint t_cost, uint m_cost, uint parallelism,
ReadOnlySpan<byte> pwd,
ReadOnlySpan<byte> salt,
Span<byte> hash,
Span<byte> encoded,
int type, int version)
{
fixed (byte* p_pwd = pwd, p_salt = salt, p_hash = hash, p_encoded = encoded)
{
return argon2_hash(
t_cost,
m_cost,
parallelism,
p_pwd,
pwd.Length,
p_salt,
salt.Length,
p_hash,
hash.Length,
p_encoded,
encoded.Length,
type,
version
);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Argon2Error Verify(ReadOnlySpan<byte> encoded, ReadOnlySpan<byte> pwd, long pwdlen, int type)
{
fixed (byte* p_pwd = pwd, p_encoded = encoded)
{
return argon2_verify(p_pwd, p_encoded, pwdlen, type);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Argon2Error Decode(Argon2Context ctx, ReadOnlySpan<byte> str, int type)
{
fixed (byte* p_str = str)
{
return decode_string(ctx, p_str, type);
}
}
[DllImport("libargon2", EntryPoint = "argon2_hash")]
internal static extern unsafe Argon2Error argon2_hash(uint t_cost, uint m_cost, uint parallelism,
byte* pwd, long pwdlen,
byte* salt, long saltlen,
byte* hash, long hashlen,
byte* encoded, long encodedlen,
int type, int version
);
[DllImport("libargon2", EntryPoint = "argon2_verify")]
internal static extern unsafe Argon2Error argon2_verify(byte* encoded, byte* pwd, long pwdlen, int type);
[DllImport("libargon2", EntryPoint = "decode_string")]
internal static extern unsafe Argon2Error decode_string(Argon2Context ctx, byte* str, int type);
}