Code Monkey home page Code Monkey logo

keygrip's Introduction

keygrip

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

Keygrip is a node.js module for signing and verifying data through a rotating credential system, in which new server keys can be added and old ones removed regularly, without invalidating client credentials.

Install

$ npm install keygrip

API

keys = Keygrip(keylist)

This creates a new Keygrip based on the provided keylist.

var Keygrip = require('keygrip')
keys = Keygrip(["SEKRIT2", "SEKRIT1"])

The keylist is an array of all valid keys for signing, in descending order of freshness; new keys should be unshifted into the array and old keys should be popped.

The tradeoff here is that adding more keys to the keylist allows for more granular freshness for key validation, at the cost of a more expensive worst-case scenario for old or invalid hashes.

Keygrip keeps a reference to this array to automatically reflect any changes. This reference is stored using a closure to prevent external access.

When using Keygrip to encrypt and decrypt data, each key's length is important, as it should be at least the minimum key length for the cipher you are using, otherwise it'll be padded with NULs. The default cipher, AES 256, should be a 32 character string key, for example.

var buf = keys.sign(data)

This creates a HMAC based on the first key in the keylist, and outputs it as a buffer.

Uses .hash= as the underlying algorithm.

var index = keys.indexOf(data)

This loops through all of the keys currently in the keylist until the digest of the current key matches the given digest, at which point the current index is returned. If no key is matched, -1 is returned.

The idea is that if the index returned is greater than 0, the data should be re-signed to prevent premature credential invalidation, and enable better performance for subsequent challenges.

var bool = keys.verify(data)

This uses index to return true if the digest matches any existing keys, and false otherwise.

var buf = keys.encrypt(message, [iv])

Creates an encrypted message as a buffer based on the first key in the keylist and optionally based on an initialization vector.

Uses .cipher= as the underlying algorithm. Note that iv length is important.

var [buf, i] = keys.decrypt(message, [iv])

Decrypts a message, optionally with an initialization vector. Returns a buffer as buf. Also returns i, the index of the key used. If i !== 0, you may want to re-encrypt the message to use the latest key.

keys.hash=

Set the hashing algorithm for signing, defaulting to sha256.

.cipher=

Set the algorithm used for message encryption, defaulting to aes-256-cbc.

License

MIT

keygrip's People

Contributors

jed avatar jonathanong avatar dougwilson avatar scriby avatar emilbayes avatar ggoodale avatar guybedford avatar bobrik avatar jwatte avatar miksago avatar fresheneesz avatar

Watchers

James Cloos avatar  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.