Imports #
"bytes"
"crypto/ed25519"
"crypto/sha256"
"encoding/base64"
"encoding/binary"
"errors"
"fmt"
"io"
"strconv"
"strings"
"unicode"
"unicode/utf8"
"bytes"
"crypto/ed25519"
"crypto/sha256"
"encoding/base64"
"encoding/binary"
"errors"
"fmt"
"io"
"strconv"
"strings"
"unicode"
"unicode/utf8"
const algEd25519 = 1var errInvalidSigner = *ast.CallExprvar errMalformedNote = *ast.CallExprvar errMismatchedVerifier = *ast.CallExprvar errSignerAlg = *ast.CallExprvar errSignerHash = *ast.CallExprvar errSignerID = *ast.CallExprvar errVerifierAlg = *ast.CallExprvar errVerifierHash = *ast.CallExprvar errVerifierID = *ast.CallExprvar sigPrefix = *ast.CallExprvar sigSplit = *ast.CallExprtype verifierMap map[nameHash][]VerifierA Signer signs messages using a specific key.
type Signer interface {
Name() string
KeyHash() uint32
Sign(msg []byte) ([]byte, error)
}A Verifier verifies messages signed with a specific key.
type Verifier interface {
Name() string
KeyHash() uint32
Verify(msg []byte, sig []byte) bool
}A Verifiers is a collection of known verifier keys.
type Verifiers interface {
Verifier(name string, hash uint32) (Verifier, error)
}An InvalidSignatureError indicates that the given key was known and the associated Verifier rejected the signature.
type InvalidSignatureError struct {
Name string
Hash uint32
}A Note is a text and signatures.
type Note struct {
Text string
Sigs []Signature
UnverifiedSigs []Signature
}A Signature is a single signature found in a note.
type Signature struct {
Name string
Hash uint32
Base64 string
}An UnknownVerifierError indicates that the given key is not known. The Open function records signatures without associated verifiers as unverified signatures.
type UnknownVerifierError struct {
Name string
KeyHash uint32
}An UnverifiedNoteError indicates that the note successfully parsed but had no verifiable signatures.
type UnverifiedNoteError struct {
Note *Note
}An ambiguousVerifierError indicates that the given name and hash match multiple keys passed to [VerifierList]. (If this happens, some malicious actor has taken control of the verifier list, at which point we may as well give up entirely, but we diagnose the problem instead.)
type ambiguousVerifierError struct {
name string
hash uint32
}type nameHash struct {
name string
hash uint32
}signer is a trivial Signer implementation.
type signer struct {
name string
hash uint32
sign func([]byte) ([]byte, error)
}verifier is a trivial Verifier implementation.
type verifier struct {
name string
hash uint32
verify func([]byte, []byte) bool
}func (e *UnverifiedNoteError) Error() stringfunc (e *InvalidSignatureError) Error() stringfunc (e *UnknownVerifierError) Error() stringfunc (e *ambiguousVerifierError) Error() stringGenerateKey generates a signer and verifier key pair for a named server. The signer key skey is private and must be kept secret.
func GenerateKey(rand io.Reader, name string) (skey string, vkey string, err error)func (v *verifier) KeyHash() uint32func (s *signer) KeyHash() uint32func (v *verifier) Name() stringfunc (s *signer) Name() stringNewEd25519VerifierKey returns an encoded verifier key using the given name and Ed25519 public key.
func NewEd25519VerifierKey(name string, key ed25519.PublicKey) (string, error)NewSigner constructs a new [Signer] from an encoded signer key.
func NewSigner(skey string) (Signer, error)NewVerifier construct a new [Verifier] from an encoded verifier key.
func NewVerifier(vkey string) (Verifier, error)Open opens and parses the message msg, checking signatures from the known verifiers. For each signature in the message, Open calls known.Verifier to find a verifier. If known.Verifier returns a verifier and the verifier accepts the signature, Open records the signature in the returned note's Sigs field. If known.Verifier returns a verifier but the verifier rejects the signature, Open returns an InvalidSignatureError. If known.Verifier returns an UnknownVerifierError, Open records the signature in the returned note's UnverifiedSigs field. If known.Verifier returns any other error, Open returns that error. If no known verifier has signed an otherwise valid note, Open returns an [UnverifiedNoteError]. In this case, the unverified note can be fetched from inside the error.
func Open(msg []byte, known Verifiers) (*Note, error)func (s *signer) Sign(msg []byte) ([]byte, error)Sign signs the note with the given signers and returns the encoded message. The new signatures from signers are listed in the encoded message after the existing signatures already present in n.Sigs. If any signer uses the same key as an existing signature, the existing signature is elided from the output.
func Sign(n *Note, signers ...Signer) ([]byte, error)func (m verifierMap) Verifier(name string, hash uint32) (Verifier, error)VerifierList returns a [Verifiers] implementation that uses the given list of verifiers.
func VerifierList(list ...Verifier) Verifiersfunc (v *verifier) Verify(msg []byte, sig []byte) boolchop chops s at the first instance of sep, if any, and returns the text before and after sep. If sep is not present, chop returns before is s and after is empty.
func chop(s string, sep string) (before string, after string)isValidName reports whether name is valid. It must be non-empty and not have any Unicode spaces or pluses.
func isValidName(name string) boolkeyHash computes the key hash for the given server name and encoded public key.
func keyHash(name string, key []byte) uint32Generated with Arrow