Search in sources :

Example 1 with Ed25519PublicKeyParameters

use of org.bouncycastle.crypto.params.Ed25519PublicKeyParameters in project popstellar by dedis.

the class Base64DataUtils method generatePoPToken.

/**
 * @return a pseudo randomly generated valid Ed25519 keypair
 */
public static PoPToken generatePoPToken() {
    byte[] privateKey = new byte[KEY_LENGTH];
    RANDOM.nextBytes(privateKey);
    Ed25519PrivateKeyParameters privateKeyParameters = new Ed25519PrivateKeyParameters(privateKey, 0);
    Ed25519PublicKeyParameters publicKeyParameters = privateKeyParameters.generatePublicKey();
    return new PoPToken(privateKey, publicKeyParameters.getEncoded());
}
Also used : PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) Ed25519PublicKeyParameters(org.bouncycastle.crypto.params.Ed25519PublicKeyParameters) Ed25519PrivateKeyParameters(org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters)

Example 2 with Ed25519PublicKeyParameters

use of org.bouncycastle.crypto.params.Ed25519PublicKeyParameters in project hedera-services by hashgraph.

the class Ed25519PrivateKey method fromBytes.

public static Ed25519PrivateKey fromBytes(byte[] keyBytes) {
    Ed25519PrivateKeyParameters privKeyParams;
    Ed25519PublicKeyParameters pubKeyParams = null;
    if (keyBytes.length == Ed25519.SECRET_KEY_SIZE) {
        // if the decoded bytes matches the length of a private key, try that
        privKeyParams = new Ed25519PrivateKeyParameters(keyBytes, 0);
    } else if (keyBytes.length == Ed25519.SECRET_KEY_SIZE + Ed25519.PUBLIC_KEY_SIZE) {
        // some legacy code delivers private and public key pairs concatted together
        try {
            // this is how we read only the first 32 bytes
            privKeyParams = new Ed25519PrivateKeyParameters(new ByteArrayInputStream(keyBytes, 0, Ed25519.SECRET_KEY_SIZE));
            // read the remaining 32 bytes as the public key
            pubKeyParams = new Ed25519PublicKeyParameters(keyBytes, Ed25519.SECRET_KEY_SIZE);
            return new Ed25519PrivateKey(privKeyParams, pubKeyParams);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } else {
        // decode a properly DER-encoded private key descriptor
        var privateKeyInfo = PrivateKeyInfo.getInstance(keyBytes);
        try {
            var privateKey = privateKeyInfo.parsePrivateKey();
            privKeyParams = new Ed25519PrivateKeyParameters(((ASN1OctetString) privateKey).getOctets(), 0);
            var pubKeyData = privateKeyInfo.getPublicKeyData();
            if (pubKeyData != null) {
                pubKeyParams = new Ed25519PublicKeyParameters(pubKeyData.getOctets(), 0);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return new Ed25519PrivateKey(privKeyParams, pubKeyParams);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Ed25519PublicKeyParameters(org.bouncycastle.crypto.params.Ed25519PublicKeyParameters) IOException(java.io.IOException) Ed25519PrivateKeyParameters(org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters)

Example 3 with Ed25519PublicKeyParameters

use of org.bouncycastle.crypto.params.Ed25519PublicKeyParameters in project starcoin-java by starcoinorg.

the class SignatureUtils method getPublicKey.

@SneakyThrows
public static Ed25519PublicKey getPublicKey(Ed25519PrivateKey privateKey) {
    Ed25519PrivateKeyParameters key = new Ed25519PrivateKeyParameters(privateKey.value.content(), 0);
    Ed25519PublicKeyParameters publicKeyParameters = key.generatePublicKey();
    Ed25519PublicKey publicKey = new Ed25519PublicKey(new Bytes(publicKeyParameters.getEncoded()));
    return publicKey;
}
Also used : Bytes(com.novi.serde.Bytes) Ed25519PublicKeyParameters(org.bouncycastle.crypto.params.Ed25519PublicKeyParameters) Ed25519PrivateKeyParameters(org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters) SneakyThrows(lombok.SneakyThrows)

Example 4 with Ed25519PublicKeyParameters

use of org.bouncycastle.crypto.params.Ed25519PublicKeyParameters in project starcoin-java by starcoinorg.

the class SignatureUtils method multiEd25519Verify.

public static boolean multiEd25519Verify(MultiEd25519PublicKey publicKey, byte[] signingMessage, byte[] signature) {
    Ed25519PublicKeyParameters key = new Ed25519PublicKeyParameters(publicKey.value.content(), 0);
    Ed25519Signer signer = new Ed25519Signer();
    signer.init(false, key);
    signer.update(signingMessage, 0, signingMessage.length);
    return signer.verifySignature(signature);
}
Also used : Ed25519Signer(org.bouncycastle.crypto.signers.Ed25519Signer) Ed25519PublicKeyParameters(org.bouncycastle.crypto.params.Ed25519PublicKeyParameters)

Example 5 with Ed25519PublicKeyParameters

use of org.bouncycastle.crypto.params.Ed25519PublicKeyParameters in project starcoin-java by starcoinorg.

the class SignatureUtils method ed25519Verify.

public static boolean ed25519Verify(Ed25519PublicKey publicKey, byte[] signingMessage, byte[] signature) {
    Ed25519PublicKeyParameters key = new Ed25519PublicKeyParameters(publicKey.value.content(), 0);
    Ed25519Signer signer = new Ed25519Signer();
    signer.init(false, key);
    signer.update(signingMessage, 0, signingMessage.length);
    return signer.verifySignature(signature);
}
Also used : Ed25519Signer(org.bouncycastle.crypto.signers.Ed25519Signer) Ed25519PublicKeyParameters(org.bouncycastle.crypto.params.Ed25519PublicKeyParameters)

Aggregations

Ed25519PublicKeyParameters (org.bouncycastle.crypto.params.Ed25519PublicKeyParameters)13 Ed25519PrivateKeyParameters (org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters)9 Ed25519Signer (org.bouncycastle.crypto.signers.Ed25519Signer)4 PoPToken (com.github.dedis.popstellar.model.objects.security.PoPToken)2 IOException (java.io.IOException)2 SneakyThrows (lombok.SneakyThrows)2 KeyGenerationException (com.github.dedis.popstellar.utility.error.keys.KeyGenerationException)1 UninitializedWalletException (com.github.dedis.popstellar.utility.error.keys.UninitializedWalletException)1 Bytes (com.novi.serde.Bytes)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 GeneralSecurityException (java.security.GeneralSecurityException)1 SignatureException (java.security.SignatureException)1 CryptoException (org.bouncycastle.crypto.CryptoException)1 Signer (org.bouncycastle.crypto.Signer)1 SHA256Digest (org.bouncycastle.crypto.digests.SHA256Digest)1 RSAKeyParameters (org.bouncycastle.crypto.params.RSAKeyParameters)1 RSADigestSigner (org.bouncycastle.crypto.signers.RSADigestSigner)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1 UnsignedByte (org.xrpl.xrpl4j.codec.addresses.UnsignedByte)1