示例#1
0
        public static string Encode(string publicKey, int choice = 2)
        {
            byte[] hashMessage = null;
            byte[] messageBytes = m_encoding.GetBytes(publicKey);

            switch (choice%6)
            {
                case 0:
                    var hmacmd5 = new HMACMD5(m_keyBytes);
                    hashMessage = hmacmd5.ComputeHash(messageBytes);
                    break;
                case 1:
                    var hmacripedmd160 = new HMACRIPEMD160(m_keyBytes);
                    hashMessage = hmacripedmd160.ComputeHash(messageBytes);
                    break;
                case 2:
                    var hmacsha1 = new HMACSHA1(m_keyBytes);
                    hashMessage = hmacsha1.ComputeHash(messageBytes);
                    break;
                case 3:
                    var hmacsha256 = new HMACSHA256(m_keyBytes);
                    hashMessage = hmacsha256.ComputeHash(messageBytes);
                    break;
                case 4:
                    var hmacsha384 = new HMACSHA384(m_keyBytes);
                    hashMessage = hmacsha384.ComputeHash(messageBytes);
                    break;
                case 5:
                    var hmacsha512 = new HMACSHA512(m_keyBytes);
                    hashMessage = hmacsha512.ComputeHash(messageBytes);
                    break;
            }

            return Convert.ToBase64String(hashMessage);
        }
示例#2
0
 /// <summary>
 /// Computes the HMAC-RIPEMD-160 for the key and bytes.
 /// </summary>
 public static Byte[] ComputeHMAC(Byte[] key, Byte[] bytes)
 {
     HMACRIPEMD160 hmac = new HMACRIPEMD160(key);
     return hmac.ComputeHash(bytes);
 }
		public void HMACRIPEMD160_c (string testName, HMACRIPEMD160 hmac, byte[] input, byte[] result) 
		{
			MemoryStream ms = new MemoryStream (input);
			byte[] output = hmac.ComputeHash (ms); 
			Assert.AreEqual (result, output, testName + ".c.1");
			Assert.AreEqual (result, hmac.Hash, testName + ".c.2");
			// required or next operation will still return old hash
			hmac.Initialize ();
		}
		public void HMACRIPEMD160_b (string testName, HMACRIPEMD160 hmac, byte[] input, byte[] result) 
		{
			byte[] output = hmac.ComputeHash (input, 0, input.Length); 
			Assert.AreEqual (result, output, testName + ".b.1");
			Assert.AreEqual (result, hmac.Hash, testName + ".b.2");
			// required or next operation will still return old hash
			hmac.Initialize ();
		}
		public void HMACRIPEMD160_a (string testName, HMACRIPEMD160 hmac, byte[] input, byte[] result) 
		{
			byte[] output = hmac.ComputeHash (input); 
			AssertEquals (testName + ".a.1", result, output);
			AssertEquals (testName + ".a.2", result, hmac.Hash);
			// required or next operation will still return old hash
			hmac.Initialize ();
		}
示例#6
0
        /// <summary>
        /// Computes a keyed hash for a source file based on key provided and the HMAC algorithm selected by the user
        /// Defaults to HMACSHA1
        /// </summary>
        /// <param name="file">file to get the HMAC of</param>
        /// <param name="hmacAlgo">HMAC algorithm to use</param>
        /// <param name="hmacKey">Key supplied by the user</param>
        public string CalculateHMAC(string file, string hmacAlgo, byte[] hmacKey)
        {
            string resultHmac = "";
            byte[] hashOfInputFile;
            try
            {
                switch (hmacAlgo)
                {
                    case "HMACMD5":
                        using (HMACMD5 hmacSha1 = new HMACMD5(hmacKey))
                        {
                            using (FileStream objFS = new FileStream(file, FileMode.Open))
                            {
                                // Computing the hash of the input file
                                hashOfInputFile = hmacSha1.ComputeHash(objFS);
                            }
                        }
                        break;
                    case "HMACSHA256":
                        using (HMACSHA256 hmacSha1 = new HMACSHA256(hmacKey))
                        {
                            using (FileStream objFS = new FileStream(file, FileMode.Open))
                            {
                                // Computing the hash of the input file
                                hashOfInputFile = hmacSha1.ComputeHash(objFS);
                            }
                        }
                        break;
                    case "HMACSHA384":
                        using (HMACSHA384 hmacSha1 = new HMACSHA384(hmacKey))
                        {
                            using (FileStream objFS = new FileStream(file, FileMode.Open))
                            {
                                // Computing the hash of the input file
                                hashOfInputFile = hmacSha1.ComputeHash(objFS);
                            }
                        }
                        break;
                    case "HMACSHA512":
                        using (HMACSHA512 hmacSha1 = new HMACSHA512(hmacKey))
                        {
                            using (FileStream objFS = new FileStream(file, FileMode.Open))
                            {
                                // Computing the hash of the input file
                                hashOfInputFile = hmacSha1.ComputeHash(objFS);
                            }
                        }
                        break;
                    case "HMACRIPEMD160":
                        using (HMACRIPEMD160 hmacSha1 = new HMACRIPEMD160(hmacKey))
                        {
                            using (FileStream objFS = new FileStream(file, FileMode.Open))
                            {
                                // Computing the hash of the input file
                                hashOfInputFile = hmacSha1.ComputeHash(objFS);
                            }
                        }
                        break;
                    default:// "HMACSHA1":
                        using (HMACSHA1 hmacSha1 = new HMACSHA1(hmacKey))
                        {
                            using(FileStream objFS = new FileStream(file, FileMode.Open))
                            {
                                // Computing the hash of the input file
                                hashOfInputFile = hmacSha1.ComputeHash(objFS);
                            }
                        }
                        break;
                }
                resultHmac = BitConverter.ToString(hashOfInputFile).Replace("-", String.Empty).ToLower();
                return resultHmac;
             }
            catch
            {
                resultHmac = HMAC_ERROR;
                return resultHmac;
            }
            finally
            {

            }
        }