private System.Security.Cryptography.HashAlgorithm GetHashAlgorithm(HashAlgorithm.Algorithms algorithm) { System.Security.Cryptography.HashAlgorithm hashAlgorithm; switch (algorithm) { case HashAlgorithm.Algorithms.CRC32: hashAlgorithm = new CRC32(); break; case HashAlgorithm.Algorithms.MD5: hashAlgorithm = MD5.Create(); break; case HashAlgorithm.Algorithms.SHA1: hashAlgorithm = SHA1.Create(); break; case HashAlgorithm.Algorithms.SHA256: hashAlgorithm = SHA256.Create(); break; case HashAlgorithm.Algorithms.SHA512: hashAlgorithm = SHA512.Create(); break; default: throw new ArgumentOutOfRangeException(nameof(algorithm), algorithm, null); } return(hashAlgorithm); }
/// <summary> /// Gets checksum of filename (if it includeFilename is true) and file contents /// </summary> /// <param name="algorithm">Hash algorithm to use (this is not the same as System.Security.Cryptography.HashAlgorithm)</param> /// <param name="includeFilename">If true, includes filename when calculating hash</param> public void GetChecksum(HashAlgorithm.Algorithms algorithm, bool includeFilename = false) { var hashAlgorithm = GetHashAlgorithm(algorithm); var checksum = CalculateHash(includeFilename, hashAlgorithm); if (!string.IsNullOrEmpty(checksum)) { Checksum = checksum; } }