Exemplo n.º 1
0
        /// <summary>
        /// Hashes a clear string to the given hash type
        /// </summary>
        /// <param name="clearString">
        /// Clear string to hash
        /// </param>
        /// <param name="hashAlgorithmType">
        /// hash Algorithm to be used
        /// </param>
        /// <param name="salt">
        /// Salt to be applied to hashing algorithm
        /// </param>
        /// <param name="hashHex">
        /// The hash Hex.
        /// </param>
        /// <param name="hashCaseType">
        /// The hash Case.
        /// </param>
        /// <param name="hashRemoveChars">
        /// The hash Remove Chars.
        /// </param>
        /// <param name="standardComp">
        /// The standard Comp.
        /// </param>
        /// <returns>
        /// Hashed String as Hex or Base64
        /// </returns>
        public static string Hash(
            [NotNull] string clearString,
            HashAlgorithmType hashAlgorithmType = HashAlgorithmType.SHA1,
            [CanBeNull] string salt             = null,
            bool hashHex = true,
            HashCaseType hashCaseType          = HashCaseType.Upper,
            [CanBeNull] string hashRemoveChars = null,
            bool standardComp = true)
        {
            CodeContracts.VerifyNotNull(clearString, "clearString");

            byte[] buffer;

            if (salt.IsSet())
            {
                buffer = GeneratePasswordBuffer(salt, clearString, standardComp);
            }
            else
            {
                var unencodedBytes = Encoding.UTF8.GetBytes(clearString); // UTF8 used to maintain compatibility
                buffer = new byte[unencodedBytes.Length];
                Buffer.BlockCopy(unencodedBytes, 0, buffer, 0, unencodedBytes.Length);
            }

            var hashedBytes = Hash(buffer, hashAlgorithmType); // Hash

            var hashedString = hashHex ? hashedBytes.ToHexString() : Convert.ToBase64String(hashedBytes);

            // Adjust the case of the hash output
            switch (hashCaseType)
            {
            case HashCaseType.Upper:
                hashedString = hashedString.ToUpper();
                break;

            case HashCaseType.Lower:
                hashedString = hashedString.ToLower();
                break;
            }

            if (hashRemoveChars.IsSet())
            {
                hashedString = hashRemoveChars.Aggregate(
                    hashedString,
                    (current, removeChar) => current.Replace(removeChar.ToString(), string.Empty));
            }

            return(hashedString);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Hashes a clear string to the given hashtype
        /// </summary>
        /// <param name="clearString">
        /// Clear string to hash
        /// </param>
        /// <param name="hashAlgorithmType">
        /// hash Algorithm to be used
        /// </param>
        /// <param name="salt">
        /// Salt to be applied to hashing algorithm
        /// </param>
        /// <param name="useSalt">
        /// Should salt be applied to hashing algorithm
        /// </param>
        /// <param name="hashHex">
        /// The hash Hex.
        /// </param>
        /// <param name="hashCaseType">
        /// The hash Case.
        /// </param>
        /// <param name="hashRemoveChars">
        /// The hash Remove Chars.
        /// </param>
        /// <param name="standardComp">
        /// The standard Comp.
        /// </param>
        /// <returns>
        /// Hashed String as Hex or Base64 
        /// </returns>
        public static string Hash(
      [NotNull] string clearString, 
      HashAlgorithmType hashAlgorithmType = HashAlgorithmType.SHA1, 
      [CanBeNull] string salt = null, 
      bool hashHex = true, 
      HashCaseType hashCaseType = HashCaseType.Upper, 
      [CanBeNull] string hashRemoveChars = null, 
      bool standardComp = true)
        {
            CodeContracts.ArgumentNotNull(clearString, "clearString");

              byte[] buffer;

              if (salt.IsSet())
              {
            buffer = GeneratePasswordBuffer(salt, clearString, standardComp);
              }
              else
              {
            byte[] unencodedBytes = Encoding.UTF8.GetBytes(clearString); // UTF8 used to maintain compatibility
            buffer = new byte[unencodedBytes.Length];
            Buffer.BlockCopy(unencodedBytes, 0, buffer, 0, unencodedBytes.Length);
              }

              byte[] hashedBytes = Hash(buffer, hashAlgorithmType); // Hash

              string hashedString = hashHex ? hashedBytes.ToHexString() : Convert.ToBase64String(hashedBytes);

              // Adjust the case of the hash output
              switch (hashCaseType)
              {
            case HashCaseType.Upper:
              hashedString = hashedString.ToUpper();
              break;
            case HashCaseType.Lower:
              hashedString = hashedString.ToLower();
              break;
              }

              if (hashRemoveChars.IsSet())
              {
            hashedString = hashRemoveChars.Aggregate(
              hashedString, (current, removeChar) => current.Replace(removeChar.ToString(), string.Empty));
              }

              return hashedString;
        }