From fa6931b7485468788a8157201258c13e41153d3d Mon Sep 17 00:00:00 2001 From: Muzzammil Shahid Date: Thu, 29 Feb 2024 19:00:12 +0500 Subject: [PATCH] implement cryptosign --- .../java/io/xconn/cryptology/CryptoSign.java | 41 +++++++++++++++++ .../io/xconn/cryptology/CryptoSignTest.java | 45 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 src/main/java/io/xconn/cryptology/CryptoSign.java create mode 100644 src/test/java/io/xconn/cryptology/CryptoSignTest.java diff --git a/src/main/java/io/xconn/cryptology/CryptoSign.java b/src/main/java/io/xconn/cryptology/CryptoSign.java new file mode 100644 index 0000000..1b85f61 --- /dev/null +++ b/src/main/java/io/xconn/cryptology/CryptoSign.java @@ -0,0 +1,41 @@ +package io.xconn.cryptology; + +import java.security.SecureRandom; + +import org.bouncycastle.crypto.AsymmetricCipherKeyPair; +import org.bouncycastle.crypto.generators.Ed25519KeyPairGenerator; +import org.bouncycastle.crypto.params.Ed25519KeyGenerationParameters; +import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters; +import org.bouncycastle.crypto.params.Ed25519PublicKeyParameters; +import org.bouncycastle.crypto.signers.Ed25519Signer; + +public class CryptoSign { + public static KeyPair generateKeyPair() { + Ed25519KeyPairGenerator keyPairGenerator = new Ed25519KeyPairGenerator(); + keyPairGenerator.init(new Ed25519KeyGenerationParameters(new SecureRandom())); + + AsymmetricCipherKeyPair keyPair = keyPairGenerator.generateKeyPair(); + + Ed25519PrivateKeyParameters privateKey = (Ed25519PrivateKeyParameters) keyPair.getPrivate(); + Ed25519PublicKeyParameters publicKey = (Ed25519PublicKeyParameters) keyPair.getPublic(); + + return new KeyPair(publicKey.getEncoded(), privateKey.getEncoded()); + } + + public static byte[] getPublicKey(byte[] privateKey) { + Ed25519PrivateKeyParameters privateKeyParam = new Ed25519PrivateKeyParameters(privateKey, 0); + + return privateKeyParam.generatePublicKey().getEncoded(); + } + + + public static byte[] sign(byte[] privateKey, byte[] challenge) { + Ed25519PrivateKeyParameters privateKeyParam = new Ed25519PrivateKeyParameters(privateKey, 0); + + Ed25519Signer signer = new Ed25519Signer(); + signer.init(true, privateKeyParam); + signer.update(challenge, 0, challenge.length); + + return signer.generateSignature(); + } +} diff --git a/src/test/java/io/xconn/cryptology/CryptoSignTest.java b/src/test/java/io/xconn/cryptology/CryptoSignTest.java new file mode 100644 index 0000000..57c1554 --- /dev/null +++ b/src/test/java/io/xconn/cryptology/CryptoSignTest.java @@ -0,0 +1,45 @@ +package io.xconn.cryptology; + +import org.bouncycastle.util.encoders.Hex; +import org.junit.jupiter.api.Test; + +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 io.xconn.cryptology.CryptoSign.generateKeyPair; +import static io.xconn.cryptology.CryptoSign.getPublicKey; +import static io.xconn.cryptology.CryptoSign.sign; + +public class CryptoSignTest { + private static final String privateKey = "6b19991b461d1073918a25525652c4c913fb28b07142faf1146f6bde228653c5"; + private static final String publicKey = "568850ef3a95c4fc3720f534004f1837ed4e32049271b2e1a8c3d979b571e3e4"; + + @Test + public void testGenerateKeyPair() { + KeyPair keyPair = generateKeyPair(); + + assertNotNull(keyPair.getPublicKey()); + assertNotNull(keyPair.getPrivateKey()); + assertEquals(32, keyPair.getPublicKey().length); + assertEquals(32, keyPair.getPrivateKey().length); + } + + @Test + public void testGetPublicKey() { + byte[] publicKey = getPublicKey(Hex.decode(privateKey)); + + assertArrayEquals(Hex.decode(CryptoSignTest.publicKey), publicKey); + } + + @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); + } +}