Movatterモバイル変換


[0]ホーム

URL:


ed25519

packagestandard library
go1.25.5Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2025 License:BSD-3-ClauseImports:9Imported by:8,319

Details

Repository

cs.opensource.google/go/go

Links

Documentation

Overview

Package ed25519 implements the Ed25519 signature algorithm. Seehttps://ed25519.cr.yp.to/.

These functions are also compatible with the “Ed25519” function defined inRFC 8032. However, unlikeRFC 8032's formulation, this package's private keyrepresentation includes a public key suffix to make multiple signingoperations with the same key more efficient. This package refers to theRFC8032 private key as the “seed”.

Operations involving private keys are implemented using constant-timealgorithms.

Example (Ed25519ctx)
pub, priv, err := GenerateKey(nil)if err != nil {log.Fatal(err)}msg := []byte("The quick brown fox jumps over the lazy dog")sig, err := priv.Sign(nil, msg, &Options{Context: "Example_ed25519ctx",})if err != nil {log.Fatal(err)}if err := VerifyWithOptions(pub, msg, sig, &Options{Context: "Example_ed25519ctx",}); err != nil {log.Fatal("invalid signature")}

Index

Examples

Constants

View Source
const (// PublicKeySize is the size, in bytes, of public keys as used in this package.PublicKeySize = 32// PrivateKeySize is the size, in bytes, of private keys as used in this package.PrivateKeySize = 64// SignatureSize is the size, in bytes, of signatures generated and verified by this package.SignatureSize = 64// SeedSize is the size, in bytes, of private key seeds. These are the private key representations used byRFC 8032.SeedSize = 32)

Variables

This section is empty.

Functions

funcGenerateKey

func GenerateKey(randio.Reader) (PublicKey,PrivateKey,error)

GenerateKey generates a public/private key pair using entropy from rand.If rand is nil,crypto/rand.Reader will be used.

The output of this function is deterministic, and equivalent to readingSeedSize bytes from rand, and passing them toNewKeyFromSeed.

funcSign

func Sign(privateKeyPrivateKey, message []byte) []byte

Sign signs the message with privateKey and returns a signature. It willpanic if len(privateKey) is notPrivateKeySize.

funcVerify

func Verify(publicKeyPublicKey, message, sig []byte)bool

Verify reports whether sig is a valid signature of message by publicKey. Itwill panic if len(publicKey) is notPublicKeySize.

The inputs are not considered confidential, and may leak through timing sidechannels, or if an attacker has control of part of the inputs.

funcVerifyWithOptionsadded ingo1.20

func VerifyWithOptions(publicKeyPublicKey, message, sig []byte, opts *Options)error

VerifyWithOptions reports whether sig is a valid signature of message bypublicKey. A valid signature is indicated by returning a nil error. It willpanic if len(publicKey) is notPublicKeySize.

If opts.Hash iscrypto.SHA512, the pre-hashed variant Ed25519ph is used andmessage is expected to be a SHA-512 hash, otherwise opts.Hash must becrypto.Hash(0) and the message must not be hashed, as Ed25519 performs twopasses over messages to be signed.

The inputs are not considered confidential, and may leak through timing sidechannels, or if an attacker has control of part of the inputs.

Types

typeOptionsadded ingo1.20

type Options struct {// Hash can be zero for regular Ed25519, or crypto.SHA512 for Ed25519ph.Hashcrypto.Hash// Context, if not empty, selects Ed25519ctx or provides the context string// for Ed25519ph. It can be at most 255 bytes in length.Contextstring}

Options can be used withPrivateKey.Sign orVerifyWithOptionsto select Ed25519 variants.

func (*Options)HashFuncadded ingo1.20

func (o *Options) HashFunc()crypto.Hash

HashFunc returns o.Hash.

typePrivateKey

type PrivateKey []byte

PrivateKey is the type of Ed25519 private keys. It implementscrypto.Signer.

funcNewKeyFromSeed

func NewKeyFromSeed(seed []byte)PrivateKey

NewKeyFromSeed calculates a private key from a seed. It will panic iflen(seed) is notSeedSize. This function is provided for interoperabilitywithRFC 8032.RFC 8032's private keys correspond to seeds in thispackage.

func (PrivateKey)Equaladded ingo1.15

func (privPrivateKey) Equal(xcrypto.PrivateKey)bool

Equal reports whether priv and x have the same value.

func (PrivateKey)Public

func (privPrivateKey) Public()crypto.PublicKey

Public returns thePublicKey corresponding to priv.

func (PrivateKey)Seed

func (privPrivateKey) Seed() []byte

Seed returns the private key seed corresponding to priv. It is provided forinteroperability withRFC 8032.RFC 8032's private keys correspond to seedsin this package.

func (PrivateKey)Sign

func (privPrivateKey) Sign(randio.Reader, message []byte, optscrypto.SignerOpts) (signature []byte, errerror)

Sign signs the given message with priv. rand is ignored and can be nil.

If opts.HashFunc() iscrypto.SHA512, the pre-hashed variant Ed25519ph is usedand message is expected to be a SHA-512 hash, otherwise opts.HashFunc() mustbecrypto.Hash(0) and the message must not be hashed, as Ed25519 performs twopasses over messages to be signed.

A value of typeOptions can be used as opts, or crypto.Hash(0) orcrypto.SHA512 directly to select plain Ed25519 or Ed25519ph, respectively.

typePublicKey

type PublicKey []byte

PublicKey is the type of Ed25519 public keys.

func (PublicKey)Equaladded ingo1.15

func (pubPublicKey) Equal(xcrypto.PublicKey)bool

Equal reports whether pub and x have the same value.

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp