-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSM3.cs
63 lines (57 loc) · 1.87 KB
/
SM3.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
using System.Runtime.InteropServices;
namespace SMCrypto.NET
{
public static class SM3
{
public static string sm3_hash(byte[] msg) {
IntPtr ptr = Api.sm3_hash(msg, new IntPtr(msg.Length));
if (ptr == IntPtr.Zero)
{
throw new Exception("SM3 hash failed. Returned null pointer.");
}
try
{
string? hash = Marshal.PtrToStringAnsi(ptr) ?? throw new Exception("SM3 hash failed. Failed to convert unmanaged string to managed string.");
return hash;
}
finally
{
Api.free_char_array(ptr);
}
}
public static string sm3_hash_string(string msg_str)
{
IntPtr ptr = Api.sm3_hash_string(msg_str);
if (ptr == IntPtr.Zero)
{
throw new Exception("SM3 hash failed. Returned null pointer.");
}
try
{
string? hash = Marshal.PtrToStringAnsi(ptr) ?? throw new Exception("SM3 hash failed. Failed to convert unmanaged string to managed string.");
return hash;
}
finally
{
Api.free_char_array(ptr);
}
}
public static string sm3_hash_file(string file_path)
{
IntPtr ptr = Api.sm3_hash_file(file_path);
if (ptr == IntPtr.Zero)
{
throw new Exception("SM3 hash failed. Returned null pointer.");
}
try
{
string? hash = Marshal.PtrToStringAnsi(ptr) ?? throw new Exception("SM3 hash failed. Failed to convert unmanaged string to managed string.");
return hash;
}
finally
{
Api.free_char_array(ptr);
}
}
}
}