Exemplo n.º 1
0
 /// <summary>
 /// Calculates the checksum of a file
 /// </summary>
 /// <param name="filePath">path to the file to be checked</param>
 /// <param name="hashingAlgorithm">The hashing algo to be used. Defaults to SHA256</param>
 /// <returns>Byte array representing the checksum of the file</returns>
 /// <exception cref="ArgumentException">The hashing algo supplied is not supported by the OS/Language Version</exception>
 public static byte[] GetChecksum(string filePath, HashingAlgorithm hashingAlgorithm = HashingAlgorithm.SHA256)
 {
     using (var hasher = System.Security.Cryptography.HashAlgorithm.Create(hashingAlgorithm.ToString()))
     {
         if (hasher == null)
         {
             throw new ArgumentException($"{hashingAlgorithm} not a valid hashing algorithm");
         }
         using (var stream = System.IO.File.OpenRead(filePath))
         {
             return(hasher.ComputeHash(stream));
         }
     }
 }
Exemplo n.º 2
0
        public Task <IHashingService> CreateHashingService(HashingAlgorithm algorithm)
        {
            switch (algorithm)
            {
            case HashingAlgorithm.PBKDF2_HMACSHA1_1000:
                return(Task.FromResult <IHashingService>(new Pbkdf2HashingService_HMACSHA1_1000()));

            case HashingAlgorithm.PBKDF2_HMACSHA256_1000:
                return(Task.FromResult <IHashingService>(new Pbkdf2HashingService_HMACSHA256_1000()));

            case HashingAlgorithm.PBKDF2_HMACSHA512_1000:
                return(Task.FromResult <IHashingService>(new Pbkdf2HashingService_HMACSHA512_1000()));

            default: throw new ApplicationException($"Error creating hashing service with algorithm [{algorithm.ToString()}], a hashing service with this algorithm does not exist.");
            }
        }