GoCrypto is a Go library that provides various cryptographic functionalities including hashing, encryption, and key management. This library supports both symmetric and asymmetric encryption, as well as various hashing algorithms.
To install GoCrypto, use the following command:
go get github.com/mekramy/gocrypto
GoCrypto provides interfaces and implementations for hashing data using various algorithms.
Hasher Generate hash from data and password. For security usecase like password use argon2 or bcrypt driver.
type Hasher interface {
Hash(data []byte) ([]byte, error)
Validate(hash, data []byte) (bool, error)
}
Generate new argon2 hash driver (recommended for password). Pass 0 for parameter to use defaults.
hasher := gocrypto.NewArgon2Hasher(0, 0, 0, 0, 0)
hash, err := hasher.Hash([]byte("password"))
isValid, err := hasher.Validate(hash, []byte("password"))
Generate new bcrypt hash driver (alternative for password). Pass 0 for parameter to use defaults.
hasher := gocrypto.NewBcryptHasher(0)
hash, err := hasher.Hash([]byte("password"))
isValid, err := hasher.Validate(hash, []byte("password"))
Generate new Hmac hash driver (recommended for message sign).
hasher := gocrypto.HMacHasher([]byte("key"), gocrypto.MD5)
hash, err := hasher.Hash([]byte("message"))
isValid, err := hasher.Validate(hash, []byte("message"))
GoCrypto provides interfaces and implementations for encrypting and decrypting data.
type Encrypt interface {
Sign(data []byte) ([]byte, error)
ValidateSignature(data []byte, signature []byte) (bool, error)
Encrypt(data []byte) ([]byte, error)
Decrypt(data []byte) ([]byte, error)
EncryptBase64(data []byte) (string, error)
DecryptBase64(encrypted string) ([]byte, error)
}
key, _ := gocrypto.NewSimpleKey(gocrypto.Simple32)
encryptor := gocrypto.NewSymmetric(key, gocrypto.SHA256)
encrypted, err := encryptor.Encrypt([]byte("message"))
decrypted, err := encryptor.Decrypt(encrypted)
key, _ := gocrypto.NewRSAKey(gocrypto.RSA2048)
encryptor := gocrypto.NewAsymmetric(*key, gocrypto.SHA256)
encrypted, err := encryptor.Encrypt([]byte("message"))
decrypted, err := encryptor.Decrypt(encrypted)
GoCrypto provides functionalities for generating and managing cryptographic keys.
key, err := gocrypto.NewSimpleKey(gocrypto.Simple32)
fmt.Println(key.Hex())
fmt.Println(key.Base64())
key, err := gocrypto.NewRSAKey(gocrypto.RSA2048)
privateKeyPEM, err := key.PrivateKeyPEM(false)
publicKeyPEM, err := key.PublicKeyPEM(false)
GoCrypto provides utility functions for parsing keys and certificates.
privateKey, err := gocrypto.ParsePrivateKey(pemEncodedKey, true)
publicKey, err := gocrypto.ParsePublicKey(pemEncodedKey, true)
csr, err := gocrypto.ParseCertificateRequest(pemEncodedCSR, true)
cert, err := gocrypto.ParseCertificate(pemEncodedCert, true)
GoCrypto support following hashing algorithms for signing.
MD5
: MD5 (not recommended for security)SHA1
: SHA1 (not recommended for security)SHA224
: SHA2 224 bitSHA256
: SHA2 256 bit (recommended)SHA384
: SHA2 384 bitSHA512
: SHA2 512 bitSHA3224
:SHA3 224 bitSHA3256
:SHA3 256 bitSHA3384
:SHA3 384 bitSHA3512
:SHA3 512 bit