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