1.0.10 • Published 4 months ago

@jihyunlab/secret v1.0.10

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

@jihyunlab/secret

Version Downloads Last commit License Linter code style: prettier\ Build Lint codecov

@jihyunlab/secret provides text and file encryption functions that can be easily used without separate implementation.\ @jihyunlab/secret provides an encryption function using AES-256-GCM and securely manages the encryption key by using environment variables as the encryption key. Additionally, you can install the command line interface tool @jihyunlab/secret-cli.

Requirements

Node.js

Setup

npm i @jihyunlab/secret

Encryption key

If you register JIHYUNLAB_SECRET_KEY in a system or user environment variable, that environment variable will be used as the encryption key during encryption.

export JIHYUNLAB_SECRET_KEY=YourKey

Text encryption

Text encryption encrypts the input text and returns an encrypted hex string.

import { Text } from '@jihyunlab/secret';

const encrypted = Text.encrypt('string');
const decrypted = Text.decrypt(encrypted);

Instead of using an encryption key from an environment variable, you can input the key directly.

const encrypted = Text.encrypt('string', 'your key');
const decrypted = Text.decrypt(encrypted, 'your key');

File encryption

File encryption encrypts the input file and returns an encrypted buffer object.

import { File } from '@jihyunlab/secret';

const encrypted = File.encrypt('file');
const decrypted = File.decrypt('file_enc');

File encryption and decryption results can be immediately exported to another file.

const encrypted = File.encrypt('file', 'file_enc');
const decrypted = File.decrypt('file_enc', 'file_dec');

Instead of using an encryption key from an environment variable, you can input the key directly.

const encrypted = File.encrypt('file', 'file_enc', 'your key');
const decrypted = File.decrypt('file_enc', 'file_dec', 'your key');

If you want to mix text and file encryption features, please see below.

import { Text, File } from '@jihyunlab/secret';
import { writeFileSync } from 'fs';

const encrypted = Text.encrypt('string');
writeFileSync('file_enc', Buffer.from(encrypted, 'hex'));

const decrypted = File.decrypt('file_enc');
decrypted.toString('utf8');

@jihyunlab/crypto

You can easily use the encryption function of @jihyunlab/crypto.

import { Crypto } from '@jihyunlab/secret';

const encrypted = Crypto.encrypt.hex('string');
const decrypted = Crypto.decrypt.hex(encrypted);

You can implement cryptographic functions using buffers.

const encrypted = Crypto.encrypt.buffer(Buffer.from('string'));
const decrypted = Crypto.decrypt.buffer(encrypted);

You can use predefined functions to select the output type of the encrypted text and the input type of the text to be decrypted.

Crypto.encrypt.hex('string');
Crypto.decrypt.hex(encrypted);

Crypto.encrypt.binary('string');
Crypto.decrypt.binary(encrypted);

Crypto.encrypt.base64('string');
Crypto.decrypt.base64(encrypted);

Crypto.encrypt.base64url('string');
Crypto.decrypt.base64url(encrypted);

Crypto.encrypt.buffer(Buffer.from('string'));
Crypto.decrypt.buffer(encrypted);

Crypto.encrypt.uint8Array(Buffer.from('string'));
Crypto.decrypt.uint8Array(encrypted);

You can choose the input and output type of text.\ Input and output text types must be types defined in Node.js.

const encrypted = Crypto.encrypt.string('string', 'utf8', 'base64url');
const decrypted = Crypto.decrypt.string(encrypted, 'base64url', 'utf8');

Instead of using an encryption key from an environment variable, you can input the key directly.

const encrypted = Crypto.encrypt.hex('string', 'utf8', 'your key');
const decrypted = Crypto.decrypt.hex(encrypted, 'utf8', 'your key');

Encrypted .env

You can decrypt and use .env encrypted with @jihyunlab/secret-cli in a simple way.

TEXT=d879f8dfb00b7f9d73bf755569726ed296332765988e28dda664350291f4ca382cff501e82
import { Env } from '@jihyunlab/secret';
import { config } from 'dotenv';

Env.load(config());
console.log(process.env.TEXT);

If you use a library other than dotenv, you can still decrypt the contents of environment variables.

const text = Text.decrypt(process.env.TEXT || '');
console.log(text);

Credits

Authored and maintained by JihyunLab <info@jihyunlab.com>

License

Open source licensed as MIT.

1.0.9

4 months ago

1.0.8

4 months ago

1.0.10

4 months ago

1.0.7

4 months ago

1.0.6

4 months ago

1.0.5

4 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago