Class Ed25519
Ed25519 signatures (RFC 8032): the signatures of Solana, Cardano, Stellar, Tezos, SSH keys and many protocols beside.
A private key is a 32-byte seed; the public key is 32 bytes and a
signature 64. Signing is deterministic and constant time; verification is
the cofactorless RFC 8032 check, rejecting a non-canonical S and a
public key or R that does not decode, as libsodium does.
byte[] seed = SecureRandom.bytes(32);
byte[] message = "hello".getBytes();
byte[] pub = Ed25519.publicKey(seed);
byte[] sig = Ed25519.sign(seed, pub, message);
boolean ok = Ed25519.verify(message, sig, pub);
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intLength of a private key (the seed).static final intLength of a public key.static final intLength of a signature. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanisOnCurve(byte[] point) Whether 32 bytes are the encoding of a point on the curve.static byte[]publicKey(byte[] privateKey) The public key of a 32-byte seed.static byte[]sign(byte[] privateKey, byte[] message) Signs a message with a seed, deriving the public key first.static byte[]sign(byte[] privateKey, byte[] publicKey, byte[] message) Signs a message.static booleanverify(byte[] message, byte[] signature, byte[] publicKey) Verifies a signature.
-
Field Details
-
PRIVATE_KEY_LENGTH
public static final int PRIVATE_KEY_LENGTHLength of a private key (the seed).- See Also:
-
PUBLIC_KEY_LENGTH
public static final int PUBLIC_KEY_LENGTHLength of a public key.- See Also:
-
SIGNATURE_LENGTH
public static final int SIGNATURE_LENGTHLength of a signature.- See Also:
-
-
Method Details
-
isOnCurve
public static boolean isOnCurve(byte[] point) Whether 32 bytes are the encoding of a point on the curve. Solana's program-derived addresses are exactly the hashes for which this answersfalse. -
publicKey
public static byte[] publicKey(byte[] privateKey) The public key of a 32-byte seed.
Throws
CryptoException: if the seed is not 32 bytes
-
sign
public static byte[] sign(byte[] privateKey, byte[] message) Signs a message with a seed, deriving the public key first. When the public key is at hand, [#sign(byte[], byte[], byte[])] saves that work, which is most of the cost of a signature. -
sign
public static byte[] sign(byte[] privateKey, byte[] publicKey, byte[] message) Signs a message.
Parameters
-
privateKey: the 32-byte seed -
publicKey: the seed's public key, frompublicKey(byte[]); a wrong one produces a signature nobody can verify -
message: the message, of any length
Returns
the 64-byte signature
Throws
CryptoException: if an argument is null or the wrong length
-
-
verify
public static boolean verify(byte[] message, byte[] signature, byte[] publicKey) Verifies a signature.
Returns
trueifsignaturewas made overmessageby the key behindpublicKey;falsefor any malformed or wrong signature or key
-