Ejemplo n.º 1
0
        public static string ComputeHash(string plainText, string saltText = "", HashAlgorithms algorithm = HashAlgorithms.SHA1, Encoding encoding = null)
        {
            try
            {
                if (string.IsNullOrEmpty(plainText))
                {
                    return(plainText);
                }
                if (encoding == null)
                {
                    encoding = Encoding.UTF8;
                }
                HashAlgorithm hashProvider = null;
                switch (algorithm)
                {
                default:
                case HashAlgorithms.SHA1:
                    hashProvider = new SHA1CryptoServiceProvider(); break;

                case HashAlgorithms.SHA256:
                    hashProvider = new SHA256CryptoServiceProvider(); break;

                case HashAlgorithms.SHA384:
                    hashProvider = new SHA384CryptoServiceProvider(); break;

                case HashAlgorithms.SHA512:
                    hashProvider = new SHA512CryptoServiceProvider(); break;

                case HashAlgorithms.MD5:
                    hashProvider = new MD5CryptoServiceProvider(); break;

                case HashAlgorithms.RIPEMD160:
                    hashProvider = new RIPEMD160Managed(); break;
                }

                string hashedText = plainText + saltText;
                byte[] hashbytes  = encoding.GetBytes(hashedText);
                byte[] inputbytes = hashProvider.ComputeHash(hashbytes);
                hashProvider.Clear();
                return(CryptoHelper.GetHexaDecimal(inputbytes));
            }
            catch { return(plainText); }
        }