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); }
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()); }
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> ()); } } } }
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); }
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)); } }