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 Decrypter
- type Encrypter
- 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 Decrypter ¶
type Decrypter interface {
// BlockSize returns the Decrypter’s block size.
BlockSize() int
// Decrypt decrypts the first block in src into dst.
// Dst and src must overlap entirely or not at all.
Decrypt(dst, src []byte)
}
Decrypter is the type of an object that can decrypt a block of data. Note: this interface is satisfied by the Block type in crypto/cipher.
type Encrypter ¶
type Encrypter interface {
// BlockSize returns the Encrypter’s block size.
BlockSize() int
// Encrypt encrypts the first block in src into dst.
// Dst and src must overlap entirely or not at all.
Encrypt(dst, src []byte)
}
Encrypter is the type of an object that can encrypt a block of data. Note: this interface is satisfied by the Block type in crypto/cipher.
type KeyStore ¶
type KeyStore interface {
// DecoderByID looks up a key in the store by its ID.
// It returns the key's type and a [Decrypter] for decrypting a data block using the key.
// If no key with the given ID is found,
// ErrNotFound is returned.
DecoderByID(context.Context, int64) (int, Decrypter, error)
// EncoderByType looks up a key in the store by its type.
// It returns the key's ID and an [Encrypter] for encrypting a data block using the key.
// In case there are multiple keys of the given type,
// it is up to the implementation to choose one and return it.
// (For example, it could choose the newest one.)
// If no key with the given type is found,
// ErrNotFound is returned.
EncoderByType(context.Context, int) (int64, Encrypter, error)
// Version reports the highest encoded-data format produced and understood by the KeyStore.
// It should return the number 2.
// (Earlier versions of this module didn’t include this method,
// and are considered to be at format version 1.
// Future versions may introduce new formats.)
Version() int
}
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.
The meanings of the "type" values are user-defined. You may choose to give all your keys the same type, or you might prefer to use different types for different resources (e.g. 1 for users, 2 for documents, etc).