Akamai.Utils.ExtensionMethods.ComputeHash C# (CSharp) Method

ComputeHash() public static method

Computes the hash of a given InputStream. This is a wrapper over the HashAlgorithm crypto functions.
public static ComputeHash ( this stream, ChecksumAlgorithm hashType = ChecksumAlgorithm.SHA256, long maxBodySize = null ) : byte[]
stream this the source stream. Use a MemoryStream if uncertain.
hashType ChecksumAlgorithm the Algorithm to use to compute the hash
maxBodySize long
return byte[]
        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);
        }

Usage Example

示例#1
0
 public void ComputeHashNullTest()
 {
     Assert.IsNull(ExtensionMethods.ComputeHash(null));
 }