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.

GoSecurity Boundaries

Code

gosnippet
31 lines
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

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.