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

@shgysk8zer0/aes-gcm

v1.0.7

Published

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

Downloads

531

Readme

@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

| Method | Description | Example | |--------|-------------|---------| | 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)

| Option | Type | Default | Description | |--------|------|---------|-------------| | name | string | 'AES-GCM' | The name of the algorithm. | | length | number | 256 | The desired key length in bits. | | extractable | boolean | true | Whether the key should be extractable. | | usages | string[] | ['encrypt', 'decrypt'] | Usages for the key. |

Encrypt Data

encrypt(key, thing, options)

| Option | Type | Default | Description | |--------|------|---------|-------------| | iv | Uint8Array | undefined | Initialization vector (IV) used for encryption. | | output | string | 'ui8' | Output format for the encrypted data. |

Decrypt Data

decrypt(key, thing, options)

| Option | Type | Default | Description | |--------|------|---------|-------------| | input | string | 'base64' | Input format of the encrypted data when thing is a string. | | output | string | 'buffer' | Output format for the decrypted data. |

Create Secret Key from Password

createSecretKeyFromPassword(pass, options)

| Option | Type | Default | Description | |--------|------|---------|-------------| | name | string | 'AES-GCM' | The name of the key algorithm. | | length | number | 256 | The desired key length in bits. | | hash | string | 'SHA-256' | The hash algorithm to use for PBKDF2. | | iterations | number | 100000 | The number of iterations for PBKDF2. | | extractable | boolean | false | Whether the key can be extracted. | | usages | string[] | ['encrypt', 'decrypt'] | The intended usages for the key. |

Hash Data

hash(thing, options)

| Option | Type | Default | Description | |--------|------|---------|-------------| | algo | string | 'SHA-256' | The hashing algorithm to use. | | output | string | 'buffer' | The output format for the hash. |

Sign Data

sign(key, thing, options)

| Option | Type | Default | Description | |--------|------|---------|-------------| | algo | string | 'SHA-256' | The hashing algorithm to use. | | iv | Uint8Array | undefined | Initialization vector (IV) used for encryption. | | output | string | 'ui8' | Output format for the signed data. |

Verify Signature

verifySignature(key, source, signature, options)

| Option | Type | Default | Description | |--------|------|---------|-------------| | algo | string | 'SHA-256' | The hashing algorithm used for signing. | | input | string | 'hex' | The input format of the signature if it's a string. |

Encrypt File

encryptFile(key, file, name)

| Option | Type | Default | Description | |--------|------|---------|-------------| | key | CryptoKey | required | A cryptographic key for encryption. | | file | File | required | The file to encrypt. | | name | string | Date.now().toString(36) + FILE_EXT | The name of the encrypted file. |

Decrypt File

decryptFile(key, file)

| Option | Type | Default | Description | |--------|------|---------|-------------| | key | CryptoKey | required | A cryptographic key for decryption. | | file | File | required | The file to decrypt. |