Search in sources :

Example 1 with KeyPairGenerator

use of net.i2p.crypto.eddsa.KeyPairGenerator 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);
    }
}
Also used : EdDSAEngine(net.i2p.crypto.eddsa.EdDSAEngine) Path(java.nio.file.Path) KeyPair(java.security.KeyPair) EdDSAPublicKey(net.i2p.crypto.eddsa.EdDSAPublicKey) KeyPairGenerator(net.i2p.crypto.eddsa.KeyPairGenerator) EdDSAPrivateKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) EdDSAPrivateKey(net.i2p.crypto.eddsa.EdDSAPrivateKey) EdDSAPublicKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec) PostConstruct(javax.annotation.PostConstruct)

Aggregations

IOException (java.io.IOException)1 Path (java.nio.file.Path)1 KeyPair (java.security.KeyPair)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 PostConstruct (javax.annotation.PostConstruct)1 EdDSAEngine (net.i2p.crypto.eddsa.EdDSAEngine)1 EdDSAPrivateKey (net.i2p.crypto.eddsa.EdDSAPrivateKey)1 EdDSAPublicKey (net.i2p.crypto.eddsa.EdDSAPublicKey)1 KeyPairGenerator (net.i2p.crypto.eddsa.KeyPairGenerator)1 EdDSAPrivateKeySpec (net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec)1 EdDSAPublicKeySpec (net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec)1