crypto

package
v0.0.0-...-4dde864 Latest Latest
Warning

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

Go to latest
Published: May 15, 2020 License: LGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HashLength    = 32
	AddressLength = 20
)

Variables

View Source
var (
	ErrExceedHashLength = errors.New("bytes length exceed maximum hash length of 32")

	ZeroAddress = CommonAddress{}
	ZeroHash    = Hash{}
)

Functions

func CompressPubkey

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

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

func DecompressPubkey

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

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

func Ecrecover

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

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

func GenerateKey

func GenerateKey(rand io.Reader) (prv *secp256k1.PrivateKey, err error)

Generate an elliptic curve public / private keypair. If params is nil, the recommended default parameters for the key will be chosen.

func IsHexAddress

func IsHexAddress(s string) bool

IsHexAddress verifies whether a string can represent a valid hex-encoded Ethereum address or not.

func LoadECDSA

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

LoadECDSA loads a secp256k1 private key from the given file.

func SaveECDSA

func SaveECDSA(file string, key *secp256k1.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) (*secp256k1.PublicKey, error)

SigToPub returns the public key that created the given signature.

func Sign

func Sign(hash []byte, prv *secp256k1.PrivateKey) ([]byte, 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 hash 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 ToPrivateKey

func ToPrivateKey(d []byte) (*secp256k1.PrivateKey, error)

ToECDSA creates a private key with the given D value.

func ValidateSignatureValues

func ValidateSignatureValues(v byte, r, s *big.Int, homestead bool) bool

ValidateSignatureValues verifies whether the signature values are valid with the given chain rules. The v value is assumed to be either 0 or 1.

func VerifySignature

func VerifySignature(pubkey, hash, signature []byte) bool

VerifySignature checks that the given public key created signature over hash. The public key should be in compressed (33 bytes) or uncompressed (65 bytes) format. The signature should have the 64 byte [R || S] format.

Types

type ByteCode

type ByteCode []byte

type CommonAddress

type CommonAddress [AddressLength]byte

Address represents the 20 byte address of an Ethereum account.

func BigToAddress

func BigToAddress(b *big.Int) CommonAddress

BigToAddress returns Address with byte values of b. If b is larger than len(h), b will be cropped from the left.

func BytesToAddress

func BytesToAddress(b []byte) CommonAddress

BytesToAddress returns Address with value b. If b is larger than len(h), b will be cropped from the left.

func CreateAddress

func CreateAddress(b CommonAddress, nonce uint64) CommonAddress

CreateAddress creates an ethereum address given the bytes and the nonce

func CreateAddress2

func CreateAddress2(b CommonAddress, salt [32]byte, code []byte) CommonAddress

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

func HexToAddress

func HexToAddress(s string) CommonAddress

HexToAddress returns Address with byte values of s. If s is larger than len(h), s will be cropped from the left.

func PubkeyToAddress

func PubkeyToAddress(p *secp256k1.PublicKey) CommonAddress

func (CommonAddress) Big

func (a CommonAddress) Big() *big.Int

String implements fmt.Stringer.

func (CommonAddress) Bytes

func (a CommonAddress) Bytes() []byte

Bytes gets the string representation of the underlying address.

func (CommonAddress) Format

func (a CommonAddress) Format(s fmt.State, c rune)

Format implements fmt.Formatter, forcing the byte slice to be formatted as is, without going through the stringer interface used for logging.

func (CommonAddress) Hash

func (a CommonAddress) Hash() Hash

Hash converts an address to a hash by left-padding it with zeros.

func (CommonAddress) Hex

func (a CommonAddress) Hex() string

Hex returns an EIP55-compliant hex string representation of the address.

func (CommonAddress) IsEmpty

func (a CommonAddress) IsEmpty() bool

String implements fmt.Stringer.

func (CommonAddress) MarshalText

func (a CommonAddress) MarshalText() ([]byte, error)

MarshalText returns the hex representation of a.

func (*CommonAddress) SetBytes

func (a *CommonAddress) SetBytes(b []byte)

SetBytes sets the address to the value of b. If b is larger than len(a) it will panic.

func (CommonAddress) String

func (a CommonAddress) String() string

String implements fmt.Stringer.

func (*CommonAddress) UnmarshalJSON

func (a *CommonAddress) UnmarshalJSON(input []byte) error

UnmarshalJSON parses a hash in hex syntax.

func (*CommonAddress) UnmarshalText

func (a *CommonAddress) UnmarshalText(input []byte) error

UnmarshalText parses a hash in hex syntax.

type Hash

type Hash [HashLength]byte

func Big2Hash

func Big2Hash(x *big.Int) Hash

func BigToHash

func BigToHash(b *big.Int) Hash

BigToHash sets byte representation of b to hash. If b is larger than len(h), b will be cropped from the left.

func Bytes2Hash

func Bytes2Hash(b []byte) Hash

func BytesToHash

func BytesToHash(b []byte) Hash

BytesToHash sets b to hash. If b is larger than len(h), b will be cropped from the left.

func GetByteCodeHash

func GetByteCodeHash(byteCode ByteCode) Hash

func HexToHash

func HexToHash(s string) Hash

HexToHash sets byte representation of s to hash. If b is larger than len(h), b will be cropped from the left.

func Keccak256Hash

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

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

func RandomHash

func RandomHash() Hash

func (Hash) Big

func (h Hash) Big() *big.Int

Big converts a hash to a big integer.

func (Hash) Bytes

func (h Hash) Bytes() []byte

func (*Hash) IsEmpty

func (hash *Hash) IsEmpty() bool

func (*Hash) IsEqual

func (hash *Hash) IsEqual(target *Hash) bool

IsEqual returns true if target is the same as hash.

func (Hash) MarshalText

func (h Hash) MarshalText() ([]byte, error)

MarshalText returns the hex representation of h.

func (*Hash) SetBytes

func (h *Hash) SetBytes(b []byte)

func (Hash) String

func (h Hash) String() string

func (*Hash) UnmarshalJSON

func (h *Hash) UnmarshalJSON(input []byte) error

UnmarshalJSON parses a hash in hex syntax.

func (*Hash) UnmarshalText

func (h *Hash) UnmarshalText(input []byte) error

UnmarshalText parses a hash in hex syntax.

Directories

Path Synopsis
Package bn256 implements the Optimal Ate pairing over a 256-bit Barreto-Naehrig curve.
Package bn256 implements the Optimal Ate pairing over a 256-bit Barreto-Naehrig curve.
cloudflare
Package bn256 implements a particular bilinear group at the 128-bit security level.
Package bn256 implements a particular bilinear group at the 128-bit security level.
google
Package bn256 implements a particular bilinear group.
Package bn256 implements a particular bilinear group.
Package secp256k1 implements support for the elliptic curves needed for decred.
Package secp256k1 implements support for the elliptic curves needed for decred.
Package sha3 implements the SHA-3 fixed-output-length hash functions and the SHAKE variable-output-length hash functions defined by FIPS-202.
Package sha3 implements the SHA-3 fixed-output-length hash functions and the SHAKE variable-output-length hash functions defined by FIPS-202.

Jump to

Keyboard shortcuts

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