Skip to content

Commit

Permalink
add more keccak256 test cases as required
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim8y committed Oct 9, 2023
1 parent f92cc9f commit c1ee50a
Showing 1 changed file with 82 additions and 4 deletions.
86 changes: 82 additions & 4 deletions tests/Neo.UnitTests/SmartContract/Native/UT_CryptoLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,19 +325,97 @@ public void TestBls12381ScalarMul_Compat()
);
}

/// <summary>
/// Keccak256 cases are verified in https://emn178.github.io/online-tools/keccak_256.html
/// </summary>
[TestMethod]
public void TestKeccak256()
public void TestKeccak256_HelloWorld()
{
// Arrange
byte[] inputData = "Hello, Keccak!"u8.ToArray();
string expectedHashHex = "49222c3dd1a2b5cf30a8720050c31844dbc8eca466595b5ce9c11932b7aac985";
byte[] inputData = "Hello, World!"u8.ToArray();
string expectedHashHex = "acaf3289d7b601cbd114fb36c4d29c85bbfd5e133f14cb355c3fd8d99367964f";

// Act
byte[] outputData = CryptoLib.Keccak256(inputData);
string outputHashHex = Hex.ToHexString(outputData);

// Assert
Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value.");
Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for 'Hello, World!'.");
}
[TestMethod]
public void TestKeccak256_Keccak()
{
// Arrange
byte[] inputData = "Keccak"u8.ToArray();
string expectedHashHex = "868c016b666c7d3698636ee1bd023f3f065621514ab61bf26f062c175fdbe7f2";

// Act
byte[] outputData = CryptoLib.Keccak256(inputData);
string outputHashHex = Hex.ToHexString(outputData);

// Assert
Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for 'Keccak'.");
}

[TestMethod]
public void TestKeccak256_Cryptography()
{
// Arrange
byte[] inputData = "Cryptography"u8.ToArray();
string expectedHashHex = "53d49d225dd2cfe77d8c5e2112bcc9efe77bea1c7aa5e5ede5798a36e99e2d29";

// Act
byte[] outputData = CryptoLib.Keccak256(inputData);
string outputHashHex = Hex.ToHexString(outputData);

// Assert
Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for 'Cryptography'.");
}

[TestMethod]
public void TestKeccak256_Testing123()
{
// Arrange
byte[] inputData = "Testing123"u8.ToArray();
string expectedHashHex = "3f82db7b16b0818a1c6b2c6152e265f682d5ebcf497c9aad776ad38bc39cb6ca";

// Act
byte[] outputData = CryptoLib.Keccak256(inputData);
string outputHashHex = Hex.ToHexString(outputData);

// Assert
Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for 'Testing123'.");
}

[TestMethod]
public void TestKeccak256_LongString()
{
// Arrange
byte[] inputData = "This is a longer string for Keccak256 testing purposes."u8.ToArray();
string expectedHashHex = "24115e5c2359f85f6840b42acd2f7ea47bc239583e576d766fa173bf711bdd2f";

// Act
byte[] outputData = CryptoLib.Keccak256(inputData);
string outputHashHex = Hex.ToHexString(outputData);

// Assert
Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for the longer string.");
}

[TestMethod]
public void TestKeccak256_BlankString()
{
// Arrange
byte[] inputData = ""u8.ToArray();
string expectedHashHex = "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470";

// Act
byte[] outputData = CryptoLib.Keccak256(inputData);
string outputHashHex = Hex.ToHexString(outputData);

// Assert
Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for blank string.");
}

}
}

0 comments on commit c1ee50a

Please sign in to comment.