Exemple #1
0
        /// <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());
        }
Exemple #2
0
        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());
        }