Esempio n. 1
0
        public virtual byte[] HMACSignatures(string algorithm, string msg, string secret)
        {
            dynamic Hmac;

            if (string.IsNullOrWhiteSpace(secret))
            {
                secret = HmacKey;
            }

            byte[] key = ConvertBase64StringToByte(secret); // secret key MUST in base64 string(Hashed string)

            switch (algorithm)
            {
            case SHA1:
                Hmac = new HMACSHA1(key);
                break;

            case SHA256:
                Hmac = new HMACSHA256(key);
                break;

            case SHA512:
                Hmac = new HMACSHA512(key);
                break;

            default:
                throw new Exception("Invalid hashing algorithm");
            }

            byte[] result = Hmac.ComputeHash(Encoding.UTF8.GetBytes(msg));
            Hmac.Clear();
            Hmac = null;

            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// Computes the hash.
        /// </summary>
        /// <param name="version">The version.</param>
        /// <param name="header">The header.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="data">The scope bytes.</param>
        /// <param name="privacy">The privacy provider.</param>
        /// <param name="length">The length bytes.</param>
        /// <returns></returns>
        public OctetString ComputeHash(VersionCode version, ISegment header, SecurityParameters parameters, ISnmpData data, IPrivacyProvider privacy, byte[] length)
        {
            if (header == null)
            {
                throw new ArgumentNullException(nameof(header));
            }

            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            if (privacy == null)
            {
                throw new ArgumentNullException(nameof(privacy));
            }

            var key = PasswordToKey(_password, parameters.EngineId.GetRaw());

            using (var sha512 = new HMACSHA512(key))
            {
                var hash = sha512.ComputeHash(ByteTool.PackMessage(length, version, header, parameters, data).ToBytes());
#if NET471
                sha512.Clear();
#endif
                var result = new byte[DigestLength];
                Buffer.BlockCopy(hash, 0, result, 0, result.Length);
                return(new OctetString(result));
            }
        }
 private HashedTag Compute(byte[] data, IKey key)
 {
     using (HMACSHA512 hmac = new HMACSHA512(Key.Bytes))
     {
         var hash = hmac.ComputeHash(data);
         hmac.Clear();
         return(new HashedTag(hash));
     }
 }
Esempio n. 4
0
 private void CreateHashPassword(string password, out byte[] passwordSalt, out byte[] passwordHash)
 {
     using (HMACSHA512 hmac = new HMACSHA512())
     {
         passwordSalt = hmac.Key;
         passwordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(password));
         hmac.Clear();
     }
 }
Esempio n. 5
0
        public static string HMACSHA512(string sourceText, string key)
        {
            var keyByte     = Encoding.UTF8.GetBytes(key);
            var sourceBytes = Encoding.UTF8.GetBytes(sourceText);

            using (var hmacSha512 = new HMACSHA512(keyByte))
            {
                var bytes = hmacSha512.ComputeHash(sourceBytes);
                hmacSha512.Clear();
                return(BitConverter.ToString(bytes).Replace("-", "").ToLower());
            }
        }
Esempio n. 6
0
        private bool VerifyPasswordHash(string password, byte[] passwordSalt, byte[] passwordHash)
        {
            bool samePassword;

            using (HMACSHA512 hmac = new HMACSHA512(passwordSalt))
            {
                byte[] informedPassword = hmac.ComputeHash(Encoding.UTF8.GetBytes(password));
                samePassword = passwordHash.SequenceEqual(informedPassword);
                hmac.Clear();
            }

            return(samePassword);
        }
Esempio n. 7
0
        } //compute hash from arguments and return hash value as string

        private static string GetHMACSHA512Hash(string text)
        {
            //create variables for computing hash and making it a string
            UnicodeEncoding UniCode = new UnicodeEncoding();

            byte[]     HashResult;
            byte[]     msg        = UniCode.GetBytes(text);
            HMACSHA512 hashString = new HMACSHA512();
            string     Str        = "";

            //compute hash with HMACSHA512 module and format output as string
            //convert bytes in HashResult to string values
            HashResult = hashString.ComputeHash(msg);
            foreach (byte x in HashResult)
            {
                Str += String.Format("{0:x2}", x);
            }

            //clear excess resource usage
            hashString.Clear();
            return(Str);
        } //compute hash from arguments and return hash value as string