/// <summary> /// Computes the hash of a given InputStream. This is a wrapper over the HashAlgorithm crypto functions. /// </summary> /// <param name="stream">the source stream. Use a MemoryStream if uncertain.</param> /// <param name="hashType">the Algorithm to use to compute the hash</param> /// <returns>a byte[] representation of the hash. If the Stream is a null object /// then null will be returned. If the Stream is empty an empty byte[] {} will be returned.</returns> public static byte[] ComputeHash(this Stream stream, ChecksumAlgorithm hashType = ChecksumAlgorithm.SHA256) { if (stream == null) return null; using (var algorithm = HashAlgorithm.Create(hashType.ToString())) return algorithm.ComputeHash(stream); }
/// <summary> /// Computes the hash of a given InputStream. This is a wrapper over the HashAlgorithm crypto functions. /// </summary> /// <param name="stream">the source stream. Use a MemoryStream if uncertain.</param> /// <param name="hashType">the Algorithm to use to compute the hash</param> /// <returns>a byte[] representation of the hash. If the Stream is a null object /// then null will be returned. If the Stream is empty an empty byte[] {} will be returned.</returns> public static byte[] ComputeHash(this Stream stream, ChecksumAlgorithm hashType = ChecksumAlgorithm.SHA256, long? maxBodySize = null) { if (stream == null) return null; using (var algorithm = HashAlgorithm.Create(hashType.ToString())) if (maxBodySize != null && maxBodySize > 0) return algorithm.ComputeHash(stream.ReadExactly((long) maxBodySize)); else return algorithm.ComputeHash(stream); }
/// <summary> /// Computes the hash of a given InputStream. This is a wrapper over the HashAlgorithm crypto functions. /// </summary> /// <param name="stream">the source stream. Use a MemoryStream if uncertain.</param> /// <param name="hashType">the Algorithm to use to compute the hash</param> /// <returns>a byte[] representation of the hash. If the Stream is a null object /// then null will be returned. If the Stream is empty an empty byte[] {} will be returned.</returns> public static byte[] ComputeHash(this Stream stream, ChecksumAlgorithm hashType = ChecksumAlgorithm.SHA256) { if (stream == null) { return(null); } using (var algorithm = HashAlgorithm.Create(hashType.ToString())) return(algorithm.ComputeHash(stream)); }
/// <summary> /// Computes the hash of a given InputStream. This is a wrapper over the HashAlgorithm crypto functions. /// </summary> /// <param name="stream">the source stream. Use a MemoryStream if uncertain.</param> /// <param name="hashType">the Algorithm to use to compute the hash</param> /// <returns>a byte[] representation of the hash. If the Stream is a null object /// then null will be returned. If the Stream is empty an empty byte[] {} will be returned.</returns> public static byte[] ComputeHash(this Stream stream, ChecksumAlgorithm hashType = ChecksumAlgorithm.SHA256, long?maxBodySize = null) { if (stream == null) { return(null); } using (var algorithm = HashAlgorithm.Create(hashType.ToString())) if (maxBodySize != null && maxBodySize > 0) { return(algorithm.ComputeHash(stream.ReadExactly((long)maxBodySize))); } else { return(algorithm.ComputeHash(stream)); } }