crypto

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2020 License: GPL-3.0, GPL-3.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// NonceSize is the default NonceSize
	NonceSize = 24
)

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 DecompressPubkey

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

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

func FromECDSA

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

FromECDSA exports a private key into a binary dump.

func FromECDSAPub deprecated

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

Deprecated: Please use crypto.CompressPubkey to get []byte pubkey

func FromWIF

func FromWIF(wif string) (*ecdsa.PrivateKey, error)

Import a WIF string to get a private key

func GenerateKey

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

func GetBytes added in v1.0.9

func GetBytes(key interface{}) ([]byte, error)

func GetRandomBytes

func GetRandomBytes(len int) ([]byte, error)

GetRandomBytes returns len random looking bytes

func GetRandomNonce

func GetRandomNonce() ([]byte, error)

GetRandomNonce returns a random byte array of length NonceSize

func Hash160

func Hash160(buf []byte) []byte

Hash160 calculates the hash ripemd160(sha256(b)).

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 common.Hash)

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

func LegacyKeccak256 added in v1.0.4

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

兼容以太坊的老版本Keccak256

func LegacyKeccak256Hash added in v1.0.4

func LegacyKeccak256Hash(data ...[]byte) (h common.Hash)

兼容以太坊的老版本Keccak256

func LoadECDSA

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

LoadECDSA loads a secp256k1 private key from the given file.

func P256FromECDSAPub added in v1.0.1

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

func P256ToECDSA added in v1.0.1

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

func P256ToECDSAPub added in v1.0.1

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

func PrvKeyToWIF

func PrvKeyToWIF(prvKey *ecdsa.PrivateKey) string

func PubkeyBytesToAddress

func PubkeyBytesToAddress(pubKeyCompressBytes []byte) common.Address

This is only for P2PKH account address.

func PubkeyToAddress(p *ecdsa.PublicKey) common.Address {
	pubBytes := CompressPubkey(p)
	return PubkeyBytesToAddress(pubBytes)
}

func PubkeyBytesToAddressOutchain added in v1.0.8

func PubkeyBytesToAddressOutchain(pubKeyCompressBytes []byte) common.Address

func RequestIdToContractAddress

func RequestIdToContractAddress(deployRequestId common.Hash) common.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 ScriptHashToAddress

func ScriptHashToAddress(scriptHash []byte) common.Address

func ScriptToAddress

func ScriptToAddress(redeemScript []byte) common.Address

This is for P2SH address, start with P3

func Sign

func Sign(hash []byte, prv *ecdsa.PrivateKey) ([]byte, error)

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

func Ecrecover(hash, sig []byte) ([]byte, error) {
	pub, err := SigToPub(hash, sig)
	if err != nil {
		return nil, err
	}
	bytes := (*btcec.PublicKey)(pub).SerializeUncompressed()
	return bytes, err
}

// SigToPub returns the public key that created the given signature.

func SigToPub(hash, sig []byte) (*ecdsa.PublicKey, error) {
	// Convert to btcec input format with 'recovery id' v at the beginning.
	btcsig := make([]byte, 65)
	btcsig[0] = sig[64] + 27
	copy(btcsig[1:], sig)

	pub, _, err := btcec.RecoverCompact(btcec.S256(), btcsig, hash)
	return (*ecdsa.PublicKey)(pub), err
}

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

func Sm2ToECDSA added in v1.0.9

func Sm2ToECDSA(d []byte) (*sm2.PrivateKey, error)

func ToECDSA

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

ToECDSA creates a private key with the given D value.

func ToECDSAPub

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

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 ToWIF

func ToWIF(prvKey []byte) string

Export private key to a WIF format with compress

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 CryptoGm added in v1.0.1

type CryptoGm struct {
}

func (*CryptoGm) Decrypt added in v1.0.1

func (c *CryptoGm) Decrypt(key, ciphertext []byte) (plaintext []byte, err error)

func (*CryptoGm) Encrypt added in v1.0.1

func (c *CryptoGm) Encrypt(key []byte, plaintext []byte) (ciphertext []byte, err error)

func (*CryptoGm) GetHash added in v1.0.1

func (c *CryptoGm) GetHash() (h hash.Hash, err error)

func (*CryptoGm) Hash added in v1.0.1

func (c *CryptoGm) Hash(msg []byte) (hash []byte, err error)

func (*CryptoGm) KeyGen added in v1.0.1

func (c *CryptoGm) KeyGen() ([]byte, error)

func (*CryptoGm) PrivateKeyToInstance added in v1.0.1

func (c *CryptoGm) PrivateKeyToInstance(privKey []byte) (interface{}, error)

func (*CryptoGm) PrivateKeyToPubKey added in v1.0.1

func (c *CryptoGm) PrivateKeyToPubKey(privKey []byte) ([]byte, error)

func (*CryptoGm) Sign added in v1.0.1

func (c *CryptoGm) Sign(privKey, message []byte) (signature []byte, err error)

func (*CryptoGm) Verify added in v1.0.1

func (c *CryptoGm) Verify(pubKey, signature, message []byte) (valid bool, err error)

type CryptoP256 added in v1.0.1

type CryptoP256 struct {
}

func (*CryptoP256) Decrypt added in v1.0.1

func (c *CryptoP256) Decrypt(key, ciphertext []byte) (plaintext []byte, err error)

func (*CryptoP256) Encrypt added in v1.0.1

func (c *CryptoP256) Encrypt(key []byte, plaintext []byte) (ciphertext []byte, err error)

func (*CryptoP256) GetHash added in v1.0.1

func (c *CryptoP256) GetHash() (h hash.Hash, err error)

func (*CryptoP256) Hash added in v1.0.1

func (c *CryptoP256) Hash(msg []byte) (hash []byte, err error)

func (*CryptoP256) KeyGen added in v1.0.1

func (c *CryptoP256) KeyGen() (privKey []byte, err error)

func (*CryptoP256) PrivateKeyToInstance added in v1.0.1

func (c *CryptoP256) PrivateKeyToInstance(privKey []byte) (interface{}, error)

func (*CryptoP256) PrivateKeyToPubKey added in v1.0.1

func (c *CryptoP256) PrivateKeyToPubKey(privKey []byte) ([]byte, error)

func (*CryptoP256) Sign added in v1.0.1

func (c *CryptoP256) Sign(privKey, message []byte) (signature []byte, err error)

func (*CryptoP256) Verify added in v1.0.1

func (c *CryptoP256) Verify(pubKey, signature []byte, message []byte) (valid bool, err error)

type CryptoS256 added in v1.0.1

type CryptoS256 struct {
}

func (*CryptoS256) Decrypt added in v1.0.1

func (c *CryptoS256) Decrypt(key, ciphertext []byte) (plaintext []byte, err error)

func (*CryptoS256) Encrypt added in v1.0.1

func (c *CryptoS256) Encrypt(key []byte, plaintext []byte) (ciphertext []byte, err error)

func (*CryptoS256) GetHash added in v1.0.1

func (c *CryptoS256) GetHash() (h hash.Hash, err error)

func (*CryptoS256) Hash added in v1.0.1

func (c *CryptoS256) Hash(msg []byte) (hash []byte, err error)

func (*CryptoS256) KeyGen added in v1.0.1

func (c *CryptoS256) KeyGen() (privKey []byte, err error)

func (*CryptoS256) PrivateKeyToInstance added in v1.0.1

func (c *CryptoS256) PrivateKeyToInstance(privKey []byte) (interface{}, error)

func (*CryptoS256) PrivateKeyToPubKey added in v1.0.1

func (c *CryptoS256) PrivateKeyToPubKey(privKey []byte) ([]byte, error)

func (*CryptoS256) Sign added in v1.0.1

func (c *CryptoS256) Sign(privKey, message []byte) (signature []byte, err error)

func (*CryptoS256) Verify added in v1.0.1

func (c *CryptoS256) Verify(pubKey, signature, message []byte) (valid bool, err error)

type ECDSASignature added in v1.0.1

type ECDSASignature struct {
	R, S *big.Int
}

type ICrypto added in v1.0.1

type ICrypto interface {

	// KeyGen generates a key using opts.
	KeyGen() (privKey []byte, err error)
	PrivateKeyToPubKey(privKey []byte) (pubKey []byte, err error)
	PrivateKeyToInstance(privKey []byte) (interface{}, error)

	// Hash hashes messages msg using options opts.
	// If opts is nil, the default hash function will be used.
	Hash(msg []byte) (hash []byte, err error)

	// GetHash returns and instance of hash.Hash using options opts.
	// If opts is nil, the default hash function will be returned.
	GetHash() (h hash.Hash, err error)

	// Sign signs digest using key k.
	// The opts argument should be appropriate for the algorithm used.
	//
	// Note that when a signature of a hash of a larger message is needed,
	// the caller is responsible for hashing the larger message and passing
	// the hash (as digest).
	Sign(privKey, message []byte) (signature []byte, err error)

	// Verify verifies signature against key k and digest
	// The opts argument should be appropriate for the algorithm used.
	Verify(pubKey, signature, message []byte) (valid bool, err error)

	// Encrypt encrypts plaintext using key k.
	// The opts argument should be appropriate for the algorithm used.
	Encrypt(key []byte, plaintext []byte) (ciphertext []byte, err error)

	// Decrypt decrypts ciphertext using key k.
	// The opts argument should be appropriate for the algorithm used.
	Decrypt(key, ciphertext []byte) (plaintext []byte, err error)
}
var MyCryptoLib ICrypto = &CryptoS256{}

Directories

Path Synopsis
gmsm
sm2
crypto/x509 add sm2 support
crypto/x509 add sm2 support
sm3

Jump to

Keyboard shortcuts

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