Documentation
¶
Index ¶
- Constants
- Variables
- func AddChecksum(bytes []byte) []byte
- func CheckChecksum(checked []byte) (message []byte, checksumOk bool)
- func Generate(al Algorithm, rdr io.Reader) (public PublicKey, private PrivateKey, err error)
- func IsPrivate(k Key) bool
- func IsPublic(k Key) bool
- func Match(pub PublicKey, pvt PrivateKey) bool
- func MaybePrivate(s string) bool
- func MaybePublic(s string) bool
- func NameOf(al Algorithm) string
- func RegisterAlgorithm(id AlgorithmID, al Algorithm) error
- func SameAlgorithm(a1 Algorithm, a2 Algorithm) bool
- type Algorithm
- type AlgorithmID
- type IdentifiedData
- func (z *IdentifiedData) DecodeMsg(dc *msgp.Reader) (err error)
- func (z *IdentifiedData) EncodeMsg(en *msgp.Writer) (err error)
- func (z *IdentifiedData) MarshalMsg(b []byte) (o []byte, err error)
- func (z *IdentifiedData) Msgsize() (s int)
- func (z *IdentifiedData) UnmarshalMsg(bts []byte) (o []byte, err error)
- type Key
- type PrivateKey
- func (key PrivateKey) Algorithm() Algorithm
- func (key PrivateKey) ExtraBytes() []byte
- func (key PrivateKey) FullString() string
- func (key PrivateKey) IsZero() bool
- func (key PrivateKey) KeyBytes() []byte
- func (key PrivateKey) Marshal() (serialized []byte, err error)
- func (key PrivateKey) MarshalMsg(in []byte) (out []byte, err error)
- func (key *PrivateKey) MarshalString() (string, error)
- func (key PrivateKey) MarshalText() ([]byte, error)
- func (key *PrivateKey) Msgsize() (s int)
- func (key PrivateKey) Sign(message []byte) Signature
- func (key PrivateKey) Size() int
- func (key PrivateKey) String() string
- func (key *PrivateKey) Truncate()
- func (key *PrivateKey) Unmarshal(serialized []byte) error
- func (key *PrivateKey) UnmarshalMsg(in []byte) (leftover []byte, err error)
- func (key *PrivateKey) UnmarshalText(text []byte) error
- func (key *PrivateKey) Zeroize()
- type PublicKey
- func (key PublicKey) Algorithm() Algorithm
- func (key PublicKey) ExtraBytes() []byte
- func (key PublicKey) FullString() string
- func (key PublicKey) IsZero() bool
- func (key PublicKey) KeyBytes() []byte
- func (key PublicKey) Marshal() (serialized []byte, err error)
- func (key PublicKey) MarshalMsg(in []byte) (out []byte, err error)
- func (key *PublicKey) MarshalString() (string, error)
- func (key PublicKey) MarshalText() ([]byte, error)
- func (key *PublicKey) Msgsize() (s int)
- func (key PublicKey) Size() int
- func (key PublicKey) String() string
- func (key *PublicKey) Truncate()
- func (key *PublicKey) Unmarshal(serialized []byte) error
- func (key *PublicKey) UnmarshalMsg(in []byte) (leftover []byte, err error)
- func (key *PublicKey) UnmarshalText(text []byte) error
- func (key PublicKey) Verify(message []byte, sig Signature) bool
- func (key *PublicKey) Zeroize()
- type Signature
- func (signature Signature) Algorithm() Algorithm
- func (signature *Signature) Bytes() []byte
- func (signature Signature) Marshal() (serialized []byte, err error)
- func (signature Signature) MarshalMsg(in []byte) (out []byte, err error)
- func (signature *Signature) MarshalString() (string, error)
- func (signature Signature) MarshalText() ([]byte, error)
- func (signature *Signature) Msgsize() (s int)
- func (signature Signature) Size() int
- func (signature *Signature) Unmarshal(serialized []byte) error
- func (signature *Signature) UnmarshalMsg(in []byte) (leftover []byte, err error)
- func (signature *Signature) UnmarshalText(text []byte) error
- func (signature Signature) Verify(message []byte, key PublicKey) bool
Constants ¶
const ( // ChecksumPadWidth is the moduland of which the length of a checksummed // byte slice will always equal 0. // // In other words, we choose a checksum width such that // `len(summedMsg) % ChecksumPadWidth == 0`. // // We choose 5 because we expect to encode this data in a base32 encoding, // which needs no padding when the input size is a multiple of 5. ChecksumPadWidth = 5 // ChecksumMinBytes is the minimum number of checksum bytes. The chance // that a checksum will accidentally pass is roughly `1 / (256 ^ ChecksumMinBytes)`, // so the value of 3 used gives a false positive rate of about 1 / 16 million. ChecksumMinBytes = 3 )
const PrivateKeyPrefix = "npvt"
PrivateKeyPrefix always prefixes Ndau private keys in text serialization
const PublicKeyPrefix = "npub"
PublicKeyPrefix always prefixes Ndau public keys in text serialization
Variables ¶
re-export package-native algorithms
Functions ¶
func AddChecksum ¶
AddChecksum adds a checksum to a byte slice.
The number of bytes of the checksum depend on the width of the data: an appropriate number will be used, at least `ChecksumMinBytes`, such that the total length of the returned slice is a multiple of `ChecksumPadWidth`.
The checksum bytes are appended to the end of the message.
A single byte is also added at the head of the byte slice, containing the number of padding bytes, for ease of checking the checksum.
func CheckChecksum ¶
CheckChecksum validates the checksum of a summed byte slice.
It returns the wrapped message stripped of checksum data and a boolean that indicates if the checksum was correct; if the boolean is false, the message is not valid.
func Match ¶
func Match(pub PublicKey, pvt PrivateKey) bool
Match indicates whether or not given public and private keys match each other
func MaybePrivate ¶
MaybePrivate provides a fast way to check whether a string looks like it might be an ndau private key.
To get a definitive answer as to whether something is a private key, one must attempt to deserialize it using UnmarshalText and check the error value. That takes some work; it's faster to use this to get a first impression.
This function will allow some false positives, but no false negatives: some values for which it returns `true` may not be actual valid keys, but no values for which it returns `false` will return actual valid keys.
func MaybePublic ¶
MaybePublic provides a fast way to check whether a string looks like it might be an ndau public key.
To get a definitive answer as to whether something is a public key, one must attempt to deserialize it using UnmarshalText and check the error value. That takes some work; it's faster to use this to get a first impression.
This function will allow some false positives, but no false negatives: some values for which it returns `true` may not be actual valid keys, but no values for which it returns `false` will return actual valid keys.
func RegisterAlgorithm ¶
func RegisterAlgorithm(id AlgorithmID, al Algorithm) error
RegisterAlgorithm makes it possible to serialize and deserialize custom Algorithms
If you build a custom Algorithm, you probably want to call this in an init function All IDs < 128 are reserved for canonical implementations.
func SameAlgorithm ¶
SameAlgorithm returns true when two algorithms are in fact the same algorithm, even if they are not the same instance.
Unknown algorithms are never the same.
Types ¶
type Algorithm ¶
type Algorithm interface { // PublicKeySize is the size in bytes of this algorithm's public keys PublicKeySize() int // PrivateKeySize is the size in bytes of this algorithm's private keys PrivateKeySize() int // SignatureSize is the size in bytes of this algorithm's signatures SignatureSize() int // Public generates a public key when given a private key Public(private []byte) []byte // Generate creates a new keypair Generate(rand io.Reader) (public, private []byte, err error) // Sign signs the message with privateKey and returns a signature Sign(private, message []byte) []byte // Verify verifies a message's signature // // Return true if the signature is valid Verify(public, message, sig []byte) bool }
Algorithm abstracts over a variety of signature algorithms
The required methods here are low-level, for simplicity of external implementation. Consumers should consider generating their keys using the `Generate` function and then interacting with the keys and signatures using the high-level interface.
type AlgorithmID ¶
type AlgorithmID uint8
AlgorithmID is an identifier uniquely associated with each supported signature algorithm
func (*AlgorithmID) DecodeMsg ¶
func (z *AlgorithmID) DecodeMsg(dc *msgp.Reader) (err error)
DecodeMsg implements msgp.Decodable
func (AlgorithmID) EncodeMsg ¶
func (z AlgorithmID) EncodeMsg(en *msgp.Writer) (err error)
EncodeMsg implements msgp.Encodable
func (AlgorithmID) MarshalMsg ¶
func (z AlgorithmID) MarshalMsg(b []byte) (o []byte, err error)
MarshalMsg implements msgp.Marshaler
func (AlgorithmID) Msgsize ¶
func (z AlgorithmID) Msgsize() (s int)
Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message
func (*AlgorithmID) UnmarshalMsg ¶
func (z *AlgorithmID) UnmarshalMsg(bts []byte) (o []byte, err error)
UnmarshalMsg implements msgp.Unmarshaler
type IdentifiedData ¶
type IdentifiedData struct { Algorithm AlgorithmID Data []byte }
IdentifiedData is a byte slice associated with an algorithm
func (*IdentifiedData) DecodeMsg ¶
func (z *IdentifiedData) DecodeMsg(dc *msgp.Reader) (err error)
DecodeMsg implements msgp.Decodable
func (*IdentifiedData) EncodeMsg ¶
func (z *IdentifiedData) EncodeMsg(en *msgp.Writer) (err error)
EncodeMsg implements msgp.Encodable
func (*IdentifiedData) MarshalMsg ¶
func (z *IdentifiedData) MarshalMsg(b []byte) (o []byte, err error)
MarshalMsg implements msgp.Marshaler
func (*IdentifiedData) Msgsize ¶
func (z *IdentifiedData) Msgsize() (s int)
Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message
func (*IdentifiedData) UnmarshalMsg ¶
func (z *IdentifiedData) UnmarshalMsg(bts []byte) (o []byte, err error)
UnmarshalMsg implements msgp.Unmarshaler
type Key ¶
type Key interface { encoding.TextMarshaler encoding.TextUnmarshaler fmt.Stringer msgp.Marshaler msgp.Unmarshaler msgp.Sizer KeyBytes() []byte ExtraBytes() []byte Algorithm() Algorithm Truncate() Zeroize() }
A Key is a public or private key which knows about its algorithm
This is most useful when abstracting over what might be a public or a private key. To recover the concrete instance, consider a typeswitch:
switch key := keyI.(type) { case PublicKey:
...
case PrivateKey:
... }
Key includes several other interfaces to ensure consistent marshalling and unmarshalling in both binary and text formats
type PrivateKey ¶
type PrivateKey struct {
// contains filtered or unexported fields
}
A PrivateKey is the private half of a keypair
func ParsePrivateKey ¶
func ParsePrivateKey(s string) (*PrivateKey, error)
ParsePrivateKey parses a string representation of a private key, if possible
func RawPrivateKey ¶
func RawPrivateKey(al Algorithm, key, extra []byte) (*PrivateKey, error)
RawPrivateKey creates a PrivateKey from raw data
This is unsafe and subject to only minimal type-checking; it should normally be avoided.
func (PrivateKey) Algorithm ¶
func (key PrivateKey) Algorithm() Algorithm
Algorithm returns the key's algorithm
func (PrivateKey) ExtraBytes ¶
func (key PrivateKey) ExtraBytes() []byte
ExtraBytes returns any extra data
func (PrivateKey) FullString ¶
func (key PrivateKey) FullString() string
FullString returns the key's human-readable serialization
func (PrivateKey) IsZero ¶
func (key PrivateKey) IsZero() bool
IsZero is true when this key is the zero value
func (PrivateKey) KeyBytes ¶
func (key PrivateKey) KeyBytes() []byte
KeyBytes returns the key's data
func (PrivateKey) Marshal ¶
Marshal marshals the key into a serialized binary format which includes a type byte for the algorithm.
func (PrivateKey) MarshalMsg ¶
MarshalMsg implements msgp.Marshaler
func (*PrivateKey) MarshalString ¶
func (key *PrivateKey) MarshalString() (string, error)
MarshalString is like MarshalText, but to a string
func (PrivateKey) MarshalText ¶
func (key PrivateKey) MarshalText() ([]byte, error)
MarshalText implements encoding.TextMarshaler.
PublicKeys encode like Keys, with the addition of a human-readable prefix for easy identification.
func (*PrivateKey) Msgsize ¶
func (key *PrivateKey) Msgsize() (s int)
Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message Msgsize implements msgp.Sizer
This method was copy-pasted from the IdentifiedData Msgsize implementation, as fundamentally a PrivateKey gets serialized as an IdentifiedData, and so should have the same size.
func (PrivateKey) Sign ¶
func (key PrivateKey) Sign(message []byte) Signature
Sign the supplied message
func (PrivateKey) String ¶
func (key PrivateKey) String() string
String returns a shorthand for the key's data
This returns the first 8 characters of the text serialization, an ellipsis, then the final 4 characters of the text serialization. Total output size is constant at 15 characters.
This destructively truncates the key, but it is a useful format for humans.
func (*PrivateKey) Truncate ¶
func (key *PrivateKey) Truncate()
Truncate removes all extra data from this key.
This is a destructive operation which cannot be undone; make copies first if you need to.
func (*PrivateKey) Unmarshal ¶
func (key *PrivateKey) Unmarshal(serialized []byte) error
Unmarshal unmarshals the serialized bytes into the PrivateKey pointer
func (*PrivateKey) UnmarshalMsg ¶
func (key *PrivateKey) UnmarshalMsg(in []byte) (leftover []byte, err error)
UnmarshalMsg implements msgp.Unmarshaler
func (*PrivateKey) UnmarshalText ¶
func (key *PrivateKey) UnmarshalText(text []byte) error
UnmarshalText implements encoding.TextUnmarshaler
func (*PrivateKey) Zeroize ¶
func (key *PrivateKey) Zeroize()
Zeroize removes all data from this key
This is a destructive operation which cannot be undone; make copies first if you need to.
type PublicKey ¶
type PublicKey struct {
// contains filtered or unexported fields
}
A PublicKey is the public half of a keypair
func ParsePublicKey ¶
ParsePublicKey parses a string representation of a public key, if possible
func RawPublicKey ¶
RawPublicKey creates a PublicKey from raw data
This is unsafe and subject to only minimal type-checking; it should normally be avoided.
func (PublicKey) Algorithm ¶
func (key PublicKey) Algorithm() Algorithm
Algorithm returns the key's algorithm
func (PublicKey) ExtraBytes ¶
func (key PublicKey) ExtraBytes() []byte
ExtraBytes returns any extra data
func (PublicKey) FullString ¶
FullString returns the key's human-readable serialization
func (PublicKey) IsZero ¶
func (key PublicKey) IsZero() bool
IsZero is true when this key is the zero value
func (PublicKey) Marshal ¶
Marshal marshals the key into a serialized binary format which includes a type byte for the algorithm.
func (PublicKey) MarshalMsg ¶
MarshalMsg implements msgp.Marshaler
func (*PublicKey) MarshalString ¶
MarshalString is like MarshalText, but to a string
func (PublicKey) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
PublicKeys encode like Keys, with the addition of a human-readable prefix for easy identification.
func (*PublicKey) Msgsize ¶
Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message Msgsize implements msgp.Sizer
This method was copy-pasted from the IdentifiedData Msgsize implementation, as fundamentally a PublicKey gets serialized as an IdentifiedData, and so should have the same size.
func (PublicKey) String ¶
String returns a shorthand for the key's data
This returns the first 8 characters of the text serialization, an ellipsis, then the final 4 characters of the text serialization. Total output size is constant at 15 characters.
This destructively truncates the key, but it is a useful format for humans.
func (*PublicKey) Truncate ¶
func (key *PublicKey) Truncate()
Truncate removes all extra data from this key.
This is a destructive operation which cannot be undone; make copies first if you need to.
func (*PublicKey) UnmarshalMsg ¶
UnmarshalMsg implements msgp.Unmarshaler
func (*PublicKey) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler
type Signature ¶
type Signature struct {
// contains filtered or unexported fields
}
A Signature is a byte slice with known algorithm type
func ParseSignature ¶
ParseSignature parses a string representation of a signature, if possible
func RawSignature ¶
RawSignature creates a Signature from raw data
This is unsafe and subject to only minimal type-checking; it should normally be avoided.
func (Signature) Marshal ¶
Marshal marshals the signature into a serialized binary format which includes a type byte for the algorithm.
func (Signature) MarshalMsg ¶
MarshalMsg implements msgp.Marshaler
func (*Signature) MarshalString ¶
MarshalString is like MarshalText, but to a string
func (Signature) MarshalText ¶
MarshalText implements encoding.TextMarshaler
This marshaller uses a custom b32 encoding which is case-insensitive and lacks certain confusing pairs, for ease of human-friendly handling. For the same reason, it embeds a checksum, so it's easy to tell whether or not it was received correctly.
func (*Signature) Msgsize ¶
Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message Msgsize implements msgp.Sizer
This method was copy-pasted from the IdentifiedData Msgsize implementation, as fundamentally a Signature gets serialized as an IdentifiedData, and so should have the same size.
func (*Signature) Unmarshal ¶
Unmarshal unmarshals the serialized binary data into the supplied signature instance
func (*Signature) UnmarshalMsg ¶
UnmarshalMsg implements msgp.Unmarshaler
func (*Signature) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler