npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sequencemedia/crypto

v1.2.268

Published

Encrypt and decrypt in Node and Bash

Downloads

1,140

Readme

@sequencemedia/crypto

Encrypt and decrypt in Node and Bash

Node

The package main exports two functions, encrypt and decrypt

import {
  encrypt,
  decrypt
} from '@sequencemedia/crypto'

encrypt

function encrypt(
  buffer: Buffer,
  secret: string,
  bytes?: number,
  algorithm?: string
): Buffer

Only buffer and secret are required

  • buffer is the data to encrypt
  • secret is the secret key to use for encryption

Both bytes and algorithm are optional

  • bytes is the number of random bytes to use for the encryption initialisation vector. The default is 16
  • algorithm is the algorithm to use for encryption. The default is aes-256-ctr

decrypt

function decrypt(
  buffer: Buffer,
  secret: string,
  bytes?: number,
  algorithm?: string
): Buffer

Only buffer and secret are required

  • buffer is the data to decrypt
  • secret is the secret key to use for decryption

Both bytes and algorithm are optional

  • bytes is the number of bytes to slice from the buffer for the decryption initialisation vector. The default is 16
  • algorithm is the algorithm to use for decryption. The default is aes-256-ctr

Bash

The package contains three scripts

  1. crypto.sh
  2. encrypt.sh
  3. decrypt.sh

Script crypto.sh exports four functions to consume in your own Bash scripts

source ./crypto.sh
  1. encrypt
  2. decrypt
  3. encrypt_directory
  4. decrypt_directory

Scripts encrypt.sh and decrypt.sh can be executed at the command line

Bash functions

encrypt

Requires CRYPTO_KEY as a variable in the Bash environment and a file path to encrypt

CRYPTO_KEY='secret'
encrypted_file_data=$(encrypt "./file.txt")
CRYPTO_KEY='secret'
encrypt "./file.txt" > "./encrypted.txt"

decrypt

Requires CRYPTO_KEY as a variable in the Bash environment and a file path to decrypt

CRYPTO_KEY='secret'
file_data=$(decrypt "./encrypted.txt")
CRYPTO_KEY='secret'
decrypt "./encrypted.txt" > "./file.txt"

encrypt_directory

Requires CRYPTO_KEY as a variable in the Bash environment and a directory path to encrypt

  • The first argument is the origin directory of files to encrypt
  • The second argument is the destination directory for the encrypted files
CRYPTO_KEY='secret'
encrypt_directory "./directory" "./encrypted"

decrypt_directory

Requires CRYPTO_KEY as a variable in the Bash environment and a directory path to decrypt

  • The first argument is the origin directory of files to decrypt
  • The second argument is the destination directory for the decrypted files
CRYPTO_KEY='secret'
decrypt_directory "./encrypted" "./directory"

Bash scripts

encrypt.sh

Requires CRYPTO_KEY as a variable in the Bash environment

  • You can provide either file or directory paths
  • You can provide --verbose or -v
File paths
  • A file origin path to encrypt
  • A file destination path for the encrypted file
CRYPTO_KEY='secret' ./encrypt.sh \
  --origin "./file.txt" \
  --destination "./encrypted.txt"
Directory paths
  • A directory origin path to encrypt
  • A directory destination path for encrypted files
CRYPTO_KEY='secret' ./encrypt.sh \
  --origin "./directory" \
  --destination "./encrypted"

decrypt.sh

Requires CRYPTO_KEY as a variable in the Bash environment

  • You can provide either file or directory paths
  • You can provide --verbose or -v
File paths
  • A file origin path to decrypt
  • A file destination path for the decrypted file
CRYPTO_KEY='secret' ./decrypt.sh \
  --origin "./encrypted.txt" \
  --destination "./file.txt"
Directory paths
  • A directory origin path to decrypt
  • A directory destination path for decrypted files
CRYPTO_KEY='secret' ./decrypt.sh \
  --origin "./encrypted" \
  --destination "./directory"