private static Stream AddVersion(Stream stream, int version) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } var versionStream = new BufferStream(_bufferManager); VintUtils.SetUInt64(versionStream, (uint)version); return(new UniteStream(versionStream, stream)); }
private static Stream AddPadding(Stream stream, int size) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } try { var lengthStream = new BufferStream(_bufferManager); VintUtils.SetUInt64(lengthStream, (ulong)stream.Length); Stream paddingStream; { using (var random = RandomNumberGenerator.Create()) { int paddingLength = size - (int)(lengthStream.Length + stream.Length); paddingStream = new BufferStream(_bufferManager); using (var safeBuffer = _bufferManager.CreateSafeBuffer(1024 * 4)) { while (paddingLength > 0) { int writeSize = Math.Min(paddingLength, safeBuffer.Value.Length); random.GetBytes(safeBuffer.Value); paddingStream.Write(safeBuffer.Value, 0, writeSize); paddingLength -= writeSize; } } } } return(new UniteStream(lengthStream, stream, paddingStream)); } catch (Exception e) { if (stream != null) { stream.Dispose(); } throw new ArgumentException(e.Message, e); } }
private static Stream AddHash(Stream stream) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } var hashStream = new BufferStream(_bufferManager); { VintUtils.SetUInt64(hashStream, (uint)ConvertHashAlgorithm.Sha256); var value = Sha256.ComputeHash(new WrapperStream(stream, true)); hashStream.Write(value, 0, value.Length); } return(new UniteStream(hashStream, stream)); }
private static Stream ToStream <T>(int version, ItemBase <T> item) where T : ItemBase <T> { Stream stream = null; try { stream = new RangeStream(item.Export(_bufferManager)); var dic = new Dictionary <byte, Stream>(); try { stream.Seek(0, SeekOrigin.Begin); BufferStream deflateBufferStream = null; try { deflateBufferStream = new BufferStream(_bufferManager); using (var deflateStream = new DeflateStream(deflateBufferStream, CompressionMode.Compress, true)) using (var safeBuffer = _bufferManager.CreateSafeBuffer(1024 * 4)) { int length; while ((length = stream.Read(safeBuffer.Value, 0, safeBuffer.Value.Length)) > 0) { deflateStream.Write(safeBuffer.Value, 0, length); } } deflateBufferStream.Seek(0, SeekOrigin.Begin); dic.Add((byte)ConvertCompressionAlgorithm.Deflate, deflateBufferStream); } catch (Exception) { if (deflateBufferStream != null) { deflateBufferStream.Dispose(); } } } catch (Exception) { } dic.Add((byte)ConvertCompressionAlgorithm.None, stream); var list = dic.ToList(); list.Sort((x, y) => { int c = x.Value.Length.CompareTo(y.Value.Length); if (c != 0) { return(c); } return(x.Key.CompareTo(y.Key)); }); for (int i = 1; i < list.Count; i++) { list[i].Value.Dispose(); } var headerStream = new BufferStream(_bufferManager); VintUtils.SetUInt64(headerStream, (uint)version); VintUtils.SetUInt64(headerStream, list[0].Key); var dataStream = new UniteStream(headerStream, list[0].Value); var crcStream = new MemoryStream(Crc32_Castagnoli.ComputeHash(new WrapperStream(dataStream, true))); return(new UniteStream(dataStream, crcStream)); } catch (Exception ex) { if (stream != null) { stream.Dispose(); } throw new ArgumentException(ex.Message, ex); } }
private static Stream Compress(Stream stream) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } try { var dic = new Dictionary <byte, Stream>(); try { stream.Seek(0, SeekOrigin.Begin); BufferStream deflateBufferStream = null; try { deflateBufferStream = new BufferStream(_bufferManager); using (var deflateStream = new DeflateStream(deflateBufferStream, CompressionMode.Compress, true)) using (var safeBuffer = _bufferManager.CreateSafeBuffer(1024 * 4)) { int length; while ((length = stream.Read(safeBuffer.Value, 0, safeBuffer.Value.Length)) > 0) { deflateStream.Write(safeBuffer.Value, 0, length); } } deflateBufferStream.Seek(0, SeekOrigin.Begin); dic.Add((byte)ConvertCompressionAlgorithm.Deflate, deflateBufferStream); } catch (Exception) { if (deflateBufferStream != null) { deflateBufferStream.Dispose(); } } } catch (Exception) { } dic.Add((byte)ConvertCompressionAlgorithm.None, stream); var list = dic.ToList(); list.Sort((x, y) => { int c = x.Value.Length.CompareTo(y.Value.Length); if (c != 0) { return(c); } return(x.Key.CompareTo(y.Key)); }); for (int i = 1; i < list.Count; i++) { list[i].Value.Dispose(); } var headerStream = new BufferStream(_bufferManager); VintUtils.SetUInt64(headerStream, list[0].Key); return(new UniteStream(headerStream, list[0].Value)); } catch (Exception ex) { if (stream != null) { stream.Dispose(); } throw new ArgumentException(ex.Message, ex); } }
private static Stream Encrypt(Stream stream, ExchangePublicKey publicKey) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (publicKey == null) { throw new ArgumentNullException(nameof(publicKey)); } try { BufferStream outStream = null; try { outStream = new BufferStream(_bufferManager); VintUtils.SetUInt64(outStream, (uint)ConvertCryptoAlgorithm.Aes256); var cryptoKey = new byte[32]; var iv = new byte[32]; using (var random = RandomNumberGenerator.Create()) { random.GetBytes(cryptoKey); random.GetBytes(iv); } { var encryptedBuffer = Exchange.Encrypt(publicKey, cryptoKey); VintUtils.SetUInt64(outStream, (uint)encryptedBuffer.Length); outStream.Write(encryptedBuffer, 0, encryptedBuffer.Length); } outStream.Write(iv, 0, iv.Length); using (var aes = Aes.Create()) { aes.KeySize = 256; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; using (var inStream = new WrapperStream(stream, true)) using (var cs = new CryptoStream(inStream, aes.CreateEncryptor(cryptoKey, iv), CryptoStreamMode.Read)) using (var safeBuffer = _bufferManager.CreateSafeBuffer(1024 * 4)) { int length; while ((length = cs.Read(safeBuffer.Value, 0, safeBuffer.Value.Length)) > 0) { outStream.Write(safeBuffer.Value, 0, length); } } } outStream.Seek(0, SeekOrigin.Begin); } catch (Exception) { if (outStream != null) { outStream.Dispose(); } throw; } return(outStream); } catch (Exception e) { throw new ArgumentException(e.Message, e); } finally { if (stream != null) { stream.Dispose(); } } }