crypto

package
v0.110.3 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2022 License: MPL-2.0 Imports: 25 Imported by: 13

Documentation

Index

Constants

View Source
const DigestLength = 32

DigestLength sets the signature digest exact length

View Source
const RecoveryIDOffset = 64

RecoveryIDOffset points to the byte offset within the signature that contains the recovery id.

View Source
const SignatureLength = 64 + 1 // 64 bytes ECDSA signature + 1 byte recovery id

SignatureLength indicates the byte length required to carry a signature with recovery id.

Variables

This section is empty.

Functions

func CompressPubkey

func CompressPubkey(pubkey *ecdsa.PublicKey) []byte

CompressPubkey encodes a public key to the 33-byte compressed format.

func CreateAddress

func CreateAddress(b types.Address, nonce uint64) types.Address

CreateAddress creates an ethereum address given the bytes and the nonce

func CreateAddress2

func CreateAddress2(b types.Address, salt [32]byte, inithash []byte) types.Address

CreateAddress2 creates an ethereum address given the address bytes, initial contract code hash and a salt.

func DecompressPubkey

func DecompressPubkey(pubkey []byte) (*ecdsa.PublicKey, error)

DecompressPubkey parses a public key in the 33-byte compressed format.

func DecryptSymmetric

func DecryptSymmetric(key []byte, cyphertext []byte) ([]byte, error)

func EcRecover

func EcRecover(ctx context.Context, data types.HexBytes, sig types.HexBytes) (types.Address, error)

func Ecrecover

func Ecrecover(hash, sig []byte) ([]byte, error)

Ecrecover returns the uncompressed public key that created the given signature.

func EncryptSymmetric

func EncryptSymmetric(key, plaintext []byte) ([]byte, error)

func ExtractSignature

func ExtractSignature(data, signature []byte) (*ecdsa.PublicKey, error)

ExtractSignature returns a public key for a given data and signature.

func ExtractSignatures

func ExtractSignatures(signaturePairs [][2]string) ([]string, error)

ExtractSignatures extract from tuples of signatures content a public key DEPRECATED: use ExtractSignature

func FromECDSA

func FromECDSA(priv *ecdsa.PrivateKey) []byte

FromECDSA exports a private key into a binary dump.

func FromECDSAPub

func FromECDSAPub(pub *ecdsa.PublicKey) []byte

func GenerateKey

func GenerateKey() (*ecdsa.PrivateKey, error)

func HexToECDSA

func HexToECDSA(hexkey string) (*ecdsa.PrivateKey, error)

HexToECDSA parses a secp256k1 private key.

func Keccak256

func Keccak256(data ...[]byte) []byte

Keccak256 calculates and returns the Keccak256 hash of the input data.

func Keccak256Hash

func Keccak256Hash(data ...[]byte) (h types.Hash)

Keccak256Hash calculates and returns the Keccak256 hash of the input data, converting it to an internal Hash data structure.

func Keccak512

func Keccak512(data ...[]byte) []byte

Keccak512 calculates and returns the Keccak512 hash of the input data.

func LoadECDSA

func LoadECDSA(file string) (*ecdsa.PrivateKey, error)

LoadECDSA loads a secp256k1 private key from the given file.

func PubkeyToAddress

func PubkeyToAddress(p ecdsa.PublicKey) types.Address

func S256

func S256() elliptic.Curve

S256 returns an instance of the secp256k1 curve.

func SaveECDSA

func SaveECDSA(file string, key *ecdsa.PrivateKey) error

SaveECDSA saves a secp256k1 private key to the given file with restrictive permissions. The key data is saved hex-encoded.

func SigToPub

func SigToPub(hash, sig []byte) (*ecdsa.PublicKey, error)

SigToPub returns the public key that created the given signature.

func Sign

func Sign(digestHash []byte, prv *ecdsa.PrivateKey) (sig []byte, err error)

Sign calculates an ECDSA signature.

This function is susceptible to chosen plaintext attacks that can leak information about the private key that is used for signing. Callers must be aware that the given digest cannot be chosen by an adversery. Common solution is to hash any input before calculating the signature.

The produced signature is in the [R || S || V] format where V is 0 or 1.

func SignBytes

func SignBytes(data []byte, prv *ecdsa.PrivateKey) (sig []byte, err error)

SignBytes signs the hash of arbitrary data.

func SignBytesAsHex

func SignBytesAsHex(data []byte, identity *ecdsa.PrivateKey) (string, error)

SignBytesAsHex signs the Keccak256 hash of arbitrary data and returns its hex representation.

func SignStringAsHex

func SignStringAsHex(data string, identity *ecdsa.PrivateKey) (string, error)

SignStringAsHex signs the Keccak256 hash of arbitrary string and returns its hex representation.

func TextAndHash

func TextAndHash(data []byte) ([]byte, string)

TextAndHash is a helper function that calculates a hash for the given message that can be safely used to calculate a signature from.

The hash is calulcated as

keccak256("\x19Ethereum Signed Message:\n"${message length}${message}).

This gives context to the signed message and prevents signing of transactions.

func TextHash

func TextHash(data []byte) []byte

TextHash is a helper function that calculates a hash for the given message that can be safely used to calculate a signature from.

The hash is calulcated as

keccak256("\x19Ethereum Signed Message:\n"${message length}${message}).

This gives context to the signed message and prevents signing of transactions.

func ToECDSA

func ToECDSA(d []byte) (*ecdsa.PrivateKey, error)

ToECDSA creates a private key with the given D value.

func ToECDSAUnsafe

func ToECDSAUnsafe(d []byte) *ecdsa.PrivateKey

ToECDSAUnsafe blindly converts a binary blob to a private key. It should almost never be used unless you are sure the input is valid and want to avoid hitting errors due to bad origin encoding (0 prefixes cut off).

func UnmarshalPubkey

func UnmarshalPubkey(pub []byte) (*ecdsa.PublicKey, error)

UnmarshalPubkey converts bytes to a secp256k1 public key.

func VerifySignatures

func VerifySignatures(signaturePairs [][3]string) error

VerifySignatures verifies tuples of signatures content/hash/public key

Types

type DHPair

type DHPair struct {
	PrvKey dr.Key
	PubKey dr.Key
}

func (DHPair) PrivateKey

func (p DHPair) PrivateKey() dr.Key

func (DHPair) PublicKey

func (p DHPair) PublicKey() dr.Key

type EthereumCrypto

type EthereumCrypto struct{}

EthereumCrypto is an implementation of Crypto with cryptographic primitives recommended by the Double Ratchet Algorithm specification. However, some details are different, see function comments for details.

func (EthereumCrypto) DH

func (c EthereumCrypto) DH(dhPair dr.DHPair, dhPub dr.Key) (dr.Key, error)

See the Crypto interface.

func (EthereumCrypto) Decrypt

func (c EthereumCrypto) Decrypt(mk dr.Key, authCiphertext, ad []byte) ([]byte, error)

See the Crypto interface.

func (EthereumCrypto) Encrypt

func (c EthereumCrypto) Encrypt(mk dr.Key, plaintext, ad []byte) ([]byte, error)

Encrypt uses a slightly different approach than in the algorithm specification: it uses AES-256-CTR instead of AES-256-CBC for security, ciphertext length and implementation complexity considerations.

func (EthereumCrypto) GenerateDH

func (c EthereumCrypto) GenerateDH() (dr.DHPair, error)

See the Crypto interface.

func (EthereumCrypto) KdfCK

func (c EthereumCrypto) KdfCK(ck dr.Key) (dr.Key, dr.Key)

See the Crypto interface.

func (EthereumCrypto) KdfRK

func (c EthereumCrypto) KdfRK(rk, dhOut dr.Key) (dr.Key, dr.Key, dr.Key)

See the Crypto interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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