Documentation ¶
Index ¶
- Constants
- Variables
- func Decrypt(data []byte, key []byte) ([]byte, error)
- func DecryptChunk(block cipher.Block, encryptedChunk []byte, nonce []byte) ([]byte, error)
- func DecryptFileToFile(encryptedFilePath, decryptedFilePath string, key []byte, chunkSize int) error
- func Encrypt(data []byte, key []byte) ([]byte, error)
- func EncryptChunk(block cipher.Block, plaintext []byte, nonce []byte) ([]byte, error)
- func EncryptFileToFile(data io.Reader, key []byte, chunkSize int, filePath string) error
- func GenerateGCM(key []byte) (gcm cipher.AEAD, block cipher.Block, err error)
- func GenerateGCMWithNonce(key []byte) (gcm cipher.AEAD, block cipher.Block, nonce []byte, err error)
- func GenerateKey(passphrase string, salt []byte, keyLength int) ([]byte, error)
- func GenerateSalt(length int) ([]byte, error)
- func GenerateSaltAndKey(passphrase string, saltLength int, keyLength int) ([]byte, []byte, error)
- func Hash(reader io.Reader, hasher hash.Hash) ([]byte, error)
- func HashBytes(data []byte, hasher hash.Hash) []byte
- func HashBytesSHA3(data []byte) []byte
- func HashBytesToString(data []byte, hasher hash.Hash) string
- func HashBytesToStringSHA3(data []byte) string
- func HashFile(file *os.File, hasher hash.Hash) ([]byte, error)
- func HashFileSHA3(file *os.File) ([]byte, error)
- func HashString(data string, hasher hash.Hash) []byte
- func HashStringSHA3(data string) []byte
- func HashStringToString(data string, hasher hash.Hash) string
- func HashStringToStringSHA3(data string) string
- func HashWithBlake2b256(reader io.Reader, key []byte) ([]byte, error)
- func HashWithBlake2b256NoKey(reader io.Reader) ([]byte, error)
- func HashWithBlake2b512(reader io.Reader, key []byte) ([]byte, error)
- func HashWithBlake2b512NoKey(reader io.Reader) ([]byte, error)
- func ReEncrypt(data []byte, oldKey []byte, newKey []byte) ([]byte, error)
- func ReEncryptFileToFile(encryptedFilePath, decryptedFilePath string, oldKey []byte, newKey []byte, ...) error
- func StreamDecrypt(data io.Reader, key []byte) (io.Reader, error)
- func StreamEncrypt(data io.Reader, key []byte) (io.Reader, error)
- func StreamReEncrypt(data io.Reader, oldKey []byte, newKey []byte) (io.Reader, error)
- type FcryptKey
- func (k *FcryptKey) Algo() string
- func (k *FcryptKey) KeyBytes() []byte
- func (k *FcryptKey) Salt() []byte
- func (k *FcryptKey) SetAlgo(a string)
- func (k *FcryptKey) SetAll(v string, s []byte, a string, key []byte)
- func (k *FcryptKey) SetKeyBytes(key []byte)
- func (k *FcryptKey) SetSalt(s []byte)
- func (k *FcryptKey) SetVersion(v string)
- func (k *FcryptKey) Version() string
- type Key
Constants ¶
const ( // MinKeyLength is the minimum length of the encryption key in bytes. MinKeyLength = 16 // DefaultKeyLength is the default length of the encryption key in bytes. DefaultKeyLength = 32 // ScryptN is the CPU/memory cost parameter for scrypt. ScryptN = 32768 // ScryptR is the block size parameter for scrypt. ScryptR = 8 // ScryptP is the parallelization parameter for scrypt. ScryptP = 1 // MinNonceSize is the minimum size of the nonce in bytes. MinNonceSize = 12 // GCMNonceSize is the size of the nonce used in GCM mode. GCMNonceSize = 12 )
Constants and errors
Variables ¶
var ( ErrCiphertextTooShort = errors.New("ciphertext too short") ErrKeyLengthTooShort = errors.New("key length too short") ErrFailedToCreateCipher = errors.New("failed to create new cipher") ErrFailedToCreateGCM = errors.New("failed to create new GCM") ErrFailedToCreateFile = errors.New("failed to create file") ErrFailedToReadData = errors.New("failed to read data") )
Error variables
Functions ¶
func Decrypt ¶
Decrypt decrypts the given ciphertext using the provided key. It returns the plaintext or an error if decryption fails.
func DecryptChunk ¶
DecryptChunk decrypts an encrypted chunk of data using the provided block cipher, nonce, and encrypted chunk. It returns the decrypted data or an error if decryption fails.
func DecryptFileToFile ¶
func DecryptFileToFile(encryptedFilePath, decryptedFilePath string, key []byte, chunkSize int) error
DecryptFileToFile decrypts the contents of an encrypted file and writes the decrypted data to a new file. It takes the path of the encrypted file, the path of the decrypted file, the encryption key, and the chunk size as parameters. The function reads the encrypted file in chunks, decrypts each chunk using AES-GCM encryption, and writes the decrypted data to the new file. It returns an error if any operation fails.
func Encrypt ¶
Encrypt encrypts the given data using the provided key and returns the encrypted result. It uses the GCM mode of operation for encryption. The nonce is randomly generated and prepended to the encrypted data.
func EncryptChunk ¶
EncryptChunk encrypts the given plaintext using the provided block cipher and nonce. It returns the ciphertext and an error, if any.
func EncryptFileToFile ¶
EncryptFileToFile encrypts the data from the given reader using the provided key and writes it to the specified file. The encryption is done in chunks of the specified size. It uses AES encryption with GCM mode. The function returns an error if any operation fails.
func GenerateGCM ¶
GenerateGCM generates a Galois/Counter Mode (GCM) cipher.AEAD and cipher.Block using the provided key. It returns the generated gcm, block, and any error encountered during the process.
func GenerateGCMWithNonce ¶ added in v0.2.0
func GenerateGCMWithNonce(key []byte) (gcm cipher.AEAD, block cipher.Block, nonce []byte, err error)
GenerateGCMWithNonce generates a Galois/Counter Mode (GCM) cipher with a random nonce. It takes a key as input and returns the GCM cipher, the underlying block cipher, the generated nonce, and any error that occurred during the process.
func GenerateKey ¶
GenerateKey generates a key using the provided passphrase, salt, and key length. It uses the scrypt key derivation function to derive the key from the passphrase and salt. The key length specifies the desired length of the generated key in bytes. Returns the generated key as a byte slice and any error encountered during the key generation process.
func GenerateSalt ¶
GenerateSalt generates a random salt of the specified length. It uses the crypto/rand package to generate cryptographically secure random bytes. The length parameter specifies the number of bytes to generate. It returns the generated salt as a byte slice and any error encountered during the generation process.
func GenerateSaltAndKey ¶ added in v0.2.2
GenerateSaltAndKey generates the salt and key using the provided passphrase and key length. It first generates the salt of the specified length and then derives the key using that salt.
func Hash ¶ added in v0.2.0
Hash calculates the hash of the given io.Reader using the provided hash.Hash.
func HashBytes ¶ added in v0.2.0
HashBytes calculates the hash of the given byte slice using the provided hash.Hash.
func HashBytesSHA3 ¶ added in v0.2.0
HashBytesSHA3 calculates the SHA3-256 hash of the given data. It uses the HashBytes function with a new SHA3-256 hash instance.
func HashBytesToString ¶ added in v0.2.0
HashBytesToString calculates the hash of the given byte slice using the provided hash.Hash.
func HashBytesToStringSHA3 ¶ added in v0.2.0
HashBytesToStringSHA3 converts a byte slice to a string representation of its SHA3 hash. It takes a byte slice `data` as input and returns the hexadecimal string representation of the SHA3 hash.
func HashFile ¶ added in v0.2.0
HashFile calculates the hash of the given file using the provided hash.Hash.
func HashFileSHA3 ¶ added in v0.2.0
HashFileSHA3 calculates the SHA3-256 hash of the given file. It takes a *os.File as input and returns the hash as a byte slice. If an error occurs during the hashing process, it is returned as the second value.
func HashString ¶
HashString calculates the hash of the given string using the provided hash.Hash.
func HashStringSHA3 ¶ added in v0.2.0
HashStringSHA3 hashes the given string using SHA3-256 algorithm and returns the resulting hash as a byte slice.
func HashStringToString ¶
HashStringToString calculates the hash of the given string using the provided hash.Hash.
func HashStringToStringSHA3 ¶ added in v0.2.0
HashStringToStringSHA3 hashes a string using SHA3-256 algorithm and returns the hashed value as a string.
func HashWithBlake2b256 ¶ added in v0.2.0
HashWithBlake2b256 hashes the contents of the provided io.Reader using BLAKE2b-256. It returns the computed hash as a byte slice.
func HashWithBlake2b256NoKey ¶ added in v0.2.0
HashWithBlake2b256NoKey calculates the Blake2b-256 hash of the data read from the given reader, without using any key. It is a convenience function that calls HashWithBlake2b256 with a nil key. The resulting hash is returned as a byte slice. If an error occurs during the hashing process, it is returned along with a nil byte slice.
func HashWithBlake2b512 ¶ added in v0.2.0
HashWithBlake2b512 hashes the contents of the provided io.Reader using BLAKE2b-512. It returns the computed hash as a byte slice.
func HashWithBlake2b512NoKey ¶ added in v0.2.0
HashWithBlake2b512NoKey calculates the Blake2b-512 hash of the data read from the given reader, without using any key. It internally calls the HashWithBlake2b512 function with a nil key. It returns the hash as a byte slice and any error encountered during the hashing process.
func ReEncrypt ¶
ReEncrypt re-encrypts the given data using the oldKey and then encrypts it again using the newKey. It returns the re-encrypted data or an error if the encryption process fails.
func ReEncryptFileToFile ¶ added in v0.2.0
func ReEncryptFileToFile(encryptedFilePath, decryptedFilePath string, oldKey []byte, newKey []byte, chunkSize int) error
ReEncryptFileToFile re-encrypts the contents of an encrypted file using a new encryption key and writes the decrypted contents to a new file.
Parameters: - encryptedFilePath: The path to the encrypted file. - decryptedFilePath: The path to the new file where the decrypted contents will be written. - oldKey: The old encryption key used to encrypt the file. - newKey: The new encryption key to be used for re-encryption. - chunkSize: The size of each chunk to be read from the encrypted file.
Returns: - An error if any error occurs during the re-encryption process, or nil if the re-encryption is successful.
Example usage:
err := ReEncryptFileToFile("/path/to/encrypted/file", "/path/to/decrypted/file", oldKey, newKey, 1024) if err != nil { fmt.Println("Error:", err) }
func StreamDecrypt ¶
StreamDecrypt decrypts the data from the given io.Reader using the provided key. It returns an io.Reader that can be used to read the decrypted data, along with any error encountered. The decryption is performed using the AES-GCM mode of operation. The key parameter is the secret key used for decryption. The data parameter is the encrypted data that needs to be decrypted. The returned io.Reader can be used to read the decrypted data. If an error occurs during decryption, it is returned along with a nil io.Reader.
func StreamEncrypt ¶
StreamEncrypt takes an input data stream and a key, and returns an encrypted data stream along with any error encountered. The function generates a GCM (Galois/Counter Mode) cipher using the provided key, and then generates a random nonce. It uses the GCM cipher in CTR (Counter) mode to create a cipher stream reader, which encrypts the input data stream. The encrypted data stream is returned along with a possible error.
func StreamReEncrypt ¶
StreamReEncrypt re-encrypts the data from the given reader using the oldKey and then encrypts it again using the newKey. It returns an io.Reader containing the re-encrypted data. If any error occurs during the decryption or encryption process, it returns nil and the error.
Types ¶
type FcryptKey ¶
type FcryptKey struct {
// contains filtered or unexported fields
}
ScryptKey struct implements the Key interface
func NewFcryptKey ¶ added in v0.2.1
NewFcryptKey creates a new FcryptKey with the specified version, salt, algorithm, and key.
func (*FcryptKey) SetAlgo ¶ added in v0.2.1
SetAlgo sets the encryption algorithm for the FcryptKey. The algorithm should be a string representing the desired encryption algorithm.
func (*FcryptKey) SetAll ¶ added in v0.2.1
SetAll sets the values of the FcryptKey struct. It takes in the version string, salt byte slice, algorithm string, and key byte slice as parameters.
func (*FcryptKey) SetKeyBytes ¶ added in v0.2.1
SetKeyBytes sets the key bytes for the FcryptKey instance. The key parameter is a byte slice containing the key bytes.
func (*FcryptKey) SetSalt ¶ added in v0.2.1
SetSalt sets the salt value for the FcryptKey. The salt is used as an additional input to the key derivation function, making it harder to perform precomputed dictionary attacks.
func (*FcryptKey) SetVersion ¶ added in v0.2.1
SetVersion sets the version of the FcryptKey.