Documentation
¶
Overview ¶
Package simplecrypto implements a simplified interface for encryption and decryption using AES encryption in CFB mode.
It is implemented using examples given in crypto/cipher and crypto/hmac.
Index ¶
- Variables
- func CheckAndDecrypt(key, ciphertext, hmac []byte) (payload []byte, err error)
- func CheckMAC(key, message, messageMAC []byte) bool
- func DecodeJSON(key, data []byte) ([]byte, error)
- func DecodeJSONReader(key []byte, r io.Reader) ([]byte, error)
- func Decrypt(key, ciphertext []byte) (payload []byte, err error)
- func EncodeJSON(key, payload []byte) ([]byte, error)
- func EncodeJSONWriter(key, payload []byte, w io.Writer) error
- func Encrypt(key, payload []byte) (ciphertext []byte, err error)
- func EncryptAndHMAC(key, payload []byte) (ciphertext, hmac []byte, err error)
- func HMAC(key, message []byte) []byte
- type JSONMessage
Constants ¶
This section is empty.
Variables ¶
var ( // ErrCiphertextTooShort is returned when the provided ciphertext is too // short to contain a valid IV. ErrCiphertextTooShort = errors.New("simplecrypto: ciphertext too short") // ErrHMACDoesNotMatch is returned when the provided HMAC does not match. ErrHMACDoesNotMatch = errors.New("simplecrypto: HMAC does not match ciphertext") )
Functions ¶
func CheckAndDecrypt ¶
CheckAndDecrypt checks the HMAC of the chiphertext, and decrypts it if the HMAC matches.
func CheckMAC ¶
CheckMAC reports whether messageMAC is a valid HMAC tag for message.
This implementation is given in the documentation for crypto/hmac.
func DecodeJSON ¶
DecodeJSON decodes a serialized JSON message (data) using a key, and returns the decrypted payload. If the decoded cyphertext does not match the decoded HMAC, then an error is returned.
func DecodeJSONReader ¶
DecodeJSONReader performs the same task as DecodeJSON, but reads from a Reader.
func Decrypt ¶
Decrypt decrypts a ciphertext using the given key. It returns a byte slice of the encrypted payload.
This function assumes the ciphertext is in the format generated by the Encrypt function, i.e. the IV followed by the encrypted payload.
Note: Ciphertexts must be authenticated as well as encrypted in order to be secure. Be sure to check the ciphertext's HMAC before decrypting it. This library provides shorthand for checking the HMAC of a ciphertext.
func EncodeJSON ¶
EncodeJSON encrypts a payload using a key, then encodes it as a JSON object, which includes the ciphertext and its HMAC.
func EncodeJSONWriter ¶
EncodeJSONWriter performs the same task as EncodeJSON, but writes to a Writer.
func Encrypt ¶
Encrypt encrypts a payload using the given key. It returns a byte slice with the IV as the first aes.BlockSize bytes, followed by the encrypted payload.
Note: Ciphertexts must be authenticated as well as encrypted in order to be secure. Be sure to calculate the ciphertext's HMAC to send with it. This library provides shorthand for calculating the HMAC of a ciphertext.
func EncryptAndHMAC ¶
EncryptAndHMAC encrypts the payload using Encrypt, then calculates the ciphertext's HMAC.
Types ¶
type JSONMessage ¶
JSONMessage defines a serialization format for a ciphertext and its HMAC. The two fields are encoded by encoding/json as base64 strings.
This type is not intended to be used directly, but is exported to show the JSON format.