abcrypt

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: Apache-2.0, MIT Imports: 9 Imported by: 0

README

abcrypt-go

CI Go Reference Go version

abcrypt-go is an implementation of the abcrypt encrypted data format.

Usage

To install this library:

go get -u github.com/sorairolake/abcrypt-go
Example

Please see example_test.go.

Documentation

See the documentation for more details.

Minimum Go version

This library requires the minimum version of Go 1.21.

Changelog

Please see CHANGELOG.adoc.

Contributing

Please see CONTRIBUTING.adoc.

License

Copyright © 2024 Shun Sakai (see AUTHORS.adoc)

This library is distributed under the terms of either the Apache License 2.0 or the MIT License.

This project is compliant with version 3.0 of the REUSE Specification. See copyright notices of individual files for more details on copyright and licensing information.

Documentation

Overview

Package abcrypt implements the abcrypt encrypted data format.

Example
ciphertext := abcrypt.EncryptWithParams([]byte(data), []byte(passphrase), 32, 3, 4)

fmt.Printf("ciphertext and input data are different: %v\n", !slices.Equal(ciphertext, []byte(data)))

plaintext, err := abcrypt.Decrypt(ciphertext, []byte(passphrase))
if err != nil {
	log.Fatal(err)
}

fmt.Printf("plaintext and input data are identical: %v\n", slices.Equal(plaintext, []byte(data)))
Output:

ciphertext and input data are different: true
plaintext and input data are identical: true

Index

Examples

Constants

View Source
const HeaderSize = 140

HeaderSize is the number of bytes of the header.

TagSize is the number of bytes of the MAC (authentication tag) of the ciphertext.

Variables

View Source
var ErrInvalidLength = errors.New("abcrypt: encrypted data is shorter than 156 bytes")

ErrInvalidLength represents an error due to the encrypted data was shorter than 156 bytes.

View Source
var ErrInvalidMagicNumber = errors.New("abcrypt: invalid magic number")

ErrInvalidMagicNumber represents an error due to the magic number (file signature) was invalid.

Functions

func Decrypt added in v0.1.2

func Decrypt(ciphertext, passphrase []byte) ([]byte, error)

Decrypt decrypts the ciphertext and returns the plaintext.

This is a convenience function for using NewDecryptor and Decryptor.Decrypt.

func Encrypt added in v0.1.2

func Encrypt(plaintext, passphrase []byte) []byte

Encrypt encrypts the plaintext and returns the ciphertext.

This uses the recommended Argon2 parameters.

This is a convenience function for using NewEncryptor and Encryptor.Encrypt.

func EncryptWithParams added in v0.1.2

func EncryptWithParams(plaintext, passphrase []byte, memoryCost, timeCost uint32, parallelism uint8) []byte

EncryptWithParams encrypts the plaintext with the given Argon2 parameters and returns the ciphertext.

This is a convenience function for using NewEncryptorWithParams and Encryptor.Encrypt.

Types

type Decryptor

type Decryptor struct {
	// contains filtered or unexported fields
}

Decryptor represents a decryptor for the abcrypt encrypted data format.

Example
dataEnc, err := os.ReadFile("testdata/data.txt.abcrypt")
if err != nil {
	log.Fatal(err)
}

fmt.Printf("input data size: %v B\n", len(dataEnc))

cipher, err := abcrypt.NewDecryptor(dataEnc, []byte(passphrase))
if err != nil {
	log.Fatal(err)
}

fmt.Printf("expected output size: %v B\n", cipher.OutLen())

plaintext, err := cipher.Decrypt()
if err != nil {
	log.Fatal(err)
}

fmt.Printf("decrypted data size: %v B\n", len(plaintext))
Output:

input data size: 170 B
expected output size: 14 B
decrypted data size: 14 B

func NewDecryptor

func NewDecryptor(ciphertext, passphrase []byte) (*Decryptor, error)

NewDecryptor creates a new Decryptor.

func (*Decryptor) Decrypt

func (d *Decryptor) Decrypt() ([]byte, error)

Decrypt decrypts the ciphertext and returns the plaintext.

func (*Decryptor) OutLen

func (d *Decryptor) OutLen() int

OutLen returns the number of output bytes of the decrypted data.

type Encryptor

type Encryptor struct {
	// contains filtered or unexported fields
}

Encryptor represents an encryptor for the abcrypt encrypted data format.

Example
fmt.Printf("input data size: %v B\n", len(data))

cipher := abcrypt.NewEncryptorWithParams([]byte(data), []byte(passphrase), 32, 3, 4)

fmt.Printf("expected output size: %v B\n", cipher.OutLen())

ciphertext := cipher.Encrypt()

fmt.Printf("encrypted data size: %v B\n", len(ciphertext))
Output:

input data size: 14 B
expected output size: 170 B
encrypted data size: 170 B

func NewEncryptor

func NewEncryptor(plaintext, passphrase []byte) *Encryptor

NewEncryptor creates a new Encryptor.

This uses the recommended Argon2 parameters.

func NewEncryptorWithParams

func NewEncryptorWithParams(plaintext, passphrase []byte, memoryCost, timeCost uint32, parallelism uint8) *Encryptor

NewEncryptorWithParams creates a new Encryptor with the given Argon2 parameters.

func (*Encryptor) Encrypt

func (e *Encryptor) Encrypt() []byte

Encrypt encrypts the plaintext and returns the ciphertext.

func (*Encryptor) OutLen

func (e *Encryptor) OutLen() int

OutLen returns the number of output bytes of the encrypted data.

type InvalidHeaderMACError added in v0.2.0

type InvalidHeaderMACError struct {
	// MAC represents the obtained MAC of the header.
	MAC [64]byte
}

InvalidHeaderMACError represents an error due to the MAC (authentication tag) of the header was invalid.

func (*InvalidHeaderMACError) Error added in v0.2.0

func (e *InvalidHeaderMACError) Error() string

Error returns a string representation of an InvalidHeaderMACError.

type InvalidMACError

type InvalidMACError struct {
	// Err represents a wrapped error.
	Err error
}

InvalidMACError represents an error due to the MAC (authentication tag) of the ciphertext was invalid.

func (*InvalidMACError) Error

func (e *InvalidMACError) Error() string

Error returns a string representation of an InvalidMACError.

func (*InvalidMACError) Unwrap

func (e *InvalidMACError) Unwrap() error

Unwrap returns the underlying error of an InvalidMACError.

type Params

type Params struct {
	// MemoryCost represents memory size in KiB.
	MemoryCost uint32 `json:"memoryCost"`

	// TimeCost represents the number of iterations.
	TimeCost uint32 `json:"timeCost"`

	// Parallelism represents the degree of parallelism.
	Parallelism uint32 `json:"parallelism"`
}

Params represents the Argon2 parameters used for the encrypted data.

Example
ciphertext, err := os.ReadFile("testdata/data.txt.abcrypt")
if err != nil {
	log.Fatal(err)
}

params, err := abcrypt.NewParams(ciphertext)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("memoryCost: %v\n", params.MemoryCost)
fmt.Printf("timeCost: %v\n", params.TimeCost)
fmt.Printf("parallelism: %v\n", params.Parallelism)
Output:

memoryCost: 32
timeCost: 3
parallelism: 4

func NewParams

func NewParams(ciphertext []byte) (*Params, error)

NewParams creates a new Params from the given ciphertext.

type UnknownVersionError

type UnknownVersionError struct {
	// Version represents the obtained version number.
	Version byte
}

UnknownVersionError represents an error due to the version was the unrecognized abcrypt version number.

func (*UnknownVersionError) Error

func (e *UnknownVersionError) Error() string

Error returns a string representation of an UnknownVersionError.

Directories

Path Synopsis
Package examples contains sample applications for the module.
Package examples contains sample applications for the module.
decrypt
Decrypt is an example of decrypting a file from the abcrypt encrypted data format.
Decrypt is an example of decrypting a file from the abcrypt encrypted data format.
encrypt
Encrypt is an example of encrypting a file to the abcrypt encrypted data format.
Encrypt is an example of encrypting a file to the abcrypt encrypted data format.
info
Info is an example of reading the Argon2 parameters from a file.
Info is an example of reading the Argon2 parameters from a file.

Jump to

Keyboard shortcuts

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