/// <summary>
 /// Create Sha512 hash of data
 /// </summary>
 /// <param name="data">Data to hash</param>
 /// <param name="key">Aes key</param>
 /// <returns>Hash</returns>
 public static byte[] HmacSha(byte[] data, byte[] key)
 {
     using (var sha512 = HMACSHA512.Create("HMACSHA512"))
     {
         sha512.Key = key;
         return(sha512.ComputeHash(data));
     }
 }
Пример #2
0
 private void ComputeHash(string path)
 {
     using (var fileStream = File.OpenRead(path))
     {
         using (var hasher = HMACSHA512.Create())
         {
             _result = hasher.ComputeHash(fileStream);
         }
     }
 }
        public static byte[] ComputeHash(byte[] input, byte[] key, string algorithm)
        {
            if (input == null || input.Length == 0)
            {
                throw new ArgumentException();
            }
            if (key == null || key.Length == 0)
            {
                throw new ArgumentException();
            }

            System.Security.Cryptography.KeyedHashAlgorithm hash;
            switch (algorithm.ToUpperInvariant())
            {
            case "MD5":
            case "HMACMD5":
                hash = HMACMD5.Create();
                break;

            case "MD160":
            case "RIPEMD160":
            case "HMACRIPEMD160":
                hash = HMACRIPEMD160.Create();
                break;

            case "SHA":
            case "SHA1":
            case "HMACSHA":
            case "HMACSHA1":
                hash = HMACSHA1.Create();
                break;

            case "SHA256":
            case "HMACSHA256":
                hash = HMACSHA256.Create();
                break;

            case "SHA384":
            case "HMACSHA384":
                hash = HMACSHA384.Create();
                break;

            case "SHA512":
            case "HMACSHA512":
                hash = HMACSHA512.Create();
                break;

            default:
                throw new NotSupportedException();
            }
            hash.Key = key;
            byte[] result = hash.ComputeHash(input);
            hash.Clear();
            return(result);
        }
Пример #4
0
        // public static string GetHash512(string password)
        public static string GetHash512(string password, string salt)
        {
            // byte[] passwordByte = ASCIIEncoding.ASCII.GetBytes(password);
            byte[] passwordByte = ASCIIEncoding.ASCII.GetBytes(password + salt + "SM");

            //HashAlgorithm algorithm = new HMACSHA512();
            HashAlgorithm algorithm = HMACSHA512.Create();
            Byte[] hashPassword = algorithm.ComputeHash(passwordByte);
            return Convert.ToBase64String(hashPassword);

        }
Пример #5
0
        /// <summary>
        /// HMAC加密,用于密码非可逆加密
        /// </summary>
        /// <param name="str">字符串参数</param>
        /// <returns>返回字符串值</returns>
        public static byte[] HMAC(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return(null);
            }

            HMAC hmac = HMACSHA512.Create();

            hmac.Key = Encoding.Default.GetBytes(key);
            return(hmac.ComputeHash(Encoding.Default.GetBytes(str)));
        }
Пример #6
0
 private static HashAlgorithm getHasher(string key)
 {
     if (key == null)
     {
         return(HMACSHA512.Create());
     }
     else
     {
         byte[] keyBytes = Encoding.ASCII.GetBytes(key);
         return(new HMACSHA512(keyBytes));
     }
 }
Пример #7
0
        private static byte[] GetHash([CanBeNull] string input, EHashType hash)
        {
            var inputBytes = Encoding.ASCII.GetBytes(input);

            switch (hash)
            {
            case EHashType.HMAC:
                return(HMAC.Create().ComputeHash(inputBytes));

#pragma warning disable RECS0030                                   // Suggests using the class declaring a static function when calling it
            case EHashType.HMACMD5:                                // DevSkim: ignore DS126858
                return(HMACMD5.Create().ComputeHash(inputBytes));  // DevSkim: ignore DS126858

            case EHashType.HMACSHA1:                               // DevSkim: ignore DS126858
                return(HMACSHA1.Create().ComputeHash(inputBytes)); // DevSkim: ignore DS126858

            case EHashType.HMACSHA256:
                return(HMACSHA256.Create().ComputeHash(inputBytes));

            case EHashType.HMACSHA384:
                return(HMACSHA384.Create().ComputeHash(inputBytes));

            case EHashType.HMACSHA512:
                return(HMACSHA512.Create().ComputeHash(inputBytes));

#pragma warning restore RECS0030                               // Suggests using the class declaring a static function when calling it

            case EHashType.MD5:                                // DevSkim: ignore DS126858
#pragma warning disable SG0006                                 // Weak hashing function
                return(MD5.Create().ComputeHash(inputBytes));  // DevSkim: ignore DS126858

#pragma warning restore SG0006                                 // Weak hashing function

            case EHashType.SHA1:                               // DevSkim: ignore DS126858
#pragma warning disable SG0006                                 // Weak hashing function
                return(SHA1.Create().ComputeHash(inputBytes)); // DevSkim: ignore DS126858

#pragma warning restore SG0006                                 // Weak hashing function

            case EHashType.SHA256:
                return(SHA256.Create().ComputeHash(inputBytes));

            case EHashType.SHA384:
                return(SHA384.Create().ComputeHash(inputBytes));

            case EHashType.SHA512:
                return(SHA512.Create().ComputeHash(inputBytes));

            default:
                return(inputBytes);
            }
        }
Пример #8
0
 private async Task <byte[]> ComputeHashAsync(string path)
 {
     using (var fileStream = File.OpenRead(path))
     {
         using (var hasher = HMACSHA512.Create())
         {
             return(await Task.Run(() =>
             {
                 return hasher.ComputeHash(fileStream);
             }));
         }
     }
 }
Пример #9
0
        /// <summary>
        /// Returns a hashed password + salt, to be used in generating a token.
        /// </summary>
        /// <param name="password">string - user's password</param>
        /// <returns>string - hashed password</returns>
        public static string GetHashedPassword(string password)
        {
            string key = string.Join(":", new string[] { password, _salt });

            using (HMAC hmac = HMACSHA512.Create(_alg))
            {
                // Hash the key.
                hmac.Key = Encoding.UTF8.GetBytes(_salt);
                hmac.ComputeHash(Encoding.UTF8.GetBytes(key));

                return(Convert.ToBase64String(hmac.Hash));
            }
        }
Пример #10
0
        private static byte[] GetHash(string Source, HashType hash)
        {
            byte[] inputBytes = Encoding.ASCII.GetBytes(Source);

            switch (hash)
            {
            case HashType.HMAC:
                return(HMAC.Create().ComputeHash(inputBytes));

            case HashType.HMACMD5:
                return(HMACMD5.Create().ComputeHash(inputBytes));

            case HashType.HMACSHA1:
                return(HMACSHA1.Create().ComputeHash(inputBytes));

            case HashType.HMACSHA256:
                return(HMACSHA256.Create().ComputeHash(inputBytes));

            case HashType.HMACSHA384:
                return(HMACSHA384.Create().ComputeHash(inputBytes));

            case HashType.HMACSHA512:
                return(HMACSHA512.Create().ComputeHash(inputBytes));

            /*
             * case HashType.MACTripleDES:
             * return MACTripleDES.Create().ComputeHash(inputBytes);
             */
            case HashType.MD5:
                return(MD5.Create().ComputeHash(inputBytes));

            /*
             * case HashType.RIPEMD160:
             * return RIPEMD160.Create().ComputeHash(inputBytes);
             */
            case HashType.SHA1:
                return(SHA1.Create().ComputeHash(inputBytes));

            case HashType.SHA256:
                return(SHA256.Create().ComputeHash(inputBytes));

            case HashType.SHA384:
                return(SHA384.Create().ComputeHash(inputBytes));

            case HashType.SHA512:
                return(SHA512.Create().ComputeHash(inputBytes));

            default:
                return(inputBytes);
            }
        }
Пример #11
0
        private static byte[] GetHash(string input, EHashType hash)
        {
            var inputBytes = Encoding.ASCII.GetBytes(input);

            switch (hash)
            {
            case EHashType.HMAC:
                return(HMAC.Create().ComputeHash(inputBytes));

            case EHashType.HMACMD5:
                return(HMACMD5.Create().ComputeHash(inputBytes));

            case EHashType.HMACSHA1:
                return(HMACSHA1.Create().ComputeHash(inputBytes));

            case EHashType.HMACSHA256:
                return(HMACSHA256.Create().ComputeHash(inputBytes));

            case EHashType.HMACSHA384:
                return(HMACSHA384.Create().ComputeHash(inputBytes));

            case EHashType.HMACSHA512:
                return(HMACSHA512.Create().ComputeHash(inputBytes));

            case EHashType.MACTripleDES:
                return(MACTripleDES.Create().ComputeHash(inputBytes));

            case EHashType.MD5:
                return(MD5.Create().ComputeHash(inputBytes));

            case EHashType.RIPEMD160:
                return(RIPEMD160.Create().ComputeHash(inputBytes));

            case EHashType.SHA1:
                return(SHA1.Create().ComputeHash(inputBytes));

            case EHashType.SHA256:
                return(SHA256.Create().ComputeHash(inputBytes));

            case EHashType.SHA384:
                return(SHA384.Create().ComputeHash(inputBytes));

            case EHashType.SHA512:
                return(SHA512.Create().ComputeHash(inputBytes));

            default:
                return(inputBytes);
            }
        }
Пример #12
0
        /// <summary>
        /// Generates a token to be used in API calls.
        /// The token is generated by hashing a message with a key, using HMAC SHA256.
        /// The message is: username:ip:userAgent:timeStamp
        /// The key is: password:ip:salt
        /// The resulting token is then concatenated with username:timeStamp and the result base64 encoded.
        ///
        /// API calls may then be validated by:
        /// 1. Base64 decode the string, obtaining the token, username, and timeStamp.
        /// 2. Ensure the timestamp is not expired.
        /// 2. Lookup the user's password from the db (cached).
        /// 3. Hash the username:ip:userAgent:timeStamp with the key of password:salt to compute a token.
        /// 4. Compare the computed token with the one supplied and ensure they match.
        /// </summary>
        /// TODO: add custom route
        public static string GenerateToken(string username, string password, string ip, string userAgent, long ticks)
        {
            string hash      = string.Join(":", new string[] { username, ip, userAgent, ticks.ToString() });
            string hashLeft  = "";
            string hashRight = "";

            using (HMAC hmac = HMACSHA512.Create(_alg))
            {
                hmac.Key = Encoding.UTF8.GetBytes(password); // Already hashed in db !! => Encoding.UTF8.GetBytes(GetHashedPassword(password));
                hmac.ComputeHash(Encoding.UTF8.GetBytes(hash));

                hashLeft  = Convert.ToBase64String(hmac.Hash);
                hashRight = string.Join(":", new string[] { username, ticks.ToString() });
            }
            return(Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Join(":", hashLeft, hashRight))));
        }
Пример #13
0
        private static byte[] GetHash(string input, EHashType hash)
        {
            byte[] inputBytes = Encoding.ASCII.GetBytes(input);

            switch (hash)
            {
            case EHashType.HMAC:
                return(HMAC.Create().ComputeHash(inputBytes));

            case EHashType.HMACMD5:
                return(HMACMD5.Create().ComputeHash(inputBytes));

            case EHashType.HMACSHA1:
                return(HMACSHA1.Create().ComputeHash(inputBytes));

            case EHashType.HMACSHA256:
                return(HMACSHA256.Create().ComputeHash(inputBytes));

            case EHashType.HMACSHA384:
                return(HMACSHA384.Create().ComputeHash(inputBytes));

            case EHashType.HMACSHA512:
                return(HMACSHA512.Create().ComputeHash(inputBytes));

#pragma warning disable CS0618 // Type or member is obsolete
            case EHashType.MD5:
#pragma warning restore CS0618 // Type or member is obsolete
                return(MD5.Create().ComputeHash(inputBytes));

#pragma warning disable CS0618 // Type or member is obsolete
            case EHashType.SHA1:
#pragma warning restore CS0618 // Type or member is obsolete
                return(SHA1.Create().ComputeHash(inputBytes));

            case EHashType.SHA256:
                return(SHA256.Create().ComputeHash(inputBytes));

            case EHashType.SHA384:
                return(SHA384.Create().ComputeHash(inputBytes));

            case EHashType.SHA512:
                return(SHA512.Create().ComputeHash(inputBytes));

            default:
                return(inputBytes);
            }
        }
Пример #14
0
        internal static string HashFile(string filePath, HashType algo)
        {
            switch (algo)
            {
            case HashType.MD5:
                return(MakeHashString(MD5.Create().ComputeHash(new FileStream(filePath, FileMode.Open))));

            case HashType.SHA1:
                return(MakeHashString(SHA1.Create().ComputeHash(new FileStream(filePath, FileMode.Open))));

            case HashType.SHA512:
                return(MakeHashString(HMACSHA512.Create().ComputeHash(new FileStream(filePath, FileMode.Open))));

            default:
                return("");
            }
        }
Пример #15
0
        private static byte[] GetHash(string input, eHashType hash)
        {
            byte[] inputBytes = Encoding.ASCII.GetBytes(input);

            switch (hash)
            {
            case eHashType.HMAC:
                return(HMAC.Create().ComputeHash(inputBytes));

            case eHashType.HMACMD5:
                return(HMACMD5.Create().ComputeHash(inputBytes));

            case eHashType.HMACSHA1:
                return(HMACSHA1.Create().ComputeHash(inputBytes));

            case eHashType.HMACSHA256:
                return(HMACSHA256.Create().ComputeHash(inputBytes));

            case eHashType.HMACSHA384:
                return(HMACSHA384.Create().ComputeHash(inputBytes));

            case eHashType.HMACSHA512:
                return(HMACSHA512.Create().ComputeHash(inputBytes));

            case eHashType.MD5:
                return(MD5.Create().ComputeHash(inputBytes));

            case eHashType.SHA1:
                return(SHA1.Create().ComputeHash(inputBytes));

            case eHashType.SHA256:
                return(SHA256.Create().ComputeHash(inputBytes));

            case eHashType.SHA384:
                return(SHA384.Create().ComputeHash(inputBytes));

            case eHashType.SHA512:
                return(SHA512.Create().ComputeHash(inputBytes));

            default:
                return(inputBytes);
            }
        }
Пример #16
0
        /// <summary>
        /// 为指定的数据创建安全令牌
        /// </summary>
        /// <param name="key">动态密钥</param>
        /// <param name="time">令牌创建时间</param>
        /// <returns></returns>
        public string Create(string key, DateTime time)
        {
            string   masterKey = this.masterKey ?? ALConfig.DllConfigs["BIUtils"]["SecurityConfig"]["MasterKey"];
            string   inputStr  = key + "|" + masterKey + "|" + time.ToString("yyyy-MM-dd HH:mm");
            Encoding encoding  = Encoding.GetEncoding((int)this.encryEncoding);

            switch (this.encryMode)
            {
            case EncryMode.HMAC:
                HMAC hmac = HMACSHA512.Create();
                hmac.Key = encoding.GetBytes(ALEncrypt.key);
                byte[] hmacData = hmac.ComputeHash(encoding.GetBytes(inputStr));
                return(BitConverter.ToString(hmacData).Replace("-", ""));

            case EncryMode.MD5:
                MD5    md5     = new MD5CryptoServiceProvider();
                byte[] md5Data = md5.ComputeHash(encoding.GetBytes(inputStr));
                return(BitConverter.ToString(md5Data).Replace("-", ""));

            default:
                throw new ArgumentOutOfRangeException("encryMode");
            }
        }
Пример #17
0
        // ReSharper disable AccessToStaticMemberViaDerivedType
        private void setHashAlgorithm()
        {
            switch (cbAlgo.SelectedIndex)
            {
            case 0:
                switch (cbOption.SelectedIndex)
                {
                case 0: _ha = SHA256.Create();
                    break;

                case 1: _ha = SHA256Cng.Create();
                    break;

                case 2: _ha = HMACSHA256.Create();
                    break;
                }
                break;

            case 1:
                switch (cbOption.SelectedIndex)
                {
                case 0: _ha = SHA512.Create();
                    break;

                case 1: _ha = SHA512Cng.Create();
                    break;

                case 2: _ha = HMACSHA512.Create();
                    break;
                }
                break;

            case 2:
                switch (cbOption.SelectedIndex)
                {
                case 0: _ha = SHA1.Create();
                    break;

                case 1: _ha = SHA1Cng.Create();
                    break;

                case 2: _ha = HMACSHA1.Create();
                    break;
                }
                break;

            case 3:
                switch (cbOption.SelectedIndex)
                {
                case 0: _ha = MD5.Create();
                    break;

                case 1: _ha = MD5Cng.Create();
                    break;

                case 2: _ha = HMACMD5.Create();
                    break;
                }
                break;

            case 4:
                //stays null for Modbus-CRC16
                break;

            case 5:
                _ha = new Crc32();
                break;
            }
        }