Code Monkey home page Code Monkey logo

micro-aes-gcm's Introduction

micro-aes-gcm

Authenticated data encryption with AES-GCM. Allows to encrypt arbitrary data in a cryptographically secure & modern way.

A simple wrapper over node.js and browser aes-gcm implementations. No dependencies.

Usage

npm install micro-aes-gcm

import * as aes from "micro-aes-gcm";
const key = Uint8Array.from([
  64, 196, 127, 247, 172,   2,  34,
  159,   6, 241,  30, 174, 183, 229,
  41, 114, 253, 122, 119, 168, 177,
  243, 155, 236, 164, 159,  98,  72,
  162, 243, 224, 195
]);
const plaintext = "Hello world";
const ciphertext = await aes.encrypt(key, message);
const plaintext = await aes.decrypt(key, ciphertext);
console.log(aes.toUTF8(plaintext) === message);
// Also works in browsers

API

function encrypt(key: Uint8Array, plaintext: Uint8Array|string): Promise<Uint8Array>;

plaintext in encrypt can be either a Uint8Array, or a string. If it's a string, new TextDecoder().encode(plaintext) would be executed before passing it further.

function decrypt(key: Uint8Array, ciphertext: Uint8Array): Promise<Uint8Array>;

Note that decrypt always returns Uint8Array. If you've encrypted UTF-8 string, toUTF8(result) should be enough to get it back.

Internals

Secretbox receives one key, and one plaintext.

The output format is: iv + ciphertext + mac:

  • iv is 12 bytes; it's an initialization vector for AES-GCM mode.
  • ciphertext length depends on plaintext
  • mac is 16 bytes; AES-GCM calculates this authentication tag for us.

To slice through IV and MAC, you can use Uint8Array.prototype.slice():

const ciphertext = await encrypt(key, plaintext);
const iv = ciphertext.slice(0, 12);
const mac = ciphertext.slice(-16);

Notes

DJB's secretbox uses XSalsa20-Poly1305. We'll use AES-GCM, which is also a good choice. DJB mentioned the AES box in his TODOs.

AES has been selected over Salsa, because it's natively implemented in Node & browsers and doesn't require any 3rd-party libraries.

License

MIT (c) Paul Miller (https://paulmillr.com), see LICENSE file.

micro-aes-gcm's People

Contributors

paulmillr avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.