use of net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec in project mxisd by kamax-io.
the class KeyManager method build.
@PostConstruct
public void build() {
try {
keySpecs = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.CURVE_ED25519_SHA512);
signEngine = new EdDSAEngine(MessageDigest.getInstance(keySpecs.getHashAlgorithm()));
keys = new ArrayList<>();
Path privKey = Paths.get(keyCfg.getPath());
if (!Files.exists(privKey)) {
KeyPair pair = (new KeyPairGenerator()).generateKeyPair();
String keyEncoded = Base64.getEncoder().encodeToString(pair.getPrivate().getEncoded());
FileUtils.writeStringToFile(privKey.toFile(), keyEncoded, StandardCharsets.ISO_8859_1);
keys.add(pair);
} else {
if (Files.isDirectory(privKey)) {
throw new RuntimeException("Invalid path for private key: " + privKey.toString());
}
if (Files.isReadable(privKey)) {
byte[] seed = Base64.getDecoder().decode(FileUtils.readFileToString(privKey.toFile(), StandardCharsets.ISO_8859_1));
EdDSAPrivateKeySpec privKeySpec = new EdDSAPrivateKeySpec(seed, keySpecs);
EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec(privKeySpec.getA(), keySpecs);
keys.add(new KeyPair(new EdDSAPublicKey(pubKeySpec), new EdDSAPrivateKey(privKeySpec)));
}
}
} catch (NoSuchAlgorithmException | IOException e) {
throw new RuntimeException(e);
}
}
use of net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec in project i2p.i2p by i2p.
the class EdDSAEngineTest method testVerifyResetsForReuse.
@Test
public void testVerifyResetsForReuse() throws Exception {
Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK, EdDSANamedCurveTable.getByName("ed25519-sha-512"));
PublicKey vKey = new EdDSAPublicKey(pubKey);
sgr.initVerify(vKey);
// First usage
sgr.update(new byte[] { 0 });
sgr.verify(TEST_MSG_SIG);
// Second usage
sgr.update(TEST_MSG);
assertThat("Second verify failed", sgr.verify(TEST_MSG_SIG), is(true));
}
use of net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec in project i2p.i2p by i2p.
the class EdDSAEngineTest method testVerifyOneShotModeMultipleUpdates.
@Test
public void testVerifyOneShotModeMultipleUpdates() throws Exception {
Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK, EdDSANamedCurveTable.getByName("ed25519-sha-512"));
PublicKey vKey = new EdDSAPublicKey(pubKey);
sgr.initVerify(vKey);
sgr.setParameter(EdDSAEngine.ONE_SHOT_MODE);
sgr.update(TEST_MSG);
exception.expect(SignatureException.class);
exception.expectMessage("update() already called");
sgr.update(TEST_MSG);
}
use of net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec in project i2p.i2p by i2p.
the class EdDSAEngineTest method testVerifyOneShot.
@Test
public void testVerifyOneShot() throws Exception {
EdDSAEngine sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK, EdDSANamedCurveTable.getByName("ed25519-sha-512"));
PublicKey vKey = new EdDSAPublicKey(pubKey);
sgr.initVerify(vKey);
assertThat("verifyOneShot() failed", sgr.verifyOneShot(TEST_MSG, TEST_MSG_SIG), is(true));
}
use of net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec in project i2p.i2p by i2p.
the class KeyPairGenerator method generateKeyPair.
public KeyPair generateKeyPair() {
if (!initialized)
initialize(DEFAULT_STRENGTH, RandomSource.getInstance());
byte[] seed = new byte[edParams.getCurve().getField().getb() / 8];
random.nextBytes(seed);
EdDSAPrivateKeySpec privKey = new EdDSAPrivateKeySpec(seed, edParams);
EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(privKey.getA(), edParams);
return new KeyPair(new EdDSAPublicKey(pubKey), new EdDSAPrivateKey(privKey));
}
Aggregations