@fnet/zip-and-encrypt
v0.1.12
Published
A simple utility to encrypt and zip a file or directory
Downloads
30
Readme
@fnet/zip-and-encrypt
This project provides a simple utility for encrypting and zipping the contents of a folder using AES-GCM encryption. It is designed to securely store or transfer folder contents by combining compression and encryption into a single step.
How It Works
The tool accepts a folder as input, compresses its contents into a ZIP file, and then encrypts the file using AES-256-GCM. You can choose to either save the encrypted file to disk or return it as a memory buffer.
Key Features
- Folder Compression: Compresses an entire folder into a single ZIP archive.
- AES-256-GCM Encryption: Utilizes modern cryptography standards for secure encryption.
- Flexible Output: Allows encrypted content to be saved as a file or used directly in memory.
- Error Handling: Provides feedback on errors related to input validation and processing.
Conclusion
The @fnet/zip-and-encrypt utility is a straightforward solution for those needing to compress and encrypt folder contents quickly and securely. It's useful for protecting sensitive data during storage or transmission without unnecessary complexity.
Developer Guide for @fnet/zip-and-encrypt
Overview
The @fnet/zip-and-encrypt
library provides a straightforward way to compress and encrypt directories using AES-GCM encryption. By taking a specified directory and a private key, the library can create a secure, encrypted zip file. This is ideal for securely archiving data that needs to be kept confidential.
Installation
You can install the library using either npm or yarn:
npm install @fnet/zip-and-encrypt
or
yarn add @fnet/zip-and-encrypt
Usage
The main functionality is provided through a single function encryptAndZip
, which zips the contents of a specified directory and encrypts the result. This function can be used to either return the encrypted data as a buffer or save it directly to disk.
Example Usage
Here's a basic example demonstrating how to use the encryptAndZip
function:
import encryptAndZip from '@fnet/zip-and-encrypt';
// Define input parameters
const privateKey = 'yourhexencodedprivatekeyhere';
const inputDirectoryPath = './path/to/your/directory';
const outputPath = './path/to/your/output/encrypted.zip';
// Using the encryptAndZip function
encryptAndZip({ privateKey, input: inputDirectoryPath, output: outputPath })
.then((result) => {
console.log('Encrypted file successfully created:', result);
})
.catch((error) => {
console.error('An error occurred:', error);
});
In this example, the function takes a private key, an input directory, and an optional output path. If an output path is specified, the encrypted zip is saved to disk. If not, the encrypted content is returned as a Buffer.
Memory Mode
If you prefer to handle the encrypted content directly in memory instead of saving it to disk, you can omit the output
parameter:
encryptAndZip({ privateKey, input: inputDirectoryPath })
.then((encryptedBuffer) => {
console.log('Encrypted content as Buffer:', encryptedBuffer);
})
.catch((error) => {
console.error('An error occurred:', error);
});
Examples
Encrypt a Directory with Output to Disk
encryptAndZip({
privateKey: 'yourhexencodedprivatekeyhere',
input: './path/to/directory',
output: './encrypted/file.zip'
}).then((outputPath) => {
console.log('File saved to:', outputPath);
}).catch(console.error);
Encrypt a Directory with Buffer Output
encryptAndZip({
privateKey: 'yourhexencodedprivatekeyhere',
input: './path/to/directory'
}).then((buffer) => {
// Use the encrypted buffer as needed
console.log('Encrypted buffer:', buffer);
}).catch(console.error);
Acknowledgement
This project relies on powerful tools such as Node.js' built-in crypto
module and archiver
for handling compression functionalities. These underpin the core features of @fnet/zip-and-encrypt
, providing reliable encryption and zipping capabilities.
Input Schema
$schema: https://json-schema.org/draft/2020-12/schema
title: EncryptAndZipArguments
type: object
properties:
privateKey:
type: string
description: The private key used for encryption (hex encoded).
input:
type: string
description: The folder whose contents will be zipped and encrypted.
output:
type: string
description: The output file path where the encrypted content will be saved. If
not provided, returns the encrypted content as a Buffer.
required:
- privateKey
- input