示例#1
0
        public void EncryptAndDecryptTest()
        {
            var key       = SecretStream.GenerateKey();
            var header    = SecretStream.GenerateHeader();
            var encrypter = new SecretStream(key, header, SecretStream.MODE_PUSH);
            var decrypter = new SecretStream(key, header, SecretStream.MODE_PULL);

            var message1 = "Hello, World!";
            var message2 = "{ \"json\": \"data\" }";
            var message3 = "Some more random messaging";

            var ciphertext1 = encrypter.Push(message1);

            encrypter.Rekey();
            var ciphertext2 = encrypter.Push(message2, SecretStream.TAG_PUSH);
            var ciphertext3 = encrypter.Push(message3, SecretStream.TAG_FINAL);

            int tag = -1;
            var d1  = decrypter.Pull(ciphertext1, out tag);

            Assert.AreEqual(tag, SecretStream.TAG_MESSAGE);
            decrypter.Rekey();
            var d2 = decrypter.Pull(ciphertext2, out tag);

            Assert.AreEqual(tag, SecretStream.TAG_PUSH);
            var d3 = decrypter.Pull(ciphertext3, out tag);

            Assert.AreEqual(tag, SecretStream.TAG_FINAL);

            // Verify that the original string and the decrypted string are equivalent
            Assert.AreEqual(Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(message1)), Convert.ToBase64String(d1));
            Assert.AreEqual(Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(message2)), Convert.ToBase64String(d2));
            Assert.AreEqual(Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(message3)), Convert.ToBase64String(d3));
        }
示例#2
0
 public void GenerateKeyTest()
 {
     Assert.AreEqual(32, SecretStream.GenerateKey().Length);
 }
示例#3
0
 public void GenerateHeaderTest()
 {
     Assert.AreEqual(24, SecretStream.GenerateHeader().Length);
 }
示例#4
0
        public static void RoundTripCheck(this LogEntry entry, ILogObjectTypeProvider typeProvider, byte[] secretKey)
        {
            var firstMemoryStream     = new MemoryStream();
            var firstSerializeContext = new LogSerializeContext(new BinaryWriter(firstMemoryStream), typeProvider);

            byte[] nonce;
            if (secretKey != null)
            {
                nonce = SecretStream.Nonce();
                firstSerializeContext.bw.WriteVarBuffer(nonce);
                using var ems = new MemoryStream();
                using var ebw = new BinaryWriter(ems);
                var ec = new LogSerializeContext(ebw, typeProvider, firstSerializeContext.Version);
                entry.Serialize(ec, false);
                firstSerializeContext.bw.WriteVarBuffer(SecretStream.EncryptMessage(ems.ToArray(), nonce, secretKey));
            }
            else
            {
                firstSerializeContext.bw.Write(false);
                entry.Serialize(firstSerializeContext, false);
            }

            var originalData = firstMemoryStream.ToArray();

            var br = new BinaryReader(new MemoryStream(originalData));
            var deserializeContext = new LogDeserializeContext(br, typeProvider);

            nonce = deserializeContext.br.ReadVarBuffer();

            LogEntry deserialized;

            if (nonce != null)
            {
                using var dms =
                          new MemoryStream(SecretStream.DecryptMessage(deserializeContext.br.ReadVarBuffer(), nonce,
                                                                       secretKey));
                using var dbr = new BinaryReader(dms);
                var dc = new LogDeserializeContext(dbr, typeProvider);
                deserialized = new LogEntry(dc);
            }
            else
            {
                deserialized = new LogEntry(deserializeContext);
            }

            var secondMemoryStream     = new MemoryCompareStream(originalData);
            var secondSerializeContext = new LogSerializeContext(new BinaryWriter(secondMemoryStream), typeProvider);

            if (secretKey != null)
            {
                secondSerializeContext.bw.WriteVarBuffer(nonce);
                using var ems = new MemoryStream();
                using var ebw = new BinaryWriter(ems);
                var ec = new LogSerializeContext(ebw, typeProvider, secondSerializeContext.Version);
                deserialized.Serialize(ec, false);
                secondSerializeContext.bw.WriteVarBuffer(SecretStream.EncryptMessage(ems.ToArray(), nonce, secretKey));
            }
            else
            {
                secondSerializeContext.bw.Write(false);
                deserialized.Serialize(secondSerializeContext, false);
            }
        }