public static byte[] HexToBinary(string hex) { const string IGNORED_CHARS = ":- "; var arr = new byte[hex.Length >> 1]; var bin = Marshal.AllocHGlobal(arr.Length); int binLength; //we call sodium_hex2bin with some chars to be ignored var ret = NativeLibsodium.sodium_hex2bin(bin, arr.Length, hex, hex.Length, IGNORED_CHARS, out binLength, null); Marshal.Copy(bin, arr, 0, binLength); Marshal.FreeHGlobal(bin); if (ret != 0) { throw new Exception("Internal error, decoding failed."); } //remove the trailing nulls from the array, if there were some format characters in the hex string before if (arr.Length != binLength) { var tmp = new byte[binLength]; Array.Copy(arr, 0, tmp, 0, binLength); return(tmp); } return(arr); }
public static byte[] DecryptChaCha20(byte[] cipherText, byte[] nonce, byte[] key) { //validate the length of the key if (key == null || key.Length != CHACHA20_KEY_BYTES) { throw new Exception(); } //throw new Exception("key", (key == null) ? 0 : key.Length, // string.Format("key must be {0} bytes in length.", CHACHA20_KEY_BYTES)); //validate the length of the nonce if (nonce == null || nonce.Length != CHACHA20_NONCEBYTES) { throw new Exception(); } //throw new Exception("nonce", (nonce == null) ? 0 : nonce.Length, // string.Format("nonce must be {0} bytes in length.", CHACHA20_NONCEBYTES)); var buffer = new byte[cipherText.Length]; var ret = NativeLibsodium.crypto_stream_chacha20_xor(buffer, cipherText, cipherText.Length, nonce, key); if (ret != 0) { throw new Exception("Error derypting message."); } return(buffer); }
public static byte[] GetRandomBytes(int count) { var buffer = new byte[count]; NativeLibsodium.randombytes_buf(buffer, count); return(buffer); }
unsafe public static byte[] EncryptChaCha20(byte[] message, byte[] nonce, byte[] key) { //validate the length of the key if (key == null || key.Length != CHACHA20_KEY_BYTES) { throw new Exception(); } //throw new Exception("key", (key == null) ? 0 : key.Length, // string.Format("key must be {0} bytes in length.", CHACHA20_KEY_BYTES)); //validate the length of the nonce if (nonce == null || nonce.Length != CHACHA20_NONCEBYTES) { throw new Exception(); } //throw new Exception("nonce", (nonce == null) ? 0 : nonce.Length, // string.Format("nonce must be {0} bytes in length.", CHACHA20_NONCEBYTES)); byte[] buffer = new byte[message.Length]; fixed(byte *bufferPtr = buffer) { int ret = NativeLibsodium.crypto_stream_chacha20_xor(bufferPtr, message, (ulong)message.Length, nonce, key); if (ret != 0) { throw new Exception("Error encrypting message."); } } return(buffer); }
unsafe public static byte[] HexToBinary(string hex) { const string IGNORED_CHARS = ":- "; byte[] arr = new byte[hex.Length >> 1]; byte[] hexBytes = Encoding.ASCII.GetBytes(hex); int binLength; fixed(byte *arrPtr = arr) { //we call sodium_hex2bin with some chars to be ignored int ret = NativeLibsodium.sodium_hex2bin( arrPtr, arr.Length, hex, hexBytes.Length, IGNORED_CHARS, out binLength, null ); if (ret != 0) { throw new Exception("Internal error, decoding failed."); } } //remove the trailing nulls from the array, if there were some format characters in the hex string before if (arr.Length != binLength) { byte[] tmp = new byte[binLength]; Array.Copy(arr, 0, tmp, 0, binLength); return(tmp); } return(arr); }
public static unsafe byte[] GetRandomBytes(int count) { byte[] buffer = new byte[count]; fixed(byte *bufferPtr = buffer) { NativeLibsodium.randombytes_buf(bufferPtr, count); } return(buffer); }
public static byte[] SignMessage(byte[] message, byte[] key) { byte[] buffer1 = new byte[message.Length + CRYPTO_SIGN_BYTES]; long bufferLength = message.Length; int ret = NativeLibsodium.crypto_sign(buffer1, ref bufferLength, message, message.Length, key); if (ret != 0) { throw new Exception("Error SignMessage failed."); } // byte[] buffer = new byte[bufferLength]; // Array.Copy( buffer1 , buffer , bufferLength ); return(buffer1); }
public static byte[] SignOpenMessage(byte[] signedMessage, byte[] key) { byte[] buffer1 = new byte[signedMessage.Length - CRYPTO_SIGN_BYTES]; long bufferLength = buffer1.Length; // Debug.Log( "bufferl ength = " + bufferLength ); int ret = NativeLibsodium.crypto_sign_open(buffer1, ref bufferLength, signedMessage, signedMessage.Length, key); // Debug.Log( "bufferl ength = " + bufferLength ); if (ret != 0) { throw new Exception("Error SignOpenMessage failed."); } // byte[] buffer = new byte[bufferLength]; // Array.Copy( buffer1 , buffer , bufferLength ); return(buffer1); }