crypto

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2022 License: Apache-2.0 Imports: 9 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// PublicKeyLenCompressed is the byte length of a compressed public key
	PublicKeyLenCompressed = 33
	// PublicKeyLenUncompressed is the byte length of an uncompressed public key
	PublicKeyLenUncompressed = 65
)
View Source
const (
	// SignatureLenRawWithV is the bytes length of signature including V value
	SignatureLenRawWithV = 65
	// SignatureLenRaw is the bytes length of signature not including V value
	SignatureLenRaw = 64

	// HashLen is the bytes length of hash for signature
	HashLen = 32
)
View Source
const (
	// PrivateKeyLen is the byte length of a private key
	PrivateKeyLen = 32
)

Variables

This section is empty.

Functions

func GenerateKeyPair

func GenerateKeyPair() (privKey *PrivateKey, pubKey *PublicKey)

GenerateKeyPair generates a private and public key pair.

func HKDF

func HKDF(secret, salt, info []byte, keyLen int) ([]byte, error)

func SHA3Sum256

func SHA3Sum256(m []byte) []byte

SHA3Sum256 returns the SHA3-256 digest of the data

func SHASum256

func SHASum256(m []byte) []byte

SHASum256 returns the SHA256 digest of the data

Types

type PrivateKey

type PrivateKey struct {
	// contains filtered or unexported fields
}

PrivateKey is a type representing a private key. TODO private key always includes public key? or create KeyPair struct for both private key and public key

func ParsePrivateKey

func ParsePrivateKey(b []byte) (*PrivateKey, error)

ParsePublicKey parse private key and return private key object.

func (*PrivateKey) Bytes

func (key *PrivateKey) Bytes() []byte

Bytes returns bytes form of private key.

func (*PrivateKey) ECDH

func (key *PrivateKey) ECDH(pubKey *PublicKey) []byte

func (*PrivateKey) PublicKey

func (key *PrivateKey) PublicKey() *PublicKey

PublicKey generates a public key paired with itself.

func (*PrivateKey) String

func (key *PrivateKey) String() string

String returns the string representation.

type PublicKey

type PublicKey struct {
	// contains filtered or unexported fields
}

PublicKey is a type representing a public key, which can be serialized to or deserialized from compressed or uncompressed formats.

func ParsePublicKey

func ParsePublicKey(pubKey []byte) (*PublicKey, error)

ParsePublicKey parses the public key into a PublicKey instance. It supports uncompressed and compressed formats. NOTE: For the efficiency, it may use the slice directly. So don't change any internal value of the public key

func (*PublicKey) Equal

func (key *PublicKey) Equal(key2 *PublicKey) bool

Equal returns true if the given public key is same as this instance semantically

func (*PublicKey) SerializeCompressed

func (key *PublicKey) SerializeCompressed() []byte

SerializeCompressed serializes the public key in a 33-byte compressed format. For the efficiency, it returns the slice internally used, so don't change any internal value in the returned slice.

func (*PublicKey) SerializeUncompressed

func (key *PublicKey) SerializeUncompressed() []byte

SerializeUncompressed serializes the public key in a 65-byte uncompressed format.

func (*PublicKey) String

func (key *PublicKey) String() string

String returns the string representation.

type Signature

type Signature struct {
	// contains filtered or unexported fields
}

Signature is a type representing an ECDSA signature with or without V.

func NewSignature

func NewSignature(hash []byte, privKey *PrivateKey) (*Signature, error)

NewSignature calculates an ECDSA signature including V, which is 0 or 1.

func ParseSignature

func ParseSignature(sig []byte) (*Signature, error)

ParseSignature parses a signature from the raw byte array of 64([R|S]) or 65([R|S|V]) bytes long. If a source signature is formatted as [V|R|S], call ParseSignatureVRS instead. NOTE: For the efficiency, it may use the slice directly. So don't change any internal value of the signature.

func ParseSignatureVRS

func ParseSignatureVRS(sig []byte) (*Signature, error)

ParseSignatureVRS parses a signature from the [V|R|S] formatted signature. If the format of a source signature is different, call ParseSignature instead.

func (*Signature) HasV

func (sig *Signature) HasV() bool

HasV returns whether the signature has V value.

func (*Signature) RecoverPublicKey

func (sig *Signature) RecoverPublicKey(hash []byte) (*PublicKey, error)

RecoverPublicKey recovers a public key from the hash of message and its signature.

func (*Signature) SerializeRS

func (sig *Signature) SerializeRS() ([]byte, error)

SerializeRS returns the 64-byte data formatted as [R|S] from the signature. For the efficiency, it returns the slice internally used, so don't change any internal value in the returned slice.

func (*Signature) SerializeRSV

func (sig *Signature) SerializeRSV() ([]byte, error)

SerializeRSV returns the 65-byte data formatted as [R|S|V] from the signature. Make sure that it has a valid V value. If it doesn't have V value, then it will throw error. For the efficiency, it returns the slice internally used, so don't change any internal value in the returned slice.

func (*Signature) SerializeVRS

func (sig *Signature) SerializeVRS() ([]byte, error)

SerializeVRS returns the 65-byte data formatted as [V|R|S] from the signature. Make sure that it has a valid V value. If it doesn't have V value, then it will throw error. For the efficiency, it returns the slice internally used, so don't change any internal value in the returned slice.

func (*Signature) String

func (sig *Signature) String() string

String returns the string representation.

func (*Signature) Verify

func (sig *Signature) Verify(msg []byte, pubKey *PublicKey) bool

Verify verifies the signature of hash using the public key.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL