Movatterモバイル変換


[0]ホーム

URL:


ecdsa

packagestandard library
go1.25.2Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2025 License:BSD-3-ClauseImports:19Imported by:62,192

Details

Repository

cs.opensource.google/go/go

Links

Documentation

Overview

Package ecdsa implements the Elliptic Curve Digital Signature Algorithm, asdefined inFIPS 186-5.

Signatures generated by this package are not deterministic, but entropy ismixed with the private key and the message, achieving the same level ofsecurity in case of randomness source failure.

Operations involving private keys are implemented using constant-timealgorithms, as long as anelliptic.Curve returned byelliptic.P224,elliptic.P256,elliptic.P384, orelliptic.P521 is used.

Example
package mainimport ("crypto/ecdsa""crypto/elliptic""crypto/rand""crypto/sha256""fmt")func main() {privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)if err != nil {panic(err)}msg := "hello, world"hash := sha256.Sum256([]byte(msg))sig, err := ecdsa.SignASN1(rand.Reader, privateKey, hash[:])if err != nil {panic(err)}fmt.Printf("signature: %x\n", sig)valid := ecdsa.VerifyASN1(&privateKey.PublicKey, hash[:], sig)fmt.Println("signature verified:", valid)}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

funcSign

func Sign(randio.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, errerror)

Sign signs a hash (which should be the result of hashing a larger message)using the private key, priv. If the hash is longer than the bit-length of theprivate key's curve order, the hash will be truncated to that length. Itreturns the signature as a pair of integers. Most applications should useSignASN1 instead of dealing directly with r, s.

funcSignASN1added ingo1.15

func SignASN1(randio.Reader, priv *PrivateKey, hash []byte) ([]byte,error)

SignASN1 signs a hash (which should be the result of hashing a larger message)using the private key, priv. If the hash is longer than the bit-length of theprivate key's curve order, the hash will be truncated to that length. Itreturns the ASN.1 encoded signature.

The signature is randomized. Most applications should usecrypto/rand.Readeras rand. Note that the returned signature does not depend deterministically onthe bytes read from rand, and may change between calls and/or between versions.

funcVerify

func Verify(pub *PublicKey, hash []byte, r, s *big.Int)bool

Verify verifies the signature in r, s of hash using the public key, pub. Itsreturn value records whether the signature is valid. Most applications shoulduse VerifyASN1 instead of dealing directly with r, s.

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

funcVerifyASN1added ingo1.15

func VerifyASN1(pub *PublicKey, hash, sig []byte)bool

VerifyASN1 verifies the ASN.1 encoded signature, sig, of hash using thepublic key, pub. Its return value records whether the signature is valid.

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

Types

typePrivateKey

type PrivateKey struct {PublicKey// D is the private scalar value.//// Modifying the raw value can produce invalid keys, and may// invalidate internal optimizations; moreover, [big.Int] methods are not// suitable for operating on cryptographic values. To encode and decode// PrivateKey values, use [PrivateKey.Bytes] and [ParseRawPrivateKey] or// [crypto/x509.MarshalPKCS8PrivateKey] and [crypto/x509.ParsePKCS8PrivateKey].// For ECDH, use [crypto/ecdh].//// This field will be deprecated in Go 1.26.D *big.Int}

PrivateKey represents an ECDSA private key.

funcGenerateKey

func GenerateKey(celliptic.Curve, randio.Reader) (*PrivateKey,error)

GenerateKey generates a new ECDSA private key for the specified curve.

Most applications should usecrypto/rand.Reader as rand. Note that thereturned key does not depend deterministically on the bytes read from rand,and may change between calls and/or between versions.

funcParseRawPrivateKeyadded ingo1.25.0

func ParseRawPrivateKey(curveelliptic.Curve, data []byte) (*PrivateKey,error)

ParseRawPrivateKey parses a private key encoded as a fixed-length big-endianinteger, according to SEC 1, Version 2.0, Section 2.3.6 (sometimes referredto as the raw format). It returns an error if the value is not reduced modulothe curve's order, or if it's zero.

curve must be one ofelliptic.P224,elliptic.P256,elliptic.P384, orelliptic.P521, or ParseRawPrivateKey returns an error.

ParseRawPrivateKey accepts the same format asecdh.Curve.NewPrivateKey doesfor NIST curves, but returns aPrivateKey instead of anecdh.PrivateKey.

Note that private keys are more commonly encoded in ASN.1 or PKCS#8 format,which can be parsed withcrypto/x509.ParseECPrivateKey orcrypto/x509.ParsePKCS8PrivateKey (andencoding/pem).

func (*PrivateKey)Bytesadded ingo1.25.0

func (priv *PrivateKey) Bytes() ([]byte,error)

Bytes encodes the private key as a fixed-length big-endian integer accordingto SEC 1, Version 2.0, Section 2.3.6 (sometimes referred to as the rawformat). It returns an error if the private key is invalid.

PrivateKey.Curve must be one ofelliptic.P224,elliptic.P256,elliptic.P384, orelliptic.P521, or Bytes returns an error.

Bytes returns the same format asecdh.PrivateKey.Bytes does for NIST curves.

Note that private keys are more commonly encoded in ASN.1 or PKCS#8 format,which can be generated withcrypto/x509.MarshalECPrivateKey orcrypto/x509.MarshalPKCS8PrivateKey (andencoding/pem).

func (*PrivateKey)ECDHadded ingo1.20

func (k *PrivateKey) ECDH() (*ecdh.PrivateKey,error)

ECDH returns k as aecdh.PrivateKey. It returns an error if the key isinvalid according to the definition ofecdh.Curve.NewPrivateKey, or if theCurve is not supported bycrypto/ecdh.

func (*PrivateKey)Equaladded ingo1.15

func (priv *PrivateKey) Equal(xcrypto.PrivateKey)bool

Equal reports whether priv and x have the same value.

SeePublicKey.Equal for details on how Curve is compared.

func (*PrivateKey)Publicadded ingo1.4

func (priv *PrivateKey) Public()crypto.PublicKey

Public returns the public key corresponding to priv.

func (*PrivateKey)Signadded ingo1.4

func (priv *PrivateKey) Sign(randio.Reader, digest []byte, optscrypto.SignerOpts) ([]byte,error)

Sign signs a hash (which should be the result of hashing a larger messagewith opts.HashFunc()) using the private key, priv. If the hash is longer thanthe bit-length of the private key's curve order, the hash will be truncatedto that length. It returns the ASN.1 encoded signature, likeSignASN1.

If rand is not nil, the signature is randomized. Most applications should usecrypto/rand.Reader as rand. Note that the returned signature does notdepend deterministically on the bytes read from rand, and may change betweencalls and/or between versions.

If rand is nil, Sign will produce a deterministic signature according toRFC6979. When producing a deterministic signature, opts.HashFunc() must be thefunction used to produce digest and priv.Curve must be one ofelliptic.P224,elliptic.P256,elliptic.P384, orelliptic.P521.

typePublicKey

type PublicKey struct {elliptic.Curve// X, Y are the coordinates of the public key point.//// Modifying the raw coordinates can produce invalid keys, and may// invalidate internal optimizations; moreover, [big.Int] methods are not// suitable for operating on cryptographic values. To encode and decode// PublicKey values, use [PublicKey.Bytes] and [ParseUncompressedPublicKey]// or [crypto/x509.MarshalPKIXPublicKey] and [crypto/x509.ParsePKIXPublicKey].// For ECDH, use [crypto/ecdh]. For lower-level elliptic curve operations,// use a third-party module like filippo.io/nistec.//// These fields will be deprecated in Go 1.26.X, Y *big.Int}

PublicKey represents an ECDSA public key.

funcParseUncompressedPublicKeyadded ingo1.25.0

func ParseUncompressedPublicKey(curveelliptic.Curve, data []byte) (*PublicKey,error)

ParseUncompressedPublicKey parses a public key encoded as an uncompressedpoint according to SEC 1, Version 2.0, Section 2.3.3 (also known as the X9.62uncompressed format). It returns an error if the point is not in uncompressedform, is not on the curve, or is the point at infinity.

curve must be one ofelliptic.P224,elliptic.P256,elliptic.P384, orelliptic.P521, or ParseUncompressedPublicKey returns an error.

ParseUncompressedPublicKey accepts the same format asecdh.Curve.NewPublicKey does for NIST curves, but returns aPublicKeyinstead of anecdh.PublicKey.

Note that public keys are more commonly encoded in DER (or PEM) format, whichcan be parsed withcrypto/x509.ParsePKIXPublicKey (andencoding/pem).

func (*PublicKey)Bytesadded ingo1.25.0

func (pub *PublicKey) Bytes() ([]byte,error)

Bytes encodes the public key as an uncompressed point according to SEC 1,Version 2.0, Section 2.3.3 (also known as the X9.62 uncompressed format).It returns an error if the public key is invalid.

PublicKey.Curve must be one ofelliptic.P224,elliptic.P256,elliptic.P384, orelliptic.P521, or Bytes returns an error.

Bytes returns the same format asecdh.PublicKey.Bytes does for NIST curves.

Note that public keys are more commonly encoded in DER (or PEM) format, whichcan be generated withcrypto/x509.MarshalPKIXPublicKey (andencoding/pem).

func (*PublicKey)ECDHadded ingo1.20

func (k *PublicKey) ECDH() (*ecdh.PublicKey,error)

ECDH returns k as aecdh.PublicKey. It returns an error if the key isinvalid according to the definition ofecdh.Curve.NewPublicKey, or if theCurve is not supported by crypto/ecdh.

func (*PublicKey)Equaladded ingo1.15

func (pub *PublicKey) Equal(xcrypto.PublicKey)bool

Equal reports whether pub and x have the same value.

Two keys are only considered to have the same value if they have the same Curve value.Note that for exampleelliptic.P256 and elliptic.P256().Params() are differentvalues, as the latter is a generic not constant time implementation.

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