cryptor

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2022 License: MIT Imports: 14 Imported by: 0

README

cryptor

Helper funcs to crypt/hash data easily on golang.

Supports:

  • BCrypt

  • Argon2id

  • AES 128/192/256

Example (AES):

import "github.com/oklookat/cryptor"

func main() {
    // 256
    var secret256 = "fUjWnZr4u7x!A%D*G-KaPdSgVkYp2s5v"
    var aes, _ = cryptor.New_AES(secret256)

    var textToEncrypt = "hello world"

    var encrypted, _ = aes.Encrypt(textToEncrypt)

    var decrypted, _ = aes.Decrypt(encrypted)
}

Example (BCrypt):

import "github.com/oklookat/cryptor"

func main() {
    var bcrypt = cryptor.New_BCrypt(10)

    var password = "qwerty"

    var hashed, _ = bcrypt.Hash(password)

    var isMatch, _ = bcrypt.Compare(password, hashed)
}

Example (Argon2id):

import "github.com/oklookat/cryptor"

func main() {
    var argon = cryptor.New_Argon(nil)

    var password = "qwerty"

    var hashed, _ = argon.Hash(password)

    var isMatch, _ = argon.Compare(password, hashed)
}

Documentation

Index

Constants

View Source
const (
	// AES-128 secret length.
	AES_128 = 16

	// AES-192 secret length.
	AES_192 = 24

	// AES-256 secret length.
	AES_256 = 32
)

Variables

View Source
var (
	ErrAesSecretLength = errors.New("[cryptor/AES] secret must be 16/24/32 bytes length")
	ErrAesMakeCipher   = errors.New("[cryptor/AES] make cipher failed")
	ErrAesMakeGCM      = errors.New("[cryptor/AES] make GCM failed")
	ErrAesOpenGCM      = errors.New("[cryptor/AES] open GCM failed")
	ErrAesDecodeHEX    = errors.New("[cryptor/AES] failed to decode hex")
	ErrAesReadNonce    = errors.New("[cryptor/AES] read nonce failed")
)
View Source
var (
	// ErrInvalidHash in returned by ComparePasswordAndHash if the provided
	// hash isn't in the expected format.
	ErrArgonInvalidHash = errors.New("[cryptor/argon] hash is not in the correct format")

	// ErrIncompatibleVariant is returned by ComparePasswordAndHash if the
	// provided hash was created using a unsupported variant of Argon2.
	// Currently only argon2id is supported by this package.
	ErrArgonIncompatibleVariant = errors.New("[cryptor/argon] incompatible variant of argon2")

	// ErrIncompatibleVersion is returned by ComparePasswordAndHash if the
	// provided hash was created using a different version of Argon2.
	ErrArgonIncompatibleVersion = errors.New("[cryptor/argon] incompatible version of argon2")
)
View Source
var (
	// hash errors.
	ErrBcryptDataTooBig = errors.New("[cryptor/bcrypt] max password size is 72 bytes")

	// parse errors.
	//
	ErrBcryptHashWrongLength = errors.New("[cryptor/bcrypt] hash must have 60 bytes length")
	ErrBcryptNoDollar        = errors.New("[cryptor/bcrypt] hash must have $ at start")
	ErrBcryptInvalidHash     = errors.New("[cryptor/bcrypt] invalid hash")
	//
	ErrBcryptUnsupportedVersion = errors.New("[cryptor/bcrypt] this version not supported")
	ErrBcryptWrongCost          = errors.New("[cryptor/bcrypt] cost NaN")
	//
	ErrBcryptSaltNotRadix = errors.New("[cryptor/bcrypt] salt not matches with radix")
	ErrBcryptHashNotRadix = errors.New("[cryptor/bcrypt] hash not matches with radix")
)

Functions

This section is empty.

Types

type AES

type AES struct {
	Secret string
}

func New_AES

func New_AES(secret string) (*AES, error)

func (*AES) Decrypt

func (a *AES) Decrypt(encrypted string) (string, error)

get encrypted and return decrypted text (AES).

func (*AES) Encrypt

func (a *AES) Encrypt(text string) (encrypted string, err error)

get text and return encrypted text (AES).

type Argon

type Argon struct {
	// the amount of memory used by the algorithm (in kibibytes).
	Memory uint32

	// the number of iterations over the memory.
	Iterations uint32

	// the number of threads (or lanes) used by the algorithm.
	// Recommended value is between 1 and runtime.NumCPU().
	Parallelism uint8

	// length of the random salt. 16 bytes is recommended for password hashing.
	SaltLength uint32

	// length of the generated key. 16 bytes or more is recommended.
	KeyLength uint32
}

describes the input parameters used by the Argon2id algorithm.

func New_Argon

func New_Argon(config *Argon) *Argon

func (*Argon) Compare

func (a *Argon) Compare(password, hash string) (match bool, err error)

check argon2id hash.

func (*Argon) GetDefaults

func (a *Argon) GetDefaults() *Argon

get default params.

func (*Argon) Hash

func (a *Argon) Hash(password string) (hash string, err error)

returns an Argon2id hash of a plain-text password using the provided algorithm parameters.

func (*Argon) IsHash

func (a *Argon) IsHash(data string) bool

is data a hash.

func (*Argon) Parse

func (a *Argon) Parse(hash string) (config *Argon, salt, key []byte, err error)

expects a hash created from this package, and parses it to return the params used to create it, as well as the salt and key (password hash).

type BCrypt

type BCrypt struct {
	Cost int
}

func New_BCrypt

func New_BCrypt(cost int) *BCrypt

func (*BCrypt) Compare

func (b *BCrypt) Compare(password, hash string) (isMatch bool, err error)

func (*BCrypt) Hash

func (b *BCrypt) Hash(password string) (hash string, err error)

func (*BCrypt) IsHash

func (b *BCrypt) IsHash(data string) bool

func (*BCrypt) Parse

func (b *BCrypt) Parse(hash string) (*BCryptHash, error)

type BCryptHash

type BCryptHash struct {
	Version string
	Cost    int
	Salt    string
	Hash    string
}

Jump to

Keyboard shortcuts

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