/// <summary> /// Initializes all required instances. /// </summary> public HashAlgorithmRegister() { psi_128bit = new implementations.psi.utility.UsageHelper_Hash(); md5 = new MD5(); md5hmac = new MD5HMAC(); ripemd160 = new RIPEMD160(); ripemd160hmac = new RIPEMD160HMAC(); sha1 = new SHA1(); sha1hmac = new SHA1HMAC(); sha256 = new SHA256(); sha256hmac = new SHA256HMAC(); sha384 = new SHA384(); sha384hmac = new SHA384HMAC(); sha512 = new SHA512(); sha512hmac = new SHA512HMAC(); }
private void CompareBlocks(HmacAlg Algorithm) { if (Algorithm == HmacAlg.Sha256Hmac) { byte[] hashKey = new byte[32]; byte[] buffer = new byte[640]; byte[] hash1 = new byte[32]; byte[] hash2 = new byte[32]; using (System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider()) { rng.GetBytes(hashKey); rng.GetBytes(buffer); } using (System.Security.Cryptography.HMACSHA256 hmac = new System.Security.Cryptography.HMACSHA256(hashKey)) hash1 = hmac.ComputeHash(buffer); // test 1: HMAC interface HMAC hmac1 = new HMAC(new SHA256Digest()); hmac1.Init(hashKey); hmac1.BlockUpdate(buffer, 0, buffer.Length); hmac1.DoFinal(hash2, 0); if (!Compare.AreEqual(hash2, hash1)) throw new Exception("hmac is not equal!"); // test 2: class with dofinal using (SHA256HMAC hmac = new SHA256HMAC()) { hmac.Init(hashKey); hmac.BlockUpdate(buffer, 0, buffer.Length); hmac.DoFinal(hash2, 0); } if (!Compare.AreEqual(hash2, hash1)) throw new Exception("hmac1 is not equal!"); // test 3: class with computemac using (SHA256HMAC hmac = new SHA256HMAC(hashKey)) hash2 = hmac.ComputeMac(buffer); if (!Compare.AreEqual(hash2, hash1)) throw new Exception("hmac2 is not equal!"); } else { // SHA512 // byte[] hash1 = new byte[64]; byte[] hash2 = new byte[64]; byte[] hashKey = new byte[64]; byte[] buffer = new byte[128]; using (System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider()) { rng.GetBytes(hashKey); rng.GetBytes(buffer); } using (System.Security.Cryptography.HMACSHA512 hmac = new System.Security.Cryptography.HMACSHA512(hashKey)) hash1 = hmac.ComputeHash(buffer); // test 1: HMAC interface HMAC hmac1 = new HMAC(new SHA512Digest()); hmac1.Init(hashKey); hmac1.BlockUpdate(buffer, 0, buffer.Length); hmac1.DoFinal(hash2, 0); if (!Compare.AreEqual(hash2, hash1)) throw new Exception("hmac1 is not equal!"); // test 2: class with dofinal using (SHA512HMAC hmac = new SHA512HMAC()) { hmac.Init(hashKey); hmac.BlockUpdate(buffer, 0, buffer.Length); hmac.DoFinal(hash2, 0); } if (!Compare.AreEqual(hash2, hash1)) throw new Exception("hmac1 is not equal!"); // test 3: class with computemac using (SHA512HMAC hmac = new SHA512HMAC(hashKey)) hash2 = hmac.ComputeMac(buffer); if (!Compare.AreEqual(hash2, hash1)) throw new Exception("hmac2 is not equal!"); } }
private void CompareBlocks(HmacAlg Algorithm) { if (Algorithm == HmacAlg.Sha256Hmac) { byte[] hashKey = new byte[32]; byte[] buffer = new byte[640]; byte[] hash1 = new byte[32]; byte[] hash2 = new byte[32]; using (System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider()) { rng.GetBytes(hashKey); rng.GetBytes(buffer); } using (System.Security.Cryptography.HMACSHA256 hmac = new System.Security.Cryptography.HMACSHA256(hashKey)) hash1 = hmac.ComputeHash(buffer); // test 1: HMAC interface HMAC hmac1 = new HMAC(new SHA256Digest()); hmac1.Init(hashKey); hmac1.BlockUpdate(buffer, 0, buffer.Length); hmac1.DoFinal(hash2, 0); if (!Compare.AreEqual(hash2, hash1)) { throw new Exception("hmac is not equal!"); } // test 2: class with dofinal using (SHA256HMAC hmac = new SHA256HMAC()) { hmac.Init(hashKey); hmac.BlockUpdate(buffer, 0, buffer.Length); hmac.DoFinal(hash2, 0); } if (!Compare.AreEqual(hash2, hash1)) { throw new Exception("hmac1 is not equal!"); } // test 3: class with computemac using (SHA256HMAC hmac = new SHA256HMAC(hashKey)) hash2 = hmac.ComputeMac(buffer); if (!Compare.AreEqual(hash2, hash1)) { throw new Exception("hmac2 is not equal!"); } } else { // SHA512 // byte[] hash1 = new byte[64]; byte[] hash2 = new byte[64]; byte[] hashKey = new byte[64]; byte[] buffer = new byte[128]; using (System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider()) { rng.GetBytes(hashKey); rng.GetBytes(buffer); } using (System.Security.Cryptography.HMACSHA512 hmac = new System.Security.Cryptography.HMACSHA512(hashKey)) hash1 = hmac.ComputeHash(buffer); // test 1: HMAC interface HMAC hmac1 = new HMAC(new SHA512Digest()); hmac1.Init(hashKey); hmac1.BlockUpdate(buffer, 0, buffer.Length); hmac1.DoFinal(hash2, 0); if (!Compare.AreEqual(hash2, hash1)) { throw new Exception("hmac1 is not equal!"); } // test 2: class with dofinal using (SHA512HMAC hmac = new SHA512HMAC()) { hmac.Init(hashKey); hmac.BlockUpdate(buffer, 0, buffer.Length); hmac.DoFinal(hash2, 0); } if (!Compare.AreEqual(hash2, hash1)) { throw new Exception("hmac1 is not equal!"); } // test 3: class with computemac using (SHA512HMAC hmac = new SHA512HMAC(hashKey)) hash2 = hmac.ComputeMac(buffer); if (!Compare.AreEqual(hash2, hash1)) { throw new Exception("hmac2 is not equal!"); } } }