Documentation
¶
Overview ¶
Package encid provides a mechanism for working with encrypted integer IDs.
Index ¶
- Variables
- func Decode(ctx context.Context, ks KeyStore, keyID int64, inp string) (int, int64, error)
- func Decode50(ctx context.Context, ks KeyStore, keyID int64, inp string) (int, int64, error)
- func Encode(ctx context.Context, ks KeyStore, typ int, n int64) (int64, string, error)
- func Encode50(ctx context.Context, ks KeyStore, typ int, n int64) (int64, string, error)
- type KeyStore
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("not found")
ErrNotFound is the type of error produced when KeyStore methods find no key.
Functions ¶
func Decode ¶
Decode decodes a keyID/string pair produced by Encode. It produces the type of the key that was used, and the bare int64 value that was encrypted. As a convenience, it maps the input string to all lowercase before decoding.
func Decode50 ¶
Decode50 decodes a keyID/string pair produced by Encode50. It produces the type of the key that was used, and the bare int64 value that was encrypted. Unlike Decode, this does not map the input to lowercase first, since base50 strings are case-sensitive.
func Encode ¶
Encode encodes a number n using a key of the given type from the given keystore. The result is the ID of the key used, followed by the encrypted string. The encrypted string is expressed in base 30, which uses digits 0-9, then lower-case bcdfghjkmnpqrstvwxyz. It excludes vowels (to avoid inadvertently spelling naughty words) and lowercase "L".
Types ¶
type KeyStore ¶
type KeyStore interface {
// DecoderByID looks up a key in the store by its ID.
// It returns the key's type and a function for decrypting a data block using the key.
// The slice arguments to the decryption function must overlap entirely or not at all.
// If no key with the given ID is found,
// ErrNotFound is returned.
DecoderByID(context.Context, int64) (int, func(dst, src []byte), error)
// EncoderByType looks up a key in the store by its type.
// It returns the key's ID and a function for encrypting a data block using the key.
// The slice arguments to the encryption function must overlap entirely or not at all.
// In case there are multiple keys of the given type,
// it is up to the implementation to choose one and return it.
// If no key with the given type is found,
// ErrNotFound is returned.
EncoderByType(context.Context, int) (int64, func(dst, src []byte), error)
}
KeyStore is an object that stores encryption keys. Each key is 16, 24, or 32 bytes long, and has an associated "type" (an int) and a unique key ID (an int64). These keys can be used to encrypt other int64s, and to decrypt the resulting strings. See Encode and Decode.