mirror of
https://github.com/tendermint/tendermint.git
synced 2026-01-03 11:45:18 +00:00
77 lines
2.0 KiB
Go
77 lines
2.0 KiB
Go
package crypto
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
|
|
"github.com/tendermint/tendermint/internal/jsontypes"
|
|
"github.com/tendermint/tendermint/libs/bytes"
|
|
)
|
|
|
|
const (
|
|
// HashSize is the size in bytes of an AddressHash.
|
|
HashSize = sha256.Size
|
|
|
|
// AddressSize is the size of a pubkey address.
|
|
AddressSize = 20
|
|
)
|
|
|
|
// 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 Address = bytes.HexBytes
|
|
|
|
// AddressHash computes a truncated SHA-256 hash of bz for use as
|
|
// a peer address.
|
|
//
|
|
// See: https://docs.tendermint.com/master/spec/core/data_structures.html#address
|
|
func AddressHash(bz []byte) Address {
|
|
h := sha256.Sum256(bz)
|
|
return Address(h[:AddressSize])
|
|
}
|
|
|
|
// Checksum returns the SHA256 of the bz.
|
|
func Checksum(bz []byte) []byte {
|
|
h := sha256.Sum256(bz)
|
|
return h[:]
|
|
}
|
|
|
|
type PubKey interface {
|
|
Address() Address
|
|
Bytes() []byte
|
|
VerifySignature(msg []byte, sig []byte) bool
|
|
Equals(PubKey) bool
|
|
Type() string
|
|
|
|
// Implementations must support tagged encoding in JSON.
|
|
jsontypes.Tagged
|
|
}
|
|
|
|
type PrivKey interface {
|
|
Bytes() []byte
|
|
Sign(msg []byte) ([]byte, error)
|
|
PubKey() PubKey
|
|
Equals(PrivKey) bool
|
|
Type() string
|
|
|
|
// Implementations must support tagged encoding in JSON.
|
|
jsontypes.Tagged
|
|
}
|
|
|
|
type Symmetric interface {
|
|
Keygen() []byte
|
|
Encrypt(plaintext []byte, secret []byte) (ciphertext []byte)
|
|
Decrypt(ciphertext []byte, secret []byte) (plaintext []byte, err error)
|
|
}
|
|
|
|
// If a new key type implements batch verification,
|
|
// the key type must be registered in github.com/tendermint/tendermint/crypto/batch
|
|
type BatchVerifier interface {
|
|
// Add appends an entry into the BatchVerifier.
|
|
Add(key PubKey, message, signature []byte) error
|
|
// Verify verifies all the entries in the BatchVerifier, and returns
|
|
// if every signature in the batch is valid, and a vector of bools
|
|
// indicating the verification status of each signature (in the order
|
|
// that signatures were added to the batch).
|
|
Verify() (bool, []bool)
|
|
}
|