示例#1
0
        /// <summary>Generates a hash for the given plain text value and returns a base64-encoded result.</summary>
        /// <param name="salt">The text to use as a salt.</param>
        /// <param name="textToHash">Plaintext value to be hashed.</param>
        /// <param name="hashAlgorithm">The algorithm to use.</param>
        public static string ComputeHash(string salt, string textToHash = null, CryptographyHash hashAlgorithm = CryptographyHash.SHA512)
        {
            HashAlgorithm hash;
              switch (hashAlgorithm)
              {
            case CryptographyHash.SHA1:
              hash = new SHA1Managed();
              break;
            case CryptographyHash.SHA256:
              hash = new SHA256Managed();
              break;
            case CryptographyHash.SHA384:
              hash = new SHA384Managed();
              break;
            case CryptographyHash.SHA512:
              hash = new SHA512Managed();
              break;
            default:
              throw new InvalidOptionException(typeof(CryptographyHash), hashAlgorithm);
              }

              byte[] hashBytes = hash.ComputeHash(SaltStringForHash(salt, textToHash));
              return Convert.ToBase64String(hashBytes);
        }
示例#2
0
 /// <summary>Compares a hash of the specified plain text value to a given hash value.</summary>
 /// <param name="textToHash">Plain text to be verified against the specified hash.</param>
 /// <param name="hashAlgorithm">The algorithm to use.</param>
 /// <param name="compareHash">Base64-encoded hash value produced by ComputeHash function.</param>
 /// <param name="salt">Salt text. This parameter can be null, in which case no salt will be used.</param>
 public static MethodStatus VerifyHash(string salt, string textToHash, CryptographyHash hashAlgorithm, string compareHash)
 {
     if (compareHash == ComputeHash(salt, textToHash, hashAlgorithm))
     return MethodStatus.Pass;
       else
     return MethodStatus.Fail;
 }
示例#3
0
 /// <summary>
 /// Gets a Hash for a file
 /// </summary>
 /// <param name="fileName">The name of the file to compute the hash for.</param>
 /// <param name="hashAlgorithm">The hash algorithm to use for this computation.</param>
 /// <returns>The hash for the file.</returns>
 public static string GetHashFromFile(string fileName, CryptographyHash hashAlgorithm = CryptographyHash.SHA512)
 {
     if (File.Exists(fileName))
     return ComputeHash(String.Empty, File.ReadAllText(fileName), hashAlgorithm);
       else
     return String.Empty;
 }