diff --git a/cryptology/src/main/java/io/xconn/cryptology/CryptoSign.java b/cryptology/src/main/java/io/xconn/cryptology/CryptoSign.java index 2a5caea..f49fa37 100644 --- a/cryptology/src/main/java/io/xconn/cryptology/CryptoSign.java +++ b/cryptology/src/main/java/io/xconn/cryptology/CryptoSign.java @@ -56,4 +56,22 @@ public static byte[] sign(byte[] privateKey, byte[] challenge) { return signer.generateSignature(); } + + /** + * Verifies the signature of the given challenge using the publicKey. + * + * @param publicKey publicKey bytes. + * @param challenge challenge bytes. + * @param signature signature bytes to verify. + * @return true if the signature is valid, false otherwise. + */ + public static boolean verify(byte[] publicKey, byte[] challenge, byte[] signature) { + Ed25519PublicKeyParameters publicKeyParam = new Ed25519PublicKeyParameters(publicKey, 0); + + Ed25519Signer verifier = new Ed25519Signer(); + verifier.init(false, publicKeyParam); + verifier.update(challenge, 0, challenge.length); + + return verifier.verifySignature(signature); + } } diff --git a/cryptology/src/test/java/io/xconn/cryptology/CryptoSignTest.java b/cryptology/src/test/java/io/xconn/cryptology/CryptoSignTest.java index 57c1554..c5fef61 100644 --- a/cryptology/src/test/java/io/xconn/cryptology/CryptoSignTest.java +++ b/cryptology/src/test/java/io/xconn/cryptology/CryptoSignTest.java @@ -6,10 +6,12 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import static io.xconn.cryptology.CryptoSign.generateKeyPair; import static io.xconn.cryptology.CryptoSign.getPublicKey; import static io.xconn.cryptology.CryptoSign.sign; +import static io.xconn.cryptology.CryptoSign.verify; public class CryptoSignTest { private static final String privateKey = "6b19991b461d1073918a25525652c4c913fb28b07142faf1146f6bde228653c5"; @@ -32,14 +34,24 @@ public void testGetPublicKey() { assertArrayEquals(Hex.decode(CryptoSignTest.publicKey), publicKey); } + String challengeString = "f9d17535fb925e9f674d648cbfc41399"; + String signatureString = "054932bce44c62d749723f808c2f7ba8b3eb6fe27a2886644317" + "cc95022da5b6211866f36572da9ee783fb229e63d0c76ab050e8aa840a48d8285537ed57f70f"; + @Test public void testSign() { - String challengeString = "f9d17535fb925e9f674d648cbfc41399"; - String expectedSignedString = "054932bce44c62d749723f808c2f7ba8b3eb6fe27a2886644317" + - "cc95022da5b6211866f36572da9ee783fb229e63d0c76ab050e8aa840a48d8285537ed57f70f"; - byte[] signedChallenge = sign(Hex.decode(privateKey), Hex.decode(challengeString)); - assertArrayEquals(Hex.decode(expectedSignedString), signedChallenge); + assertArrayEquals(Hex.decode(signatureString), signedChallenge); + } + + @Test + public void testVerify() { + byte[] challenge = Hex.decode(challengeString); + byte[] signature = Hex.decode(signatureString); + byte[] publicKeyBytes = Hex.decode(publicKey); + + boolean verified = verify(publicKeyBytes, challenge, signature); + + assertTrue(verified); } }