Ed25519 Sign And Verify Payload
Sign a payload with the Ed25519 JCA Signature API and verify both the original bytes and a tampered copy, which must fail.
Code
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.Signature;
public class Ed25519Signer {
public static void main(String[] args) throws Exception {
KeyPairGenerator generator = KeyPairGenerator.getInstance("Ed25519");
KeyPair keyPair = generator.generateKeyPair();
byte[] payload = "release notes v2".getBytes(StandardCharsets.UTF_8);
Signature signer = Signature.getInstance("Ed25519");
signer.initSign(keyPair.getPrivate());
signer.update(payload);
byte[] signature = signer.sign();
System.out.println("signature bytes: " + signature.length);
Signature verifier = Signature.getInstance("Ed25519");
verifier.initVerify(keyPair.getPublic());
verifier.update(payload);
System.out.println("valid payload: " + verifier.verify(signature));
// verify resets the object to its initial state: a different payload can be verified next, and tampering must fail
verifier.update("release notes v3".getBytes(StandardCharsets.UTF_8));
System.out.println("tampered payload: " + verifier.verify(signature));
}
}How the snippet works
The snippet is organized around the class Ed25519Signer.
What to watch for
- Imports, when present, point only at standard-library modules.
Applying it
Look for the signature bytes:, valid payload:, tampered payload: prefixes once the file starts running; each one ties a printed line to the step that produced it.
Statements that decide the outcome
KeyPairGenerator generator = KeyPairGenerator.getInstance("Ed25519");
byte[] payload = "release notes v2".getBytes(StandardCharsets.UTF_8);
verifier.update("release notes v3".getBytes(StandardCharsets.UTF_8));
Interactive Tools for This Topic
JWT Signature InspectorInspect a JWT signature: decode the header and payload, show the algorithm and signature bytes, and verify HS256/384/512 HMAC signatures with a secret.Webhook HMAC Signature VerifierVerify and generate HMAC-SHA256 webhook signatures for Stripe, GitHub, and raw hex headers locally using Web Crypto and constant-time comparison.Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.
FAQ
- What does Ed25519 Sign And Verify Payload print when run?
- The script runs standalone; its 3 labeled outputs include `signature bytes:`, `valid payload:`, `tampered payload:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.