-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3cfd5ef
commit fa6931b
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |