Esempio n. 1
0
        /// <summary>
        /// 获取字符串的 MD5 哈希值,默认编码为<see cref="Encoding.UTF8"/>
        /// </summary>
        /// <param name="value">待加密的值</param>
        /// <param name="bitType">MD5加密类型,默认为<see cref="MD5BitType.L32"/></param>
        /// <param name="encoding">编码类型,默认为<see cref="Encoding.UTF8"/></param>
        /// <returns></returns>
        public static string Signature(string value, MD5BitType bitType = MD5BitType.L32, Encoding encoding = null)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            switch (bitType)
            {
            case MD5BitType.L16:
                return(Encrypt16Func()(value)(encoding));

            case MD5BitType.L32:
                return(Encrypt32Func()(value)(encoding));

            case MD5BitType.L64:
                return(Encrypt64Func()(value)(encoding));

            default:
                throw new ArgumentOutOfRangeException(nameof(bitType), bitType, null);
            }
        }
        /// <summary>
        /// MD5 encrypt mehtod,default encrypt string is 32 bits.
        /// </summary>
        /// <param name="data">The string of encrypt.</param>
        /// <param name="bits">Encrypt string bits number,only 16,32,64.</param>
        /// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
        /// <returns>Encrypt string.</returns>
        public static string Signature(string data, MD5BitTypes bits = MD5BitTypes.L32, Encoding encoding = null)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            switch (bits)
            {
            case MD5BitTypes.L16:
                return(Encrypt16Func()(data)(encoding));

            case MD5BitTypes.L32:
                return(Encrypt32Func()(data)(encoding));

            case MD5BitTypes.L64:
                return(Encrypt64Func()(data)(encoding));

            default:
                throw new ArgumentOutOfRangeException(nameof(bits), bits, null);
            }
        }
        /// <summary>
        /// MD5 hashing method, default encrypt string is 32 bits.
        /// </summary>
        /// <param name="data">The string you want to hash.</param>
        /// <param name="bits">Encrypt string bits number,only 16,32,64.</param>
        /// <param name="isUpper"></param>
        /// <param name="isIncludeHyphen"></param>
        /// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
        /// <returns>Hashed string.</returns>
        public static string Signature(string data, MD5BitTypes bits = MD5BitTypes.L32, bool isUpper = true, bool isIncludeHyphen = false, Encoding encoding = null)
        {
            Checker.Data(data);

            encoding = encoding.SafeValue();

            return(bits switch
            {
                MD5BitTypes.L16 => Encrypt16Func()(data)(encoding).ToFixUpperCase(isUpper).ToFixHyphenChar(isIncludeHyphen),
                MD5BitTypes.L32 => Encrypt32Func()(data)(encoding).ToFixUpperCase(isUpper).ToFixHyphenChar(isIncludeHyphen),
                MD5BitTypes.L64 => Encrypt64Func()(data)(encoding).ToFixUpperCase(isUpper).ToFixHyphenChar(isIncludeHyphen),
                _ => throw new ArgumentOutOfRangeException(nameof(bits), bits, null)
            });