Documentation ¶
Overview ¶
Package gocry is a simple package for encrypting/decrypting messages using AES-GCM.
Index ¶
- Constants
- func AddCryptos(cis ...CryptoInterface) (err error)
- func CalcHash(password string, salt []byte) (hash string)
- func Decrypt(ciphertext []byte, key []byte) (plaintext []byte, err error)
- func DecryptAESGCM(ciphertext []byte, key []byte) (plaintext []byte, err error)
- func DecryptFile(filename string, key []byte) (plaintext []byte, err error)
- func Encrypt(plaintext []byte, key []byte) (ciphertext []byte, err error)
- func EncryptAESGCM(plaintext []byte, key []byte) (ciphertext []byte, err error)
- func EncryptFile(filename string, plaintext []byte, key []byte) (err error)
- func ExtractSalt(hash string) (salt []byte, err error)
- func GenerateHash(password string) (hash string)
- func GenerateKey() (key []byte)
- func GenerateKeySize(keySize uint) (key []byte)
- func SetCryptoByID(id uint16) (err error)
- func SetCryptoByName(name string) (err error)
- func ValidatePassword(password string, hash string) (match bool, err error)
- type CryptoInterface
Constants ¶
const DefaultCrypto = "aes-256-gcm"
DefaultCrypto is the default crypto that will be used if nothing else is specified.
const SaltSize = 10
SaltSize defines size of salt in bytes.
Variables ¶
This section is empty.
Functions ¶
func AddCryptos ¶
func AddCryptos(cis ...CryptoInterface) (err error)
AddCryptos is used to register cryptos into gocry. If the crypto id or name already exists it will return an error. The registered cryptos can be found using GetAllCryptos/GetCryptoByID.
func CalcHash ¶
CalcHash computes hash from password and salt. The hash is concatenated with salt and encoded in base64 format.
func Decrypt ¶
Decrypt decrypts the encrypted message. There is no need for caller to configure current crypto since gocry detects which crypto were used and will decrypt correctly.
func DecryptAESGCM ¶
DecryptAESGCM performs a decryption on a byte array using block cipher AES-GCM. It will return a error in case the function fails to decrypt, which happens when it has wrong ciphertext/key/nonce.
func DecryptFile ¶
DecryptFile read from file and decrypts the content. Calls gocry.Decrypt() internally to decrypt message.
func Encrypt ¶
Encrypt encrypts the message using current crypto. It appends a crypto id at the beginning of encrypted message, which makes gocry able to detect and decrypt correctly.
func EncryptAESGCM ¶
EncryptAESGCM performs a encryption on byte array using block cipher AES-GCM. Nonce is generated inside the function, and added to ciphertext, so one cannot reuse a nonce value for encrypting other messages.
func EncryptFile ¶
EncryptFile encrypts a message and store it in a file. Calls gocry.Encrypt() internally to encrypt message.
func ExtractSalt ¶
ExtractSalt extracts salt from base64 encoded hash.
func GenerateHash ¶
GenerateHash generates a hash from password. The salt is generated randomly and then hash is computed using the salt together with password. The hash is concatenated with salt and encoded in base64 format.
func GenerateKey ¶
func GenerateKey() (key []byte)
GenerateKey generates a key with a length matching to current crypto.
func GenerateKeySize ¶
GenerateKeySize generates a key with specified size (in bytes).
func SetCryptoByID ¶
SetCryptoByID sets current crypto from crypto id. If there is no match it will return an error.
func SetCryptoByName ¶
SetCryptoByName finds registered crypto from crypto name. If there is no match it will return an error.
Types ¶
type CryptoInterface ¶
type CryptoInterface struct { Encrypt func([]byte, []byte) ([]byte, error) Decrypt func([]byte, []byte) ([]byte, error) ID uint16 Name string KeySize uint }
CryptoInterface is a structure used to define cryptos that are available in gocry. Id and name must be unique for all cryptos. KeySize is in bytes. The name should be all in lower case with a format like:
[crypto-algorithm]-[key-size in bits]{-[block cipher mode]}
e.g. if it is AES-GCM with key length 32-byte it should be:
aes-256-gcm
func GetAllCryptos ¶
func GetAllCryptos() (cis []CryptoInterface)
GetAllCryptos returns an array of CryptoInterface available in gocry. It can be used to find what cryptos are available.
func GetCryptoByID ¶
func GetCryptoByID(id uint16) (ci *CryptoInterface, err error)
GetCryptoByID finds registered crypto from crypto id. If there is no match it will return an error.
func GetCryptosAES ¶
func GetCryptosAES() (ci []CryptoInterface)
GetCryptosAES returns an array of CryptoInterface available in gocry's AES interface.
func GetCurrentCrypto ¶
func GetCurrentCrypto() (ci *CryptoInterface)
GetCurrentCrypto returns current crypto that is used to encrypt messages.
func GetEncryptionMethod ¶
func GetEncryptionMethod(ciphertext []byte) (ci *CryptoInterface, err error)
GetEncryptionMethod gets the crypto that is used on a encrypted message. If a ciphertext that is not encrypted with gocry is given, it will return an error or wrong crypto.