gocry

package module
v0.0.0-...-c65c8ba Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 23, 2018 License: BSD-2-Clause Imports: 10 Imported by: 0

README

gocry

Very simple package to encrypt/decrypt messages.

Documentation

Overview

Package gocry is a simple package for encrypting/decrypting messages using AES-GCM.

Index

Constants

View Source
const DefaultCrypto = "aes-256-gcm"

DefaultCrypto is the default crypto that will be used if nothing else is specified.

View Source
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

func CalcHash(password string, salt []byte) (hash string)

CalcHash computes hash from password and salt. The hash is concatenated with salt and encoded in base64 format.

func Decrypt

func Decrypt(ciphertext []byte, key []byte) (plaintext []byte, err error)

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

func DecryptAESGCM(ciphertext []byte, key []byte) (plaintext []byte, err error)

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

func DecryptFile(filename string, key []byte) (plaintext []byte, err error)

DecryptFile read from file and decrypts the content. Calls gocry.Decrypt() internally to decrypt message.

func Encrypt

func Encrypt(plaintext []byte, key []byte) (ciphertext []byte, err error)

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

func EncryptAESGCM(plaintext []byte, key []byte) (ciphertext []byte, err error)

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

func EncryptFile(filename string, plaintext []byte, key []byte) (err error)

EncryptFile encrypts a message and store it in a file. Calls gocry.Encrypt() internally to encrypt message.

func ExtractSalt

func ExtractSalt(hash string) (salt []byte, err error)

ExtractSalt extracts salt from base64 encoded hash.

func GenerateHash

func GenerateHash(password string) (hash string)

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

func GenerateKeySize(keySize uint) (key []byte)

GenerateKeySize generates a key with specified size (in bytes).

func SetCryptoByID

func SetCryptoByID(id uint16) (err error)

SetCryptoByID sets current crypto from crypto id. If there is no match it will return an error.

func SetCryptoByName

func SetCryptoByName(name string) (err error)

SetCryptoByName finds registered crypto from crypto name. If there is no match it will return an error.

func ValidatePassword

func ValidatePassword(password string, hash string) (match bool, err error)

ValidatePassword checks if password matches with hash. This function returns error only when the hash is invalid.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL