示例#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>
        /// Creates a new HMAC instance
        /// </summary>
        /// <param name="theHash">The hash.</param>
        /// <returns>A <see cref="HMAC"/> instance</returns>
        /// <exception cref="NotSupportedException">If the given <paramref name="theHash"/> is not a supported HMAC</exception>
        public static System.Security.Cryptography.HMAC CreateInstance(HashType theHash)
        {
            System.Security.Cryptography.HMAC hmac = null;
            switch (theHash)
            {
                case HashType.MD5:
                    {
                        hmac = new HMACMD5();
                        break;
                    }
                case HashType.RIPEMD160:
                    {
                        hmac = new HMACRIPEMD160();
                        break;
                    }
                case HashType.SHA1:
                    {
                        hmac = new HMACSHA1();
                        break;
                    }
                case HashType.SHA256:
                    {
                        hmac = new HMACSHA256();
                        break;
                    }
                case HashType.SHA384:
                    {
                        hmac = new HMACSHA384();
                        break;
                    }
                case HashType.SHA512:
                    {
                        hmac = new HMACSHA512();
                        break;
                    }
                default:
                    {
                        throw new NotSupportedException(String.Format("The given hash :'{0}' is not a supported hmac", theHash));
                    }
            }

            return hmac;
        }
示例#3
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);
 }
		protected override void SetUp () 
		{
			hmac = new HMACRIPEMD160 ();
			hmac.Key = new byte [8];
			hash = hmac;
		}
		public void HMACRIPEMD160_e (string testName, HMACRIPEMD160 hmac, byte[] input, byte[] result) 
		{
			byte[] copy = new byte [input.Length];
			for (int i=0; i < input.Length - 1; i++)
				hmac.TransformBlock (input, i, 1, copy, i);
			hmac.TransformFinalBlock (input, input.Length - 1, 1);
			Assert.AreEqual (result, hmac.Hash, testName + ".e");
			// required or next operation will still return old hash
			hmac.Initialize ();
		}
		public void HMACRIPEMD160_d (string testName, HMACRIPEMD160 hmac, byte[] input, byte[] result) 
		{
			hmac.TransformFinalBlock (input, 0, input.Length);
			Assert.AreEqual (result, hmac.Hash, testName + ".d");
			// required or next operation will still return old hash
			hmac.Initialize ();
		}
		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 ();
		}
示例#9
0
        /// <summary>
        /// 获取加密方法
        /// </summary>
        /// <param name="hmacFormat"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        private static HMAC GetHmac(HmacFormat hmacFormat, byte[] key)
        {
            HMAC hmac = null;

            switch (hmacFormat)
            {
                case HmacFormat.HMACMD5:
                    hmac = new HMACMD5(key);
                    break;

                case HmacFormat.HMACRIPEMD160:
                    hmac = new HMACRIPEMD160(key);
                    break;

                case HmacFormat.HMACSHA1:
                    hmac = new HMACSHA1(key);
                    break;

                case HmacFormat.HMACSHA256:
                    hmac = new HMACSHA256(key);
                    break;

                case HmacFormat.HMACSHA384:
                    hmac = new HMACSHA384(key);
                    break;

                case HmacFormat.HMACSHA512:
                    hmac = new HMACSHA512(key);
                    break;
            }

            return hmac;
        }
		public void SetUp () 
		{
			hmac = new HMACRIPEMD160 ();
		}
		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 ();
		}
示例#12
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
            {

            }
        }
示例#13
0
		public void Invariants ()
		{
			var hmac = new HMACRIPEMD160 ();
			Assert.IsTrue (hmac.CanReuseTransform, "HMACRIPEMD160.CanReuseTransform");
			Assert.IsTrue (hmac.CanTransformMultipleBlocks, "HMACRIPEMD160.CanTransformMultipleBlocks");
			Assert.AreEqual ("RIPEMD160", hmac.HashName, "HMACRIPEMD160.HashName");
			Assert.AreEqual (160, hmac.HashSize, "HMACRIPEMD160.HashSize");
			Assert.AreEqual (1, hmac.InputBlockSize, "HMACRIPEMD160.InputBlockSize");
			Assert.AreEqual (1, hmac.OutputBlockSize, "HMACRIPEMD160.OutputBlockSize");
			Assert.AreEqual (64, hmac.Key.Length, "HMACRIPEMD160.Key.Length");
			Assert.AreEqual ("System.Security.Cryptography.HMACRIPEMD160", hmac.ToString (), "HMACRIPEMD160.ToString()");
		}