Ejemplo n.º 1
0
        private ValueTuple <byte[], byte[]> Random()
        {
            var range = RNGCryptoServiceProvider.GetInt32(128, Length);

            byte[] r1 = new byte[range];
            byte[] r2 = new byte[Length - range];
            RNGCryptoServiceProvider.Fill(r1);
            RNGCryptoServiceProvider.Fill(r2);
            return(ValueTuple.Create(r1, r2));
        }
Ejemplo n.º 2
0
        public void CryptoLockUnlock()
        {
            Span <byte> mac        = stackalloc byte[16];
            Span <byte> emptyText  = stackalloc byte[16];
            Span <byte> cipherText = stackalloc byte[16];
            Span <byte> inputText  = stackalloc byte[16];

            inputText[0] = (byte)'a';
            inputText[1] = (byte)'b';
            inputText[2] = (byte)'c';
            inputText[3] = (byte)'d';

            Span <byte> key = stackalloc byte[32];

            RNGCryptoServiceProvider.Fill(key);
            Span <byte> nonce = stackalloc byte[24];

            RNGCryptoServiceProvider.Fill(nonce);

            crypto_lock(mac, cipherText, key, nonce, inputText);

            var builder = new StringBuilder();

            for (int i = 0; i < cipherText.Length; i++)
            {
                builder.Append($"{cipherText[i]:X2}");
            }
            Console.WriteLine($"cipher: {builder}");

            Assert.False(cipherText.SequenceEqual(inputText), "crypto_lock failed. Spans are the same");
            Assert.False(cipherText.SequenceEqual(emptyText), "crypto_lock failed. cipher text is empty");

            // Verify that we get the same output from unlock
            Span <byte> outputText = stackalloc byte[16];

            crypto_unlock(outputText, key, nonce, mac, cipherText);

            Assert.True(outputText.SequenceEqual(inputText), "crypto_unlock failed. Spans are different");
        }
Ejemplo n.º 3
0
 private byte[] Random()
 {
     byte[] r = new byte[Length];
     RNGCryptoServiceProvider.Fill(r);
     return(r);
 }