public bool CompareTo(params HashDataProvider[] hashDataProviders) { HashProvider hasher = new HashProvider (_hashAlgo); byte[] localHash = hasher.Hash (hashDataProviders); return ByteHelper.CompareByteArrays (_digest, localHash); }
/// <summary> /// Locks the password and calculates the password hash /// </summary> public void Hash() { if (_protectedHash != null) { throw new NotSupportedException("Cannot hash twice"); } if (_injectedHash != null) { return; } _plainPassword.MakeReadOnly(); HashProvider hashProvider = new HashProvider(_hashAlgo); //The array fpr protected memory needs to have multiple size of 16 //int byteArrayLength = // 4 // 4 bytes size of the actual hash value // + hashProvider.HashBitSize/8; //byteArrayLength += byteArrayLength % 16; //byte[] myHashValue = new byte[byteArrayLength]; _currentHashSize = hashProvider.HashBitSize / 8; byte[] myHashValue = new byte[_currentHashSize]; hashProvider.Hash(myHashValue, 0, new HashSecureStringDataProvider(_plainPassword)); //HACK: It's not sure that the converted strings get deleted by the // garbage collector. Use ProtectedMemory as soon as possible _protectedHash = new SecureString(); foreach (byte b in myHashValue) { string hexString = string.Format("{0:X2}", b); foreach (char c in hexString) { _protectedHash.AppendChar(c); } } //Writes the size of the ahsh value to the start of the hashvalue array //Array.Copy(BitConverter.GetBytes((int)hashProvider.HashBitSize/8), myHashValue, 4); //Hash value starts at index 4 //hashProvider.Hash(myHashValue, 4, new HashSecureStringDataProvider(_plainPassword)); //ProtectedData.Protect(hashProvider, null, DataProtectionScope. //ProtectedMemory.Protect(myHashValue, MemoryProtectionScope.SameProcess); }
/// <summary> /// Locks the password and calculates the password hash /// </summary> public void Hash() { if (_protectedHash != null) throw new NotSupportedException ("Cannot hash twice"); if(_injectedHash != null) return; _plainPassword.MakeReadOnly (); HashProvider hashProvider = new HashProvider (_hashAlgo); //The array fpr protected memory needs to have multiple size of 16 //int byteArrayLength = // 4 // 4 bytes size of the actual hash value // + hashProvider.HashBitSize/8; //byteArrayLength += byteArrayLength % 16; //byte[] myHashValue = new byte[byteArrayLength]; _currentHashSize = hashProvider.HashBitSize / 8; byte[] myHashValue = new byte[_currentHashSize]; hashProvider.Hash (myHashValue, 0, new HashSecureStringDataProvider (_plainPassword)); //HACK: It's not sure that the converted strings get deleted by the // garbage collector. Use ProtectedMemory as soon as possible _protectedHash = new SecureString (); foreach (byte b in myHashValue) { string hexString = string.Format ("{0:X2}", b); foreach (char c in hexString) _protectedHash.AppendChar (c); } //Writes the size of the ahsh value to the start of the hashvalue array //Array.Copy(BitConverter.GetBytes((int)hashProvider.HashBitSize/8), myHashValue, 4); //Hash value starts at index 4 //hashProvider.Hash(myHashValue, 4, new HashSecureStringDataProvider(_plainPassword)); //ProtectedData.Protect(hashProvider, null, DataProtectionScope. //ProtectedMemory.Protect(myHashValue, MemoryProtectionScope.SameProcess); }
private static void TestHMAC() { ILog log = LogManager.GetLogger("TestHMAC"); HashProvider hash = new HashProvider(); byte[] h1 = hash.Hash( new HashPrimitiveDataProvider((uint)0x3c), new HashPrimitiveDataProvider((ushort)0x00), new HashEnumDataProvider(testenum.test), new HashByteDataProvider(new byte[]{0xb3,0xd5,0xcb, 0x12,0x73, 0x8b, 0xb6, 0xf9, 0x21, 0xa3, 0xda, 0x42,0xe0, 0x18, 0xd1, 0x43, 0xfa, 0x29, 0x7c, 0xa6})); HMACProvider hmac = new HMACProvider( new byte[]{0x75, 0xf0, 0x86, 0x84, 0x78, 0x24, 0xf8, 0x79, 0x39, 0x5a, 0x18, 0x14, 0x1d, 0x19, 0x0c, 0x2f, 0x01, 0x29, 0x0b, 0x05}); byte[] h2 = new byte[20]; for(int i = 0; i<20; i++) h2[i] = 0xa5; byte[] h3 = new byte[]{ 0xb9, 0x73, 0x05, 0xfa, 0xdb, 0xe3, 0x4d, 0xc5, 0x46, 0x65, 0x10, 0x00, 0x0a, 0x55, 0x04, 0x2e, 0x3f, 0xea, 0xbf, 0x27}; byte[] result = hmac.Hash(new HashByteDataProvider(h1), new HashByteDataProvider(h2), new HashByteDataProvider(h3), new HashPrimitiveDataProvider(true)); byte[] expected = new byte[]{ 0x26, 0x7e, 0xca, 0x16, 0xa1, 0x4d, 0x36, 0xe6, 0x72, 0x2e, 0xaa, 0x7f, 0x7b, 0x53, 0x4a, 0xb3, 0xce, 0x8b, 0x2a, 0xaa}; for(int i = 0; i<20; i++) { if(result[i] != expected[i]) Console.WriteLine("FAILED"); } Console.WriteLine("SUCCESS"); }