Skip to content

Commit

Permalink
Add function to verify Cryptosign challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
muzzammilshahid committed May 13, 2024
1 parent 326e984 commit 610410b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
18 changes: 18 additions & 0 deletions cryptology/src/main/java/io/xconn/cryptology/CryptoSign.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
22 changes: 17 additions & 5 deletions cryptology/src/test/java/io/xconn/cryptology/CryptoSignTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
}
}

0 comments on commit 610410b

Please sign in to comment.