crypto

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2019 License: Apache-2.0 Imports: 31 Imported by: 0

Documentation

Overview

Copyright © 2019 Annchain Authors <EMAIL ADDRESS>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2019 Annchain Authors <EMAIL ADDRESS>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2019 Annchain Authors <EMAIL ADDRESS>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2019 Annchain Authors <EMAIL ADDRESS>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2019 Annchain Authors <EMAIL ADDRESS>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

View Source
var CryptoNameMap = map[string]CryptoType{
	"secp256k1": CryptoTypeSecp256k1,
	"ed25519":   CryptoTypeEd25519,
}

Functions

func CRandBytes

func CRandBytes(numBytes int) []byte

This uses the OS and the Seed(s).

func CRandHex

func CRandHex(numDigits int) string

RandHex(24) gives 96 bits of randomness, strong enough for most purposes.

func CReader

func CReader() io.Reader

Returns a crand.Reader mixed with user-supplied entropy

func CompressPubkey

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

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

func CreateAddress

func CreateAddress(b common.Address, nonce uint64) 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 DecompressPubkey

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

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

func Ecrecover

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

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

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 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 LoadECDSA

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

LoadECDSA loads a secp256k1 private key from the given file.

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 PubkeyToAddress

func PubkeyToAddress(p ecdsa.PublicKey) common.Address

func Ripemd160

func Ripemd160(bytes []byte) []byte

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 Sha256

func Sha256(bytes []byte) []byte

func SigToPub

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

SigToPub returns the public key that created the given signature.

func Sign

func Sign(hash []byte, prv *ecdsa.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 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 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 CryptoType

type CryptoType int8
const (
	CryptoTypeEd25519 CryptoType = iota
	CryptoTypeSecp256k1
)

func (CryptoType) String

func (c CryptoType) String() string

type ISigner added in v0.0.2

type ISigner interface {
	GetCryptoType() CryptoType
	Sign(privKey PrivateKey, msg []byte) Signature
	PubKey(privKey PrivateKey) PublicKey
	Verify(pubKey PublicKey, signature Signature, msg []byte) bool
	RandomKeyPair() (publicKey PublicKey, privateKey PrivateKey)
	Address(pubKey PublicKey) common.Address
	AddressFromPubKeyBytes(pubKey []byte) common.Address
	Encrypt(publicKey PublicKey, m []byte) (ct []byte, err error)
	Decrypt(p PrivateKey, ct []byte) (m []byte, err error)
	PublicKeyFromBytes(b []byte) PublicKey
	CanRecoverPubFromSig() bool
}
var Signer ISigner

set this value when you code run

func NewSigner

func NewSigner(cryptoType CryptoType) ISigner

type KyberEd22519PrivKey

type KyberEd22519PrivKey struct {
	PrivateKey kyber.Scalar
	Suit       *edwards25519.SuiteEd25519
}

func (*KyberEd22519PrivKey) Decrypt

func (p *KyberEd22519PrivKey) Decrypt(cipherText []byte) (m []byte, err error)

type PrivateKey

type PrivateKey struct {
	Type  CryptoType
	Bytes []byte
}

func PrivateKeyFromBytes

func PrivateKeyFromBytes(typev CryptoType, bytes []byte) PrivateKey

func PrivateKeyFromString

func PrivateKeyFromString(value string) (priv PrivateKey, err error)

func (*PrivateKey) Decrypt

func (p *PrivateKey) Decrypt(ct []byte) (m []byte, err error)

func (*PrivateKey) PublicKey

func (p *PrivateKey) PublicKey() *PublicKey

func (*PrivateKey) String

func (k *PrivateKey) String() string

func (*PrivateKey) ToKyberEd25519PrivKey

func (p *PrivateKey) ToKyberEd25519PrivKey() *KyberEd22519PrivKey

type PublicKey

type PublicKey struct {
	Type  CryptoType
	Bytes []byte
}

func PublicKeyFromBytes

func PublicKeyFromBytes(typev CryptoType, bytes []byte) PublicKey

func PublicKeyFromString

func PublicKeyFromString(value string) (pub PublicKey, err error)

func PublicKeyFromStringWithCryptoType

func PublicKeyFromStringWithCryptoType(ct, pkstr string) (pub PublicKey, err error)

func (*PublicKey) Address

func (p *PublicKey) Address() common.Address

func (*PublicKey) Encrypt

func (p *PublicKey) Encrypt(m []byte) (ct []byte, err error)

func (*PublicKey) String

func (p *PublicKey) String() string

type PublicKeys added in v0.0.2

type PublicKeys []PublicKey

func (PublicKeys) Len added in v0.0.2

func (h PublicKeys) Len() int

func (PublicKeys) Less added in v0.0.2

func (h PublicKeys) Less(i, j int) bool

func (PublicKeys) Swap added in v0.0.2

func (h PublicKeys) Swap(i, j int)

type Signature

type Signature struct {
	Type  CryptoType
	Bytes []byte
}

func SignatureFromBytes

func SignatureFromBytes(typev CryptoType, bytes []byte) Signature

type SignerEd25519

type SignerEd25519 struct {
}

func (*SignerEd25519) Address

func (s *SignerEd25519) Address(pubKey PublicKey) common.Address

Address calculate the address from the pubkey

func (*SignerEd25519) AddressFromPubKeyBytes

func (s *SignerEd25519) AddressFromPubKeyBytes(pubKey []byte) common.Address

func (*SignerEd25519) CanRecoverPubFromSig added in v0.0.2

func (s *SignerEd25519) CanRecoverPubFromSig() bool

func (*SignerEd25519) Decrypt

func (s *SignerEd25519) Decrypt(p PrivateKey, ct []byte) (m []byte, err error)

func (*SignerEd25519) Encrypt

func (s *SignerEd25519) Encrypt(publicKey PublicKey, m []byte) (ct []byte, err error)

func (*SignerEd25519) GetCryptoType

func (s *SignerEd25519) GetCryptoType() CryptoType

func (*SignerEd25519) PubKey

func (s *SignerEd25519) PubKey(privKey PrivateKey) PublicKey

func (*SignerEd25519) PublicKeyFromBytes added in v0.0.2

func (s *SignerEd25519) PublicKeyFromBytes(b []byte) PublicKey

func (*SignerEd25519) RandomKeyPair

func (s *SignerEd25519) RandomKeyPair() (publicKey PublicKey, privateKey PrivateKey)

func (*SignerEd25519) Sign

func (s *SignerEd25519) Sign(privKey PrivateKey, msg []byte) Signature

func (*SignerEd25519) Verify

func (s *SignerEd25519) Verify(pubKey PublicKey, signature Signature, msg []byte) bool

type SignerSecp256k1

type SignerSecp256k1 struct {
}

func (*SignerSecp256k1) Address

func (s *SignerSecp256k1) Address(pubKey PublicKey) common.Address

Address calculate the address from the pubkey

func (*SignerSecp256k1) AddressFromPubKeyBytes

func (s *SignerSecp256k1) AddressFromPubKeyBytes(pubKey []byte) common.Address

func (*SignerSecp256k1) CanRecoverPubFromSig added in v0.0.2

func (s *SignerSecp256k1) CanRecoverPubFromSig() bool

func (*SignerSecp256k1) DealRecoverID

func (s *SignerSecp256k1) DealRecoverID(sig Signature) Signature

func (*SignerSecp256k1) Decrypt

func (s *SignerSecp256k1) Decrypt(p PrivateKey, ct []byte) (m []byte, err error)

func (*SignerSecp256k1) Encrypt

func (s *SignerSecp256k1) Encrypt(p PublicKey, m []byte) (ct []byte, err error)

func (*SignerSecp256k1) GetCryptoType

func (s *SignerSecp256k1) GetCryptoType() CryptoType

func (*SignerSecp256k1) PubKey

func (s *SignerSecp256k1) PubKey(privKey PrivateKey) PublicKey

func (*SignerSecp256k1) PublicKeyFromBytes added in v0.0.2

func (s *SignerSecp256k1) PublicKeyFromBytes(b []byte) PublicKey

func (*SignerSecp256k1) RandomKeyPair

func (s *SignerSecp256k1) RandomKeyPair() (publicKey PublicKey, privateKey PrivateKey)

func (*SignerSecp256k1) Sign

func (s *SignerSecp256k1) Sign(privKey PrivateKey, msg []byte) Signature

func (*SignerSecp256k1) Verify

func (s *SignerSecp256k1) Verify(pubKey PublicKey, signature Signature, msg []byte) bool

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