1.0.8 • Published 6 months ago

@shgysk8zer0/aes-gcm v1.0.8

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

@shgysk8zer0/aes-gcm

A JWK-base crypto library using AES-GCM secret keys

CodeQL Node CI Lint Code Base

GitHub license GitHub last commit GitHub release GitHub Sponsors

npm node-current npm bundle size gzipped npm

GitHub followers GitHub forks GitHub stars Twitter Follow

Donate using Liberapay


Description

A JWK-based crypto library using AES-GCM secret keys. This library provides a set of functions for generating, encrypting, decrypting, signing, and verifying cryptographic keys and data. It supports various cryptographic algorithms and formats, making it easy to handle secure data operations in JavaScript.

Installation

Using npm

npm install @shgysk8zer0/aes-gcm

Using unpkg.com

<script type="importmap">
{
  "imports": {
    "@shgysk8zer0/aes-gcm": "https://unpkg.com/@shgysk8zer0/aes-gcm?module"
  }
}
</script>
<script type="module">
  import { generateSecretKey } from '@shgysk8zer0/aes-gcm';

  const key = await generateSecretKey();
  console.log(key);
</script>

Usage Examples

Generating a Secret Key

import { generateSecretKey } from '@shgysk8zer0/aes-gcm';

const key = await generateSecretKey();
console.log(key);

Documentation for generateSecretKey

Encrypting and Decrypting Data

import { encrypt, decrypt, TEXT } from '@shgysk8zer0/aes-gcm';

const key = await generateSecretKey();
const data = 'Hello, World!';
const encrypted = await encrypt(key, data);
const decrypted = await decrypt(key, encrypted, { output: TEXT });

console.log(decrypted); // 'Hello, World!'

Documentation for encrypt and Documentation for decrypt

Encrypting and Decrypting Files

import { encryptFile, decryptFile } from '@shgysk8zer0/aes-gcm';

const key = await generateSecretKey();
const file = new File(['Hello, World!'], 'hello.txt', { type: 'text/plain' });
const encryptedFile = await encryptFile(key, file);
const decryptedFile = await decryptFile(key, encryptedFile);

console.log(await decryptedFile.text()); // 'Hello, World!'

Documentation for encryptFile and Documentation for decryptFile

Creating a Secret Key from a Password

import { createSecretKeyFromPassword } from '@shgysk8zer0/aes-gcm';

const password = 'super-secret-password';
const key = await createSecretKeyFromPassword(password);
console.log(key);

Documentation for createSecretKeyFromPassword

Hashing Data

import { hash, HEX } from '@shgysk8zer0/aes-gcm';

const data = 'Hello, World!';
const hashed = await hash(data, { output: HEX });

console.log(hashed);

Documentation for hash

Signing and Verifying Data

import { sign, verifySignature } from '@shgysk8zer0/aes-gcm';

const key = await generateSecretKey();
const data = 'Hello, World!';
const signature = await sign(key, data);
const isValid = await verifySignature(key, data, signature);

console.log(isValid); // true

Documentation for sign and Documentation for verifySignature

API Documentation

Methods

MethodDescriptionExample
generateSecretKey(options)Generates a new secret key.const key = await generateSecretKey();
encrypt(key, thing, options)Encrypts data using a provided CryptoKey.const encrypted = await encrypt(key, data);
decrypt(key, thing, options)Decrypts data using a provided CryptoKey.const decrypted = await decrypt(key, encrypted);
encryptFile(key, file, name)Encrypts a file using a provided CryptoKey.const encryptedFile = await encryptFile(key, file);
decryptFile(key, file)Decrypts a file using a provided CryptoKey.const decryptedFile = await decryptFile(key, encryptedFile);
createSecretKeyFromPassword(pass, options)Creates a secret key from a password.const key = await createSecretKeyFromPassword(password);
hash(thing, options)Hashes data using a specified algorithm.const hashed = await hash(data);
sign(key, thing, options)Signs data using a provided CryptoKey.const signature = await sign(key, data);
verifySignature(key, source, signature, options)Verifies the signature of a given data using a provided CryptoKey.const isValid = await verifySignature(key, data, signature);

Options

Generate Secret Key

generateSecretKey(options)

OptionTypeDefaultDescription
namestring'AES-GCM'The name of the algorithm.
lengthnumber256The desired key length in bits.
extractablebooleantrueWhether the key should be extractable.
usagesstring[]['encrypt', 'decrypt']Usages for the key.

Encrypt Data

encrypt(key, thing, options)

OptionTypeDefaultDescription
ivUint8ArrayundefinedInitialization vector (IV) used for encryption.
outputstring'ui8'Output format for the encrypted data.

Decrypt Data

decrypt(key, thing, options)

OptionTypeDefaultDescription
inputstring'base64'Input format of the encrypted data when thing is a string.
outputstring'buffer'Output format for the decrypted data.

Create Secret Key from Password

createSecretKeyFromPassword(pass, options)

OptionTypeDefaultDescription
namestring'AES-GCM'The name of the key algorithm.
lengthnumber256The desired key length in bits.
hashstring'SHA-256'The hash algorithm to use for PBKDF2.
iterationsnumber100000The number of iterations for PBKDF2.
extractablebooleanfalseWhether the key can be extracted.
usagesstring[]['encrypt', 'decrypt']The intended usages for the key.

Hash Data

hash(thing, options)

OptionTypeDefaultDescription
algostring'SHA-256'The hashing algorithm to use.
outputstring'buffer'The output format for the hash.

Sign Data

sign(key, thing, options)

OptionTypeDefaultDescription
algostring'SHA-256'The hashing algorithm to use.
ivUint8ArrayundefinedInitialization vector (IV) used for encryption.
outputstring'ui8'Output format for the signed data.

Verify Signature

verifySignature(key, source, signature, options)

OptionTypeDefaultDescription
algostring'SHA-256'The hashing algorithm used for signing.
inputstring'hex'The input format of the signature if it's a string.

Encrypt File

encryptFile(key, file, name)

OptionTypeDefaultDescription
keyCryptoKeyrequiredA cryptographic key for encryption.
fileFilerequiredThe file to encrypt.
namestringDate.now().toString(36) + FILE_EXTThe name of the encrypted file.

Decrypt File

decryptFile(key, file)

OptionTypeDefaultDescription
keyCryptoKeyrequiredA cryptographic key for decryption.
fileFilerequiredThe file to decrypt.
1.0.8

6 months ago

1.0.7

7 months ago

1.0.5

7 months ago

1.0.4

7 months ago

1.0.3

7 months ago

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago