/// <summary> /// 将Input转成UTF-8字节数组后计算Hash /// </summary> /// <param name="format">可选Hash算法</param> /// <param name="input">要计算Hash的字符串输入</param> /// <param name="salt">要计算Hash的加密key</param> /// <param name="saltToBytesType">加密key是何种类型的字符串</param> /// <returns>Hex表示的计算结果</returns> public static string Encrypt(HashCryptoType format, string input, string salt, StrToBytesType saltToBytesType = StrToBytesType.Hex) { if (string.IsNullOrEmpty(input)) { throw new ArgumentException("The input can not be none."); } if (format == HashCryptoType.None) { return(input); } var inputBytes = Encoding.UTF8.GetBytes(input); byte[] saltBytes = null; if (!string.IsNullOrEmpty(salt)) { switch (saltToBytesType) { case StrToBytesType.UTF8: saltBytes = Encoding.UTF8.GetBytes(salt); break; case StrToBytesType.Hex: saltBytes = salt.HexToBinary(); break; case StrToBytesType.Base64: saltBytes = Convert.FromBase64String(salt); break; default: throw new ArgumentException("Can't resolve saltToBytesType.", "saltToBytesType"); } } var inArray = HashEncrypt(format, inputBytes, saltBytes); return(inArray.BinaryToHex()); }
private static byte[] HashEncrypt(HashCryptoType format, byte[] input, byte[] salt) { byte[] inArray = null; var hashAlgorithm = HashAlgorithm.Create(format.ToString()); if (hashAlgorithm is KeyedHashAlgorithm) { var algorithm = (KeyedHashAlgorithm)hashAlgorithm; if (salt != null && salt.Length > 0) { algorithm.Key = salt; } inArray = algorithm.ComputeHash(input); } else { byte[] buffer; if (salt != null && salt.Length > 0) { buffer = new byte[input.Length + salt.Length]; Buffer.BlockCopy(input, 0, buffer, 0, input.Length); Buffer.BlockCopy(salt, 0, buffer, input.Length, salt.Length); } else { buffer = input; } inArray = hashAlgorithm.ComputeHash(buffer); } return(inArray); }
private static byte[] HashEncrypt(HashCryptoType format, byte[] input, byte[] salt) { byte[] buffer2; HashAlgorithm algorithm = HashAlgorithm.Create(format.ToString()); if (algorithm is KeyedHashAlgorithm) { KeyedHashAlgorithm algorithm2 = (KeyedHashAlgorithm)algorithm; if ((salt != null) && (salt.Length > 0)) { algorithm2.Key = salt; } return(algorithm2.ComputeHash(input)); } if ((salt != null) && (salt.Length > 0)) { buffer2 = new byte[input.Length + salt.Length]; Buffer.BlockCopy(input, 0, buffer2, 0, input.Length); Buffer.BlockCopy(salt, 0, buffer2, input.Length, salt.Length); } else { buffer2 = input; } return(algorithm.ComputeHash(buffer2)); }
public static string Encrypt(HashCryptoType format, string input, string salt, StrToBytesType saltToBytesType = StrToBytesType.Hex) { if (string.IsNullOrEmpty(input)) { throw new ArgumentException("The input can not be none."); } if (format == HashCryptoType.None) { return(input); } byte[] bytes = Encoding.UTF8.GetBytes(input); byte[] buffer2 = null; if (!string.IsNullOrEmpty(salt)) { switch (saltToBytesType) { case StrToBytesType.UTF8: buffer2 = Encoding.UTF8.GetBytes(salt); goto Label_0074; case StrToBytesType.Hex: buffer2 = salt.HexToBinary(); goto Label_0074; case StrToBytesType.Base64: buffer2 = Convert.FromBase64String(salt); goto Label_0074; } throw new ArgumentException("Can't resolve saltToBytesType.", "saltToBytesType"); } Label_0074: return(HashEncrypt(format, bytes, buffer2).BinaryToHex()); }
public static byte[] Encrypt(HashCryptoType format, byte[] input, byte[] salt) { if ((input == null) || (input.Length == 0)) { throw new ArgumentException("The input can not be none."); } if (format == HashCryptoType.None) { return(input); } return(HashEncrypt(format, input, salt)); }
public static string Encrypt(HashCryptoType format, string input) { if (string.IsNullOrEmpty(input)) { throw new ArgumentException("The input can not be none."); } if (format == HashCryptoType.None) { return(input); } byte[] bytes = Encoding.UTF8.GetBytes(input); return(Encrypt(format, bytes, null).BinaryToHex()); }