public void HashByCryptoStream() { var provider = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha1); var hasher = provider.CreateHash(); using (var stream = new PCLCrypto.CryptoStream(Stream.Null, hasher, CryptoStreamMode.Write)) { stream.Write(this.data, 0, this.data.Length); } Assert.Equal(this.dataHash, Convert.ToBase64String(hasher.GetValueAndReset())); }
public void HashByCryptoStream() { IHashAlgorithmProvider?provider = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha1); CryptographicHash? hasher = provider.CreateHash(); using (var stream = new PCLCrypto.CryptoStream(Stream.Null, hasher, CryptoStreamMode.Write)) { stream.Write(this.data, 0, this.data.Length); } Assert.Equal(this.dataHash, Convert.ToBase64String(hasher.GetValueAndReset())); }
public void HashByCryptoStream() { var algorithm = WinRTCrypto.MacAlgorithmProvider.OpenAlgorithm(MacAlgorithm.HmacSha1); var hasher = algorithm.CreateHash(this.keyMaterial); using (var stream = new PCLCrypto.CryptoStream(Stream.Null, hasher, CryptoStreamMode.Write)) { stream.Write(this.data, 0, this.data.Length); } Assert.Equal(this.macBase64, Convert.ToBase64String(hasher.GetValueAndReset())); }
// Returned array is guaranteed to be non-null and safe to index into the first 4 // bytes using pointer arithmetic. private static byte[] Hash(byte[] value) { byte[] hash; using (var hasher = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256).CreateHash()) { using (var stream = new CryptoStream(Stream.Null, hasher, CryptoStreamMode.Write)) { stream.Write(value, 0, value.Length - SumLength); } var intermediateHash = hasher.GetValueAndReset(); hasher.Append(intermediateHash); hash = hasher.GetValueAndReset(); } if (hash.Length != Sha256Hash.Length || hash.Length < SumLength) throw new Exception($"Double-SHA256 result has improper length {hash.Length}"); return hash; }
/// <summary> /// Creates a CryptoStream chain of transforms. /// </summary> /// <param name="stream">The ultimate stream to be read from or written to.</param> /// <param name="cryptoStreamMode">Whether to prepare for read or write operations to trigger the operations.</param> /// <param name="transforms">The transforms to apply.</param> /// <returns>The start of the chain of CryptoStreams.</returns> private static CryptoStream Chain(Stream stream, CryptoStreamMode cryptoStreamMode, params ICryptoTransform[] transforms) { Requires.NotNull(stream, "stream"); Requires.NotNullOrEmpty(transforms, "transforms"); if (cryptoStreamMode == CryptoStreamMode.Write) { // Arrange the transforming streams in this fashion: // T1 -> T2 -> stream // Which means we need recursion to define: // CS1(CS2(stream)) using (IEnumerator<ICryptoTransform> transformsEnumerator = transforms.Cast<ICryptoTransform>().GetEnumerator()) { return (CryptoStream)ChainWrite(stream, transformsEnumerator); } } else { // Arrange the transforming streams in this fashion: // stream -> T1 -> T2 // Which means we need iteration to define: // CS2(CS1(stream)) foreach (var transform in transforms) { stream = new CryptoStream(stream, transform, CryptoStreamMode.Read); } return (CryptoStream)stream; } }
/// <summary> /// Symmetrically encrypts a stream. /// </summary> /// <param name="cryptoProvider">The crypto provider.</param> /// <param name="plaintext">The stream of plaintext to encrypt.</param> /// <param name="ciphertext">The stream to receive the ciphertext.</param> /// <param name="encryptionVariables">An optional key and IV to use. May be <c>null</c> to use randomly generated values.</param> /// <param name="cancellationToken">A cancellation token.</param> /// <returns> /// A task that completes when encryption has completed, whose result is the key and IV to use to decrypt the ciphertext. /// </returns> public static async Task<SymmetricEncryptionVariables> EncryptAsync(this CryptoSettings cryptoProvider, Stream plaintext, Stream ciphertext, SymmetricEncryptionVariables encryptionVariables = null, CancellationToken cancellationToken = default(CancellationToken)) { Requires.NotNull(plaintext, "plaintext"); Requires.NotNull(ciphertext, "ciphertext"); encryptionVariables = ThisOrNewEncryptionVariables(cryptoProvider, encryptionVariables); var key = CryptoSettings.SymmetricAlgorithm.CreateSymmetricKey(encryptionVariables.Key); using (var encryptor = WinRTCrypto.CryptographicEngine.CreateEncryptor(key, encryptionVariables.IV)) { var cryptoStream = new CryptoStream(ciphertext, encryptor, CryptoStreamMode.Write); await plaintext.CopyToAsync(cryptoStream, 4096, cancellationToken).ConfigureAwait(false); cryptoStream.FlushFinalBlock(); } return encryptionVariables; }