crypto

package
v0.0.0-...-ce7d804 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2020 License: LGPL-3.0 Imports: 24 Imported by: 1

Documentation

Overview

Ripemd160

sum := crypto.Ripemd160([]byte("This is blockchain"))
fmt.Printf("%x\n", sum)

Index

Constants

View Source
const (
	CryptoTypeGM        = "gm"
	CryptoTypeEd25519   = "ed25519"
	CryptoTypeSecp256K1 = "secp256k1"
)
View Source
const PubKeyEd25519Size = 32
View Source
const SM2Magic = 0x8
View Source
const SignatureEd25519Size = 64
View Source
const SignatureGMSize = 65
View Source
const Version = "0.9.0-dev"

Variables

View Source
var (
	// ErrInvalidCryptoType --
	ErrInvalidCryptoType = errors.New("Invalid Crypto Type")
)

Functions

func CRandBytes

func CRandBytes(numBytes int) []byte

This uses the OS and the Seed(s).

func CRandHex

func CRandHex(numDigits int) string

CRandHex returns a hex encoded string that's floor(numDigits/2) * 2 long.

Note: CRandHex(24) gives 96 bits of randomness that are usually strong enough for most purposes.

func CReader

func CReader() io.Reader

Returns a crand.Reader mixed with user-supplied entropy

func CheckPriKey

func CheckPriKey(pubKey, priKey string) (bool, error)

func CreateAddress

func CreateAddress(b common.Address, nonce uint64, payload []byte) common.Address

CreateAddress creates an ethereum address given the bytes and the nonce

func CreateAddress2

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

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

func CreateContractAddress

func CreateContractAddress(b common.Address, nonce uint64, payload []byte, rand int) common.Address

CreateContractAddress creates an ethereum address given the bytes and the nonce and the random number

func Ecrecover

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

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

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 common.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 LocalAccountType

func LocalAccountType() string

LocalAccountType --

func LocalNodeType

func LocalNodeType() string

func MixEntropy

func MixEntropy(seedBytes []byte)

Mix additional bytes of randomness, e.g. from hardware, user-input, etc. It is OK to call it multiple times. It does not diminish security.

func ParseSignatureGM

func ParseSignatureGM(sig []byte) (r, s, v *big.Int, ok bool)

ParseSignatureGM --

func PubkeyToAddress

func PubkeyToAddress(pubKey PubKey) common.Address

PubkeyToAddress --

func RegisterAmino

func RegisterAmino()

RegisterAmino registers all crypto related types in the given (amino) codec.

func Ripemd160

func Ripemd160(bytes []byte) []byte

func S256

func S256() elliptic.Curve

S256 returns an instance of the secp256k1 curve.

func Sender

func Sender(hash, sig []byte) (common.Address, error)

Sender : Ecrecover sender's Address

func SetLocalAccountType

func SetLocalAccountType(t string)

SetLocalAccountType --

func SetLocalNodeType

func SetLocalNodeType(t string)

SetLocalNodeType --

func Sha256

func Sha256(bytes []byte) []byte

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 pubkey created signature over message. The signature should be in [R || S || V] format.

Types

type Address

type Address = cmn.HexBytes

An address is a []byte, but hex-encoded even in JSON. []byte leaves us the option to change the address length. Use an alias so Unmarshal methods (with ptr receivers) are available too.

type PrivKey

type PrivKey interface {
	Bytes() []byte
	Sign(msg []byte) (Signature, error)
	PubKey() PubKey
	Equals(PrivKey) bool
	Reset()
	Raw() []byte
	Type() string
}

func GenerateAccountKey

func GenerateAccountKey() (PrivKey, error)

GenerateAccountKey --

func GenerateNodeKey

func GenerateNodeKey() (PrivKey, error)

GenerateNodeKey --

func GeneratePrivKeyFromSecret

func GeneratePrivKeyFromSecret(secret []byte, t string) (PrivKey, error)

GeneratePrivKeyFromSecret --

func HexToPrivkey

func HexToPrivkey(priv string) (PrivKey, error)

func PrivKeyFromBytes

func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error)

type PrivKeyEd25519

type PrivKeyEd25519 [64]byte

Implements PrivKey

func GenPrivKeyEd25519

func GenPrivKeyEd25519() (PrivKeyEd25519, error)

func GenPrivKeyEd25519FromSecret

func GenPrivKeyEd25519FromSecret(secret []byte) (PrivKeyEd25519, error)

NOTE: secret should be the output of a KDF like bcrypt, if it's derived from user input.

func (PrivKeyEd25519) Bytes

func (privKey PrivKeyEd25519) Bytes() []byte

func (PrivKeyEd25519) Equals

func (privKey PrivKeyEd25519) Equals(other PrivKey) bool

Equals - you probably don't need to use this. Runs in constant time based on length of the keys.

func (PrivKeyEd25519) Generate

func (privKey PrivKeyEd25519) Generate(index int) PrivKeyEd25519

Deterministically generates new priv-key bytes from key.

func (PrivKeyEd25519) MarshalJSON

func (privKey PrivKeyEd25519) MarshalJSON() ([]byte, error)

func (PrivKeyEd25519) PubKey

func (privKey PrivKeyEd25519) PubKey() PubKey

func (PrivKeyEd25519) Raw

func (privKey PrivKeyEd25519) Raw() []byte

Raw --

func (PrivKeyEd25519) Reset

func (privKey PrivKeyEd25519) Reset()

Reset --

func (PrivKeyEd25519) Sign

func (privKey PrivKeyEd25519) Sign(msg []byte) (Signature, error)

func (PrivKeyEd25519) Type

func (privKey PrivKeyEd25519) Type() string

func (*PrivKeyEd25519) UnmarshalJSON

func (privKey *PrivKeyEd25519) UnmarshalJSON(input []byte) error

type PrivKeyGM

type PrivKeyGM struct {
	Db []byte
	// contains filtered or unexported fields
}

PrivKeyGM --

func GenPrivKeyGM

func GenPrivKeyGM() (*PrivKeyGM, error)

GenPrivKeyGM --

func GenPrivKeyGMFromSecret

func GenPrivKeyGMFromSecret(secret []byte) (*PrivKeyGM, error)

GenPrivKeyGMFromSecret --

func (*PrivKeyGM) Bytes

func (privKey *PrivKeyGM) Bytes() []byte

Bytes --

func (*PrivKeyGM) Equals

func (privKey *PrivKeyGM) Equals(other PrivKey) bool

Equals --

func (*PrivKeyGM) PubKey

func (privKey *PrivKeyGM) PubKey() PubKey

PubKey --

func (*PrivKeyGM) Raw

func (privKey *PrivKeyGM) Raw() []byte

Raw --

func (*PrivKeyGM) Reset

func (privKey *PrivKeyGM) Reset()

Reset --

func (*PrivKeyGM) Sign

func (privKey *PrivKeyGM) Sign(msg []byte) (Signature, error)

Sign --

func (*PrivKeyGM) Type

func (privKey *PrivKeyGM) Type() string

Type --

type PrivKeySecp256k1

type PrivKeySecp256k1 struct {
	Data []byte
	// contains filtered or unexported fields
}

func GenPrivKeySecp256k1

func GenPrivKeySecp256k1() (*PrivKeySecp256k1, error)

func GenPrivKeySecp256k1FromSecret

func GenPrivKeySecp256k1FromSecret(secret []byte) (*PrivKeySecp256k1, error)

NOTE: secret should be the output of a KDF like bcrypt, if it's derived from user input.

func (*PrivKeySecp256k1) Bytes

func (privKey *PrivKeySecp256k1) Bytes() []byte

Bytes --

func (*PrivKeySecp256k1) Equals

func (privKey *PrivKeySecp256k1) Equals(other PrivKey) bool

Equals - you probably don't need to use this. Runs in constant time based on length of the keys.

func (*PrivKeySecp256k1) PubKey

func (privKey *PrivKeySecp256k1) PubKey() PubKey

PubKey --

func (*PrivKeySecp256k1) Raw

func (privKey *PrivKeySecp256k1) Raw() []byte

Raw --

func (*PrivKeySecp256k1) Reset

func (privKey *PrivKeySecp256k1) Reset()

Reset --

func (*PrivKeySecp256k1) Sign

func (privKey *PrivKeySecp256k1) Sign(hash []byte) (Signature, 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 (*PrivKeySecp256k1) Type

func (privKey *PrivKeySecp256k1) Type() string

Type --

type PubKey

type PubKey interface {
	Address() Address
	Bytes() []byte
	VerifyBytes(msg []byte, sig Signature) bool
	Equals(PubKey) bool
	Raw() []byte
}

func HexToPubkey

func HexToPubkey(pub string) (PubKey, error)

func PubKeyFromBytes

func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error)

type PubKeyEd25519

type PubKeyEd25519 [PubKeyEd25519Size]byte

Implements PubKeyInner

func (PubKeyEd25519) Address

func (pubKey PubKeyEd25519) Address() Address

Address is the Ripemd160 of the raw pubkey bytes.

func (PubKeyEd25519) Bytes

func (pubKey PubKeyEd25519) Bytes() []byte

func (PubKeyEd25519) Equals

func (pubKey PubKeyEd25519) Equals(other PubKey) bool

func (PubKeyEd25519) MarshalJSON

func (pubKey PubKeyEd25519) MarshalJSON() ([]byte, error)

func (PubKeyEd25519) Raw

func (pubKey PubKeyEd25519) Raw() []byte

Raw --

func (PubKeyEd25519) String

func (pubKey PubKeyEd25519) String() string

func (*PubKeyEd25519) UnmarshalJSON

func (pubKey *PubKeyEd25519) UnmarshalJSON(input []byte) error

func (PubKeyEd25519) VerifyBytes

func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool

type PubKeyGM

type PubKeyGM struct {
	Data []byte
	// contains filtered or unexported fields
}

PubKeyGM --

func PubKeyGMFromBytes

func PubKeyGMFromBytes(data []byte) *PubKeyGM

PubKeyGMFromBytes --

func (*PubKeyGM) Address

func (pubKey *PubKeyGM) Address() Address

Address --

func (*PubKeyGM) Bytes

func (pubKey *PubKeyGM) Bytes() []byte

Bytes --

func (*PubKeyGM) Equals

func (pubKey *PubKeyGM) Equals(other PubKey) bool

Equals --

func (*PubKeyGM) Raw

func (pubKey *PubKeyGM) Raw() []byte

Raw --

func (*PubKeyGM) VerifyBytes

func (pubKey *PubKeyGM) VerifyBytes(msg []byte, sig Signature) bool

VerifyBytes --

type PubKeySecp256k1

type PubKeySecp256k1 struct {
	Data []byte
	// contains filtered or unexported fields
}

Implements PubKey. Compressed pubkey (just the x-cord), prefixed with 0x02 or 0x03, depending on the y-cord. type PubKeySecp256k1 [PubKeySecp256k1Size]byte

func PubKeySecp256k1FromBytes

func PubKeySecp256k1FromBytes(raw []byte) *PubKeySecp256k1

PubKeySecp256k1FromBytes --

func (*PubKeySecp256k1) Address

func (pubKey *PubKeySecp256k1) Address() Address

Address --

func (*PubKeySecp256k1) Bytes

func (pubKey *PubKeySecp256k1) Bytes() []byte

Bytes --

func (*PubKeySecp256k1) Equals

func (pubKey *PubKeySecp256k1) Equals(other PubKey) bool

Equals --

func (*PubKeySecp256k1) Raw

func (pubKey *PubKeySecp256k1) Raw() []byte

Raw --

func (*PubKeySecp256k1) String

func (pubKey *PubKeySecp256k1) String() string

func (*PubKeySecp256k1) VerifyBytes

func (pubKey *PubKeySecp256k1) VerifyBytes(msg []byte, sig Signature) bool

VerifyBytes -- 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.

type Signature

type Signature interface {
	Bytes() []byte
	IsZero() bool
	Equals(Signature) bool
	Raw() []byte
}

func NewSignatureEd25519

func NewSignatureEd25519(data []byte) Signature

func NewSignatureGM

func NewSignatureGM(data []byte) (Signature, bool)

NewSignatureGM --

func NewSignatureSecp256k1

func NewSignatureSecp256k1(data []byte) Signature

func SignatureFromBytes

func SignatureFromBytes(signBytes []byte) (sign Signature, err error)

type SignatureEd25519

type SignatureEd25519 [SignatureEd25519Size]byte

Implements Signature

func (SignatureEd25519) Bytes

func (sig SignatureEd25519) Bytes() []byte

func (SignatureEd25519) Equals

func (sig SignatureEd25519) Equals(other Signature) bool

func (SignatureEd25519) IsZero

func (sig SignatureEd25519) IsZero() bool

func (SignatureEd25519) Raw

func (sig SignatureEd25519) Raw() []byte

Raw --

func (SignatureEd25519) String

func (sig SignatureEd25519) String() string

type SignatureGM

type SignatureGM [SignatureGMSize]byte

SignatureGM --

func (SignatureGM) Bytes

func (sig SignatureGM) Bytes() []byte

Bytes --

func (SignatureGM) Equals

func (sig SignatureGM) Equals(other Signature) bool

Equals --

func (SignatureGM) IsZero

func (sig SignatureGM) IsZero() bool

IsZero --

func (SignatureGM) Raw

func (sig SignatureGM) Raw() []byte

Raw --

type SignatureSecp256k1

type SignatureSecp256k1 []byte

Implements Signature

func (SignatureSecp256k1) Bytes

func (sig SignatureSecp256k1) Bytes() []byte

func (SignatureSecp256k1) Equals

func (sig SignatureSecp256k1) Equals(other Signature) bool

func (SignatureSecp256k1) IsZero

func (sig SignatureSecp256k1) IsZero() bool

func (SignatureSecp256k1) Raw

func (sig SignatureSecp256k1) Raw() []byte

Raw --

func (SignatureSecp256k1) String

func (sig SignatureSecp256k1) String() string

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 merkle computes a deterministic minimal height Merkle tree hash.
Package merkle computes a deterministic minimal height Merkle tree hash.
Package secp256k1 wraps the bitcoin secp256k1 C library.
Package secp256k1 wraps the bitcoin secp256k1 C library.

Jump to

Keyboard shortcuts

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