public byte[] GenerateIdentificationHash(IdentificationHash IDHash) { BCrypt bCrypt = new BCrypt(); bCrypt.CreateHashNoSecretNoHMAC(); bCrypt.HashData(IDHash.SecondClientHelloHeader); bCrypt.HashData(IDHash.SecondClientHelloData); bCrypt.HashData(IDHash.ServerHelloResponseHeader); bCrypt.HashData(IDHash.ServerHelloResponseData); bCrypt.HashData(IDHash.ServerHelloResponseSecondPartHeader); bCrypt.HashData(IDHash.ServerHelloResponseSecondPartData); bCrypt.HashData(IDHash.ServerHelloResponseSecondPartTail); //bCrypt.HashData(new byte[] { }); bCrypt.HashData(IDHash.ClientKeyExchangeHeader); bCrypt.HashData(IDHash.ClientKeyExchangeData); bCrypt.HashData(IDHash.EncryptedHashMessageHeader); bCrypt.HashData(IDHash.EncryptedHashMessageData); byte[] outputBuff = new byte[32]; bCrypt.FinishHash(outputBuff); BCrypt secondBCrypt = new BCrypt(); byte[] secondOutputBuff = new byte[32]; secondBCrypt.CreateHash(this.hashSecret); secondBCrypt.HashData(Encoding.Default.GetBytes("server finished")); secondBCrypt.HashData(outputBuff); secondBCrypt.FinishHash(secondOutputBuff); secondBCrypt.HashData(secondOutputBuff); bCrypt = secondBCrypt.Duplicate(); bCrypt.HashData(Encoding.Default.GetBytes("server finished")); bCrypt.HashData(outputBuff); bCrypt.FinishHash(secondOutputBuff); byte[] retVar = new byte[0xC]; Array.Copy(secondOutputBuff, 0, retVar, 0, retVar.Length); return(retVar); }
public Crypt(byte[] ClientRandom, byte[] ServerRandom) { BCrypt bCrypt = new BCrypt(); bCrypt.CreateHash(HashSecretServerPassword); byte[] masterSecretBuff = Encoding.Default.GetBytes("master secret"); bCrypt.HashData(masterSecretBuff); byte[] BothRandomBuff = new byte[64]; Array.Copy(ClientRandom, BothRandomBuff, 32); Array.Copy(ServerRandom, 0, BothRandomBuff, 32, 32); bCrypt.HashData(BothRandomBuff); byte[] outputBuff = new byte[32]; bCrypt.FinishHash(outputBuff); bCrypt.HashData(outputBuff); BCrypt secondBCryptHashHandle = bCrypt.Duplicate(); bCrypt.FinishHash(outputBuff); secondBCryptHashHandle.HashData(masterSecretBuff); secondBCryptHashHandle.HashData(BothRandomBuff); byte[] secondOutputBuff = new byte[32]; secondBCryptHashHandle.FinishHash(secondOutputBuff); Array.Copy(secondOutputBuff, hashSecret, secondOutputBuff.Length); //hashsecret part 1 bCrypt.HashData(outputBuff); secondBCryptHashHandle = bCrypt.Duplicate(); bCrypt.FinishHash(outputBuff); secondBCryptHashHandle.HashData(masterSecretBuff); secondBCryptHashHandle.HashData(BothRandomBuff); secondBCryptHashHandle.FinishHash(secondOutputBuff); Array.Copy(secondOutputBuff, 0, hashSecret, 32, 16); //hashsecret part 2 bCrypt = new BCrypt(); bCrypt.CreateHash(hashSecret); byte[] keyExpansionBuff = Encoding.Default.GetBytes("key expansion"); bCrypt.HashData(keyExpansionBuff); byte[] BothRandomReversedBuff = new byte[64]; Array.Copy(ServerRandom, BothRandomReversedBuff, 32); Array.Copy(ClientRandom, 0, BothRandomReversedBuff, 32, 32); bCrypt.HashData(BothRandomReversedBuff); bCrypt.FinishHash(outputBuff); bCrypt.HashData(outputBuff); secondBCryptHashHandle = bCrypt.Duplicate(); bCrypt.FinishHash(outputBuff); secondBCryptHashHandle.HashData(keyExpansionBuff); secondBCryptHashHandle.HashData(BothRandomReversedBuff); secondBCryptHashHandle.FinishHash(secondOutputBuff); Array.Copy(secondOutputBuff, 0, DecryptionKey, 0, 16); Array.Copy(secondOutputBuff, 16, EncryptionKey, 0, 16); bCrypt.HashData(outputBuff); secondBCryptHashHandle = bCrypt.Duplicate(); bCrypt.FinishHash(outputBuff); secondBCryptHashHandle.HashData(keyExpansionBuff); secondBCryptHashHandle.HashData(BothRandomReversedBuff); secondBCryptHashHandle.FinishHash(secondOutputBuff); Array.Copy(secondOutputBuff, 0, DecryptNonceHash, 0, 4); Array.Copy(secondOutputBuff, 4, EncryptNonceHash, 0, 4); Console.WriteLine("DecryptNonceHash:" + Helpers.ByteArrayToString(DecryptNonceHash)); Console.WriteLine("EncryptNonceHash:" + Helpers.ByteArrayToString(EncryptNonceHash)); }