예제 #1
0
        internal static Stream Encrypt(Stream source,
                                       Headers headers, byte[] masterKey)
        {
            byte[] easKey;
            using (var buffer = new MemoryStream())
            {
                var masterSeed = headers.MasterSeed;
                buffer.Write(masterSeed, 0, masterSeed.Length);
                buffer.Write(masterKey, 0, masterKey.Length);

                easKey = BufferEx.GetHash(buffer.ToArray());
            }

            var eas = new AesManaged
            {
                KeySize = 256,
                Key     = BufferEx.Clone(easKey),
                IV      = BufferEx.Clone(headers.EncryptionIV)
            };

            Stream stream = new CryptoStream(source,
                                             eas.CreateEncryptor(),
                                             CryptoStreamMode.Write);

            stream.Write(headers.StreamStartBytes, 0,
                         headers.StreamStartBytes.Length);

            stream = new HashedBlockStream(stream, false);
            return(headers.Compression == Compressions.GZip
                ? new GZipOutputStream(stream) : stream);
        }
예제 #2
0
        public void TestRead()
        {
            using var ms  = new MemoryStream(HashStreamData);
            using var hbs = new HashedBlockStream(ms, false);
            using var br  = new BinaryReader(hbs);
            var bytes = br.ReadBytes(Data.Length);

            Assert.That(MemUtil.ArraysEqual(bytes, Data), Is.True);
            Assert.Throws <EndOfStreamException>(() => br.ReadByte());
        }
예제 #3
0
 public void TestRead()
 {
     using (var ms = new MemoryStream(hashStreamData)) {
         using (var hbs = new HashedBlockStream(ms, false)) {
             using (var br = new BinaryReader(hbs)) {
                 var bytes = br.ReadBytes(data.Length);
                 Assert.That(bytes, Is.EqualTo(data));
                 Assert.That(() => br.ReadByte(), Throws.InstanceOf <EndOfStreamException> ());
             }
         }
     }
 }
예제 #4
0
        public void TestWrite()
        {
            var buffer = new byte[HashStreamData.Length];

            using var ms = new MemoryStream(buffer);
            using (var hbs = new HashedBlockStream(ms, true))
            {
                using var bw = new BinaryWriter(hbs);
                bw.Write(Data);
            }
            Assert.That(MemUtil.ArraysEqual(buffer, HashStreamData), Is.True);
        }
예제 #5
0
        public void TestWrite()
        {
            var buffer = new byte[hashStreamData.Length];

            using (var ms = new MemoryStream(buffer)) {
                using (var hbs = new HashedBlockStream(ms, true)) {
                    using (var bw = new BinaryWriter(hbs)) {
                        bw.Write(data);
                    }
                }
                Assert.That(buffer, Is.EqualTo(hashStreamData));
            }
        }