|
15 | 15 | using Neo.SmartContract;
|
16 | 16 | using Neo.SmartContract.Native;
|
17 | 17 | using Neo.VM;
|
| 18 | +using Org.BouncyCastle.Utilities.Encoders; |
18 | 19 |
|
19 | 20 | namespace Neo.UnitTests.SmartContract.Native
|
20 | 21 | {
|
@@ -334,5 +335,97 @@ public void TestBls12381ScalarMul_Compat()
|
334 | 335 | BLS12381PointType.G2Proj
|
335 | 336 | );
|
336 | 337 | }
|
| 338 | + |
| 339 | + /// <summary> |
| 340 | + /// Keccak256 cases are verified in https://emn178.github.io/online-tools/keccak_256.html |
| 341 | + /// </summary> |
| 342 | + [TestMethod] |
| 343 | + public void TestKeccak256_HelloWorld() |
| 344 | + { |
| 345 | + // Arrange |
| 346 | + byte[] inputData = "Hello, World!"u8.ToArray(); |
| 347 | + string expectedHashHex = "acaf3289d7b601cbd114fb36c4d29c85bbfd5e133f14cb355c3fd8d99367964f"; |
| 348 | + |
| 349 | + // Act |
| 350 | + byte[] outputData = CryptoLib.Keccak256(inputData); |
| 351 | + string outputHashHex = Hex.ToHexString(outputData); |
| 352 | + |
| 353 | + // Assert |
| 354 | + Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for 'Hello, World!'."); |
| 355 | + } |
| 356 | + [TestMethod] |
| 357 | + public void TestKeccak256_Keccak() |
| 358 | + { |
| 359 | + // Arrange |
| 360 | + byte[] inputData = "Keccak"u8.ToArray(); |
| 361 | + string expectedHashHex = "868c016b666c7d3698636ee1bd023f3f065621514ab61bf26f062c175fdbe7f2"; |
| 362 | + |
| 363 | + // Act |
| 364 | + byte[] outputData = CryptoLib.Keccak256(inputData); |
| 365 | + string outputHashHex = Hex.ToHexString(outputData); |
| 366 | + |
| 367 | + // Assert |
| 368 | + Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for 'Keccak'."); |
| 369 | + } |
| 370 | + |
| 371 | + [TestMethod] |
| 372 | + public void TestKeccak256_Cryptography() |
| 373 | + { |
| 374 | + // Arrange |
| 375 | + byte[] inputData = "Cryptography"u8.ToArray(); |
| 376 | + string expectedHashHex = "53d49d225dd2cfe77d8c5e2112bcc9efe77bea1c7aa5e5ede5798a36e99e2d29"; |
| 377 | + |
| 378 | + // Act |
| 379 | + byte[] outputData = CryptoLib.Keccak256(inputData); |
| 380 | + string outputHashHex = Hex.ToHexString(outputData); |
| 381 | + |
| 382 | + // Assert |
| 383 | + Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for 'Cryptography'."); |
| 384 | + } |
| 385 | + |
| 386 | + [TestMethod] |
| 387 | + public void TestKeccak256_Testing123() |
| 388 | + { |
| 389 | + // Arrange |
| 390 | + byte[] inputData = "Testing123"u8.ToArray(); |
| 391 | + string expectedHashHex = "3f82db7b16b0818a1c6b2c6152e265f682d5ebcf497c9aad776ad38bc39cb6ca"; |
| 392 | + |
| 393 | + // Act |
| 394 | + byte[] outputData = CryptoLib.Keccak256(inputData); |
| 395 | + string outputHashHex = Hex.ToHexString(outputData); |
| 396 | + |
| 397 | + // Assert |
| 398 | + Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for 'Testing123'."); |
| 399 | + } |
| 400 | + |
| 401 | + [TestMethod] |
| 402 | + public void TestKeccak256_LongString() |
| 403 | + { |
| 404 | + // Arrange |
| 405 | + byte[] inputData = "This is a longer string for Keccak256 testing purposes."u8.ToArray(); |
| 406 | + string expectedHashHex = "24115e5c2359f85f6840b42acd2f7ea47bc239583e576d766fa173bf711bdd2f"; |
| 407 | + |
| 408 | + // Act |
| 409 | + byte[] outputData = CryptoLib.Keccak256(inputData); |
| 410 | + string outputHashHex = Hex.ToHexString(outputData); |
| 411 | + |
| 412 | + // Assert |
| 413 | + Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for the longer string."); |
| 414 | + } |
| 415 | + |
| 416 | + [TestMethod] |
| 417 | + public void TestKeccak256_BlankString() |
| 418 | + { |
| 419 | + // Arrange |
| 420 | + byte[] inputData = ""u8.ToArray(); |
| 421 | + string expectedHashHex = "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"; |
| 422 | + |
| 423 | + // Act |
| 424 | + byte[] outputData = CryptoLib.Keccak256(inputData); |
| 425 | + string outputHashHex = Hex.ToHexString(outputData); |
| 426 | + |
| 427 | + // Assert |
| 428 | + Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for blank string."); |
| 429 | + } |
337 | 430 | }
|
338 | 431 | }
|
0 commit comments