Sign And Verify Messages With ed25519
Generate an Ed25519 key pair, sign a fixed message, and show tampering either the message or the signature flips Verify to false.
Code
package main
import (
"crypto/ed25519"
"crypto/rand"
"fmt"
)
func main() {
pub, priv, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
panic(err)
}
message := []byte("release 2026-11-15 build 4213")
signature := ed25519.Sign(priv, message)
fmt.Printf("signature bytes: %d\n", len(signature))
// 原消息与原签名:校验通过
fmt.Println("verify original:", ed25519.Verify(pub, message, signature))
// 消息被篡改:签名失效
tampered := append([]byte{}, message...)
tampered[0] ^= 0x01
fmt.Println("verify tampered message:", ed25519.Verify(pub, tampered, signature))
// 签名本身被篡改:校验同样失败
badSig := append([]byte{}, signature...)
badSig[0] ^= 0x01
fmt.Println("verify tampered signature:", ed25519.Verify(pub, message, badSig))
}What the code shows
The snippet generates an Ed25519 key pair with ed25519.GenerateKey(rand.Reader), signs a fixed release message, and prints the signature length — Ed25519 signatures are 64 bytes. It then runs three verifications against ed25519.Verify and prints each boolean.
Tamper checks
- Original message with original signature: Verify returns true.
- One flipped byte in the message (tampered[0] ^= 0x01): Verify returns false.
- One flipped byte in the signature (badSig[0] ^= 0x01): Verify also returns false.
How to run it
The program needs no network or files — only crypto/rand. Every printed value is deterministic except the key material itself, so the booleans are reproducible across runs.
Interactive Tools for This Topic
HMAC GeneratorCompute an HMAC-SHA-256, SHA-384, or SHA-512 signature for a message and key using the Web Crypto API, with hex and Base64 output.Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.SHA-256 Hash GeneratorGenerate SHA-256 cryptographic hashes from text input using the Web Crypto API. Compare hashes and verify data integrity with hexadecimal digest output.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.
FAQ
- How long is an Ed25519 signature?
- Exactly 64 bytes; the program prints len(signature) right after signing.
- Why does flipping one bit in the signature break verification?
- Verify recomputes the signature relation from the public key, the message, and the signature; any altered byte makes the equation fail, so Verify returns false.
Report a problem with this exampleOpens a pre-filled issue on GitHub.