npm

@fnet/decrypt-and-unzip

0.1.8 • Public • Published

@fnet/decrypt-and-unzip

This project provides a straightforward solution for decrypting and unzipping files that have been encrypted using AES-GCM. It is designed to help users seamlessly access and extract contents from encrypted ZIP files, either storing them directly to memory or saving them to a specified location on disk.

How It Works

The tool operates by taking an encrypted file or buffer and a private key as inputs. Using AES-256-GCM encryption, it carefully decrypts the file. After decryption, it extracts the contents of the decrypted ZIP file. If an output path is provided, files are saved to that location on disk, otherwise, they are stored in memory and returned as an object.

Key Features

  • Decryption with AES-GCM: Ensures secure decryption of files using a specified private key.
  • Flexible Input Handling: Supports both file paths and buffer inputs for encrypted content.
  • ZIP Extraction: Retrieves and processes contents inside ZIP files.
  • Output Flexibility: Provides options to store extracted files either in memory or on disk.

Conclusion

In summary, this project offers a reliable and efficient means to decrypt and unzip files securely. Whether you need to handle files in-memory for quick access or extract them to disk for further use, this tool caters to both needs without any unnecessary complexity.

@fnet/decrypt-and-unzip Developer Guide

Overview

The @fnet/decrypt-and-unzip library provides a straightforward solution for decrypting a file encrypted with AES-GCM and unzipping its contents. Whether you need to handle files in memory or extract them to disk, this library simplifies the process into a single function call.

Installation

To install the library, use either npm or yarn:

npm install @fnet/decrypt-and-unzip

or

yarn add @fnet/decrypt-and-unzip

Usage

The core functionality of this library is provided by the decryptAndUnzip function. It allows you to decrypt and unzip files using a private key. The function supports two modes: extracting files to memory or to a specified output directory on disk.

Function Signature

decryptAndUnzip({ privateKey, input, output })
  • privateKey: A hex-encoded string representing the private key used for decryption.
  • input: A string specifying the file path of the encrypted file or a Buffer with the encrypted content.
  • output: An optional string specifying the directory where the files will be extracted. If omitted, files will be returned in memory.

Example Usage

Extract Files to Memory

import decryptAndUnzip from '@fnet/decrypt-and-unzip';

(async () => {
  const privateKey = 'your-hex-encoded-private-key';
  const inputFilePath = 'path/to/encrypted-file.enc';

  try {
    const filesInMemory = await decryptAndUnzip({ privateKey, input: inputFilePath });
    console.log('Decrypted files:', filesInMemory);
  } catch (error) {
    console.error('Failed to decrypt and unzip:', error);
  }
})();

Extract Files to Disk

import decryptAndUnzip from '@fnet/decrypt-and-unzip';

(async () => {
  const privateKey = 'your-hex-encoded-private-key';
  const inputFilePath = 'path/to/encrypted-file.enc';
  const outputDirectory = 'path/to/extracted-files';

  try {
    const outputPath = await decryptAndUnzip({ privateKey, input: inputFilePath, output: outputDirectory });
    console.log(`Files extracted to: ${outputPath}`);
  } catch (error) {
    console.error('Error during decryption and extraction:', error);
  }
})();

Examples

Example 1: Decrypting and Unzipping to Memory

Decrypt and process the contents of an encrypted file in memory without creating temporary files on disk.

Example 2: Decrypting and Unzipping to a Directory

Decrypt an encrypted archive and extract its contents to a specified directory, organizing files exactly as structured in the archive.

Acknowledgement

This library utilizes unzipper for handling extraction of ZIP files.

Input Schema

$schema: https://json-schema.org/draft/2020-12/schema
title: DecryptAndUnzipInput
type: object
properties:
  privateKey:
    type: string
    description: The private key used for decryption (hex encoded).
  input:
    oneOf:
      - type: string
        description: The input file path where the encrypted content is located.
      - type: string
        contentEncoding: base64
        description: The Buffer with encrypted content.
  output:
    type: string
    description: The output folder where files will be extracted (only used when
      mode is 'disk').
required:
  - privateKey
  - input

Readme

Keywords

none

Package Sidebar

Install

npm i @fnet/decrypt-and-unzip

Weekly Downloads

9

Version

0.1.8

License

MIT

Unpacked Size

23.8 kB

Total Files

6

Last publish

Collaborators

  • serdark
  • serdar986
  • gboyraz