generated from CareBoo/UPMTemplate-2020
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(crypto): ✨ add
SecretBox
related APIs for encrypt and decrypt
- Loading branch information
1 parent
6642e99
commit a6f7ea0
Showing
8 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Algorand.Unity.Crypto | ||
{ | ||
public static partial class SecretBox | ||
{ | ||
[StructLayout(LayoutKind.Explicit, Size = SizeBytes)] | ||
public unsafe struct Key | ||
{ | ||
public const int SizeBytes = sodium.crypto_secretbox_KEYBYTES; | ||
|
||
[FieldOffset(0)] public fixed byte bytes[SizeBytes]; | ||
|
||
public static implicit operator Span<byte>(Key key) | ||
{ | ||
return new Span<byte>(key.bytes, SizeBytes); | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Key.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Algorand.Unity.Crypto | ||
{ | ||
public static partial class SecretBox | ||
{ | ||
[StructLayout(LayoutKind.Explicit, Size = SizeBytes)] | ||
public unsafe struct Mac | ||
{ | ||
public const int SizeBytes = sodium.crypto_secretbox_MACBYTES; | ||
|
||
[FieldOffset(0)] public fixed byte bytes[SizeBytes]; | ||
|
||
public static implicit operator Span<byte>(Mac mac) | ||
{ | ||
return new Span<byte>(mac.bytes, SizeBytes); | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Mac.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Nonce.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Algorand.Unity.Crypto | ||
{ | ||
public static partial class SecretBox | ||
{ | ||
[StructLayout(LayoutKind.Explicit, Size = SizeBytes)] | ||
public unsafe struct Nonce | ||
{ | ||
public const int SizeBytes = sodium.crypto_secretbox_NONCEBYTES; | ||
|
||
[FieldOffset(0)] public fixed byte bytes[SizeBytes]; | ||
|
||
public static implicit operator Span<byte>(Nonce nonce) | ||
{ | ||
return new Span<byte>(nonce.bytes, SizeBytes); | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Nonce.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
|
||
namespace Algorand.Unity.Crypto | ||
{ | ||
public static partial class SecretBox | ||
{ | ||
public enum EncryptError | ||
{ | ||
Error = -1, | ||
None | ||
} | ||
|
||
public enum DecryptError | ||
{ | ||
Error = -1, | ||
None | ||
} | ||
|
||
public static int CipherLength(int messageLength) | ||
{ | ||
return messageLength + Mac.SizeBytes; | ||
} | ||
|
||
public static int MessageLength(int cipherLength) | ||
{ | ||
return cipherLength - Mac.SizeBytes; | ||
} | ||
|
||
public static unsafe EncryptError Encrypt( | ||
Span<byte> cipher, | ||
ReadOnlySpan<byte> message, | ||
Key* key, | ||
out Nonce nonce | ||
) | ||
{ | ||
nonce = Random.Bytes<Nonce>(); | ||
fixed (byte* noncePtr = nonce.bytes) | ||
fixed (byte* cipherPtr = cipher) | ||
fixed (byte* messagePtr = message) | ||
{ | ||
return (EncryptError)sodium.crypto_secretbox_easy( | ||
cipherPtr, | ||
messagePtr, | ||
(ulong)message.Length, | ||
noncePtr, | ||
key | ||
); | ||
} | ||
} | ||
|
||
public static unsafe DecryptError Decrypt( | ||
Span<byte> message, | ||
ReadOnlySpan<byte> cipher, | ||
Key* key, | ||
Nonce nonce | ||
) | ||
{ | ||
fixed (byte* messagePtr = message) | ||
fixed (byte* cipherPtr = cipher) | ||
{ | ||
return (DecryptError)sodium.crypto_secretbox_open_easy( | ||
messagePtr, | ||
cipherPtr, | ||
(ulong)cipher.Length, | ||
nonce.bytes, | ||
key | ||
); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.