Skip to content

Commit 6642e99

Browse files
committed
feat(crypto): ✨ add PwHash struct and related APIs
1 parent fdd3ccb commit 6642e99

File tree

7 files changed

+112
-5
lines changed

7 files changed

+112
-5
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
3+
namespace Algorand.Unity.Crypto
4+
{
5+
public partial struct PwHash
6+
{
7+
public enum HashAlgorithm
8+
{
9+
None,
10+
Default = sodium.crypto_pwhash_ALG_DEFAULT,
11+
Argon2I13 = sodium.crypto_pwhash_ALG_ARGON2I13,
12+
Argon2ID13 = sodium.crypto_pwhash_ALG_ARGON2ID13
13+
}
14+
15+
public enum HashError
16+
{
17+
None = 0,
18+
OutOfMemory = -1
19+
}
20+
21+
/// <summary>
22+
/// Hash a password with the given salt. If there is an error, the out handle will not be allocated.
23+
/// </summary>
24+
/// <param name="passwd"></param>
25+
/// <param name="salt"></param>
26+
/// <param name="outlen"></param>
27+
/// <param name="out"></param>
28+
/// <param name="alg"></param>
29+
/// <param name="opsLimit"></param>
30+
/// <param name="memLimit"></param>
31+
/// <returns></returns>
32+
public static HashError Hash(
33+
SodiumString passwd,
34+
Salt salt,
35+
uint outlen,
36+
out SecureMemoryHandle @out,
37+
HashAlgorithm alg = HashAlgorithm.Default,
38+
OpsLimit opsLimit = OpsLimit.Interactive,
39+
MemLimit memLimit = MemLimit.Interactive
40+
)
41+
{
42+
HashError err;
43+
@out = SecureMemoryHandle.Create((UIntPtr)outlen);
44+
unsafe
45+
{
46+
err = (HashError)sodium.crypto_pwhash(
47+
(byte*)@out.Ptr,
48+
outlen,
49+
passwd.GetUnsafePtr(),
50+
(ulong)passwd.Length,
51+
salt.bytes,
52+
(ulong)opsLimit,
53+
(UIntPtr)memLimit,
54+
(int)alg
55+
);
56+
}
57+
if (err != HashError.None)
58+
{
59+
@out.Dispose();
60+
@out = default;
61+
}
62+
return err;
63+
}
64+
}
65+
}

Runtime/Algorand.Unity.Crypto/PwHash/PwHash+Hash.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace Algorand.Unity.Crypto
5+
{
6+
public partial struct PwHash
7+
{
8+
[Serializable]
9+
[StructLayout(LayoutKind.Explicit, Size = SizeBytes)]
10+
public struct Salt
11+
{
12+
public const int SizeBytes = sodium.crypto_pwhash_SALTBYTES;
13+
[FieldOffset(0), NonSerialized] public unsafe fixed byte bytes[SizeBytes];
14+
[FieldOffset(0)] public unsafe fixed ulong ulongs[SizeBytes / sizeof(ulong)];
15+
}
16+
}
17+
}

Runtime/Algorand.Unity.Crypto/PwHash/PwHash+Salt.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Algorand.Unity.Crypto/PwHash/PwHash+Store.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public enum PasswordStorageError
1313

1414
public partial struct PwHash
1515
{
16-
public PasswordStorageError HashStore(SecureString password)
16+
public PasswordStorageError HashStore(SodiumString password)
1717
{
1818
unsafe
1919
{
@@ -28,7 +28,7 @@ public PasswordStorageError HashStore(SecureString password)
2828
}
2929

3030
public static JobHandle HashStore(
31-
SecureString password,
31+
SodiumString password,
3232
NativeReference<PwHash> hash,
3333
JobHandle inputDeps = default
3434
)
@@ -44,7 +44,7 @@ public static JobHandle HashStore(
4444
[BurstCompile]
4545
public struct HashStoreJob : IJob
4646
{
47-
public SecureString Password;
47+
public SodiumString Password;
4848
public NativeReference<PwHash> Hash;
4949

5050
public void Execute()

Runtime/Algorand.Unity.Crypto/PwHash/PwHash+Verify.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public enum PasswordVerificationError
88

99
public partial struct PwHash
1010
{
11-
public PasswordVerificationError Verify(SecureString password)
11+
public PasswordVerificationError Verify(SodiumString password)
1212
{
1313
unsafe
1414
{

Runtime/Algorand.Unity.Crypto/PwHash/PwHash.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ namespace Algorand.Unity.Crypto
99
[StructLayout(LayoutKind.Sequential, Size = SizeBytes)]
1010
public partial struct PwHash
1111
{
12+
public const uint MinLength = sodium.crypto_pwhash_PASSWD_MIN;
13+
public const uint MaxLength = sodium.crypto_pwhash_PASSWD_MAX;
14+
1215
public const int SizeBytes = sizeof(ulong)
1316
+ sizeof(ulong)
1417
+ sodium.crypto_pwhash_STRBYTES;
@@ -67,7 +70,7 @@ public byte this[int index]
6770
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6871
public unsafe byte* GetUnsafePtr()
6972
{
70-
return (byte*) UnsafeUtility.AddressOf(ref bytes[0]);
73+
return (byte*)UnsafeUtility.AddressOf(ref bytes[0]);
7174
}
7275

7376
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]

0 commit comments

Comments
 (0)