Categorygithub.com/proofrock/crypgo
modulepackage
1.2.1
Repository: https://github.com/proofrock/crypgo.git
Documentation: pkg.go.dev

# README

crypgo 1.2.0

A dead simple Go library that encrypts, decrypts, and optionally compresses strings to strings.

Algorithms used:

Compression is applied only if it reduces the size of the message.

Documentation

pkg.go.dev

Import

go get github.com/proofrock/crypgo

Usage

Given a password and a plaintext, we want to encrypt and decrypt them:

password := "hello"
plaintext := "world"
	
cyphertext, err := crypgo.Encrypt(password, plaintext)
// or cyphertext, err := crypgo.CompressAndEncrypt(password, plaintext, 19)
if err != nil {
	// ...
}

plaintext2, err := crypgo.Decrypt(password, cyphertext)
if err != nil {
	// ...
}

assert(plaintext == plaintext2)

The third argument for CompressAndEncrypt is the compression level for zstd, values are 1 to 19 (inclusive).

Byte mode

Each method also has a byte variant (same name, suffixed by Bytes) where the plaintext is a byte array instead of a string.

Different Base64 encodings

The method SetVariant allows to setup a Base64 variant, as defined by Go. For example, to use a URL-safe Base64 encoding, just for the method, code can be:

SetVariant(base64.URLEncoding)
defer SetVariant(base64.StdEncoding)

Notes

cyphertext will be Base64-encoded, and includes a checksum, the random bytes for the salt and IV (the same random bytes are used for Scrypt's salt and for XChaCha's nonce), and the encrypted/compressed plaintext, of course. Expect it to be longer than the plaintext, if compression is not applied.

Changelog

v1.2.0

  • migration to klauspost/compress, which doesn't require CGO
  • added more tests

# Functions

This function receives a password and a plain text (in string form), and a level for compression (from 1 to 19) and produces a string with their encryption, compressing the plaintext if possible.
This function receives a password, a byte array, and a level for compression (from 1 to 19) and produces a string with their encryption, compressing the byte array if possible.
This function receives a password and a cypher text (as produced by one of the Encrypt* methods) and decodes the original plaintext (if the password is the one used for encryption).
This function receives a password and a cypher text (as produced by one of the *EncryptBytes methods) and decodes the original plaintext (if the password is the one used for encryption).
This function receives a password and a plain text (in string form) and produces a string with their encryption.
This function receives a password and a a byte array and produces a string with their encryption.
Sets a Base64 variant, for example base64.URLEncoding for URL_safe encoding.