Socket
Socket
Sign inDemoInstall

jwa

Package Overview
Dependencies
3
Maintainers
3
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

jwa

JWA implementation (supports all JWS algorithms)


Version published
Maintainers
3
Weekly downloads
21,231,747
decreased by-6.84%

Weekly downloads

Package description

What is jwa?

The jwa npm package is a JavaScript implementation of JSON Web Algorithms (JWA) as specified in RFC 7518. It provides functionality to perform cryptographic operations such as signing and verifying signatures using various algorithms.

What are jwa's main functionalities?

Signing

This feature allows you to create a signature for a given payload using a specified algorithm and secret. The 'HS256' algorithm is used in this example to sign the 'Hello, world!' payload.

"use strict";\nconst jwa = require('jwa');\nconst algorithm = 'HS256';\nconst hmac = jwa(algorithm);\nconst secret = 'mysecret';\nconst payload = 'Hello, world!';\nconst signature = hmac.sign(payload, secret);\nconsole.log('Signature:', signature);

Verifying Signatures

This feature allows you to verify a signature for a given payload using the same algorithm and secret that were used to sign the payload. It returns a boolean indicating whether the signature is valid.

"use strict";\nconst jwa = require('jwa');\nconst algorithm = 'HS256';\nconst hmac = jwa(algorithm);\nconst secret = 'mysecret';\nconst payload = 'Hello, world!';\nconst signature = 'signatureFromSigning';\nconst isValid = hmac.verify(payload, signature, secret);\nconsole.log('Is signature valid?', isValid);

Other packages similar to jwa

Readme

Source

node-jwa Build Status

A JSON Web Algorithms implementation focusing (exclusively, at this point) on the algorithms necessary for JSON Web Signatures.

This library supports all of the required, recommended and optional cryptographic algorithms for JWS:

alg Parameter ValueDigital Signature or MAC Algorithm
HS256HMAC using SHA-256 hash algorithm
HS384HMAC using SHA-384 hash algorithm
HS512HMAC using SHA-512 hash algorithm
RS256RSASSA using SHA-256 hash algorithm
RS384RSASSA using SHA-384 hash algorithm
RS512RSASSA using SHA-512 hash algorithm
PS256RSASSA-PSS using SHA-256 hash algorithm
PS384RSASSA-PSS using SHA-384 hash algorithm
PS512RSASSA-PSS using SHA-512 hash algorithm
ES256ECDSA using P-256 curve and SHA-256 hash algorithm
ES384ECDSA using P-384 curve and SHA-384 hash algorithm
ES512ECDSA using P-521 curve and SHA-512 hash algorithm
noneNo digital signature or MAC value included

Please note that PS* only works on Node 6.12+ (excluding 7.x).

Requirements

In order to run the tests, a recent version of OpenSSL is required. The version that comes with OS X (OpenSSL 0.9.8r 8 Feb 2011) is not recent enough, as it does not fully support ECDSA keys. You'll need to use a version > 1.0.0; I tested with OpenSSL 1.0.1c 10 May 2012.

Testing

To run the tests, do

$ npm test

This will generate a bunch of keypairs to use in testing. If you want to generate new keypairs, do make clean before running npm test again.

Methodology

I spawn openssl dgst -sign to test OpenSSL sign → JS verify and openssl dgst -verify to test JS sign → OpenSSL verify for each of the RSA and ECDSA algorithms.

Usage

jwa(algorithm)

Creates a new jwa object with sign and verify methods for the algorithm. Valid values for algorithm can be found in the table above ('HS256', 'HS384', etc) and are case-sensitive. Passing an invalid algorithm value will throw a TypeError.

jwa#sign(input, secretOrPrivateKey)

Sign some input with either a secret for HMAC algorithms, or a private key for RSA and ECDSA algorithms.

If input is not already a string or buffer, JSON.stringify will be called on it to attempt to coerce it.

For the HMAC algorithm, secretOrPrivateKey should be a string or a buffer. For ECDSA and RSA, the value should be a string representing a PEM encoded private key.

Output base64url formatted. This is for convenience as JWS expects the signature in this format. If your application needs the output in a different format, please open an issue. In the meantime, you can use brianloveswords/base64url to decode the signature.

As of nodejs v0.11.8, SPKAC support was introduce. If your nodeJs version satisfies, then you can pass an object { key: '..', passphrase: '...' }

jwa#verify(input, signature, secretOrPublicKey)

Verify a signature. Returns true or false.

signature should be a base64url encoded string.

For the HMAC algorithm, secretOrPublicKey should be a string or a buffer. For ECDSA and RSA, the value should be a string represented a PEM encoded public key.

Example

HMAC

const jwa = require('jwa');

const hmac = jwa('HS256');
const input = 'super important stuff';
const secret = 'shhhhhh';

const signature = hmac.sign(input, secret);
hmac.verify(input, signature, secret) // === true
hmac.verify(input, signature, 'trickery!') // === false

With keys

const fs = require('fs');
const jwa = require('jwa');
const privateKey = fs.readFileSync(__dirname + '/ecdsa-p521-private.pem');
const publicKey = fs.readFileSync(__dirname + '/ecdsa-p521-public.pem');

const ecdsa = jwa('ES512');
const input = 'very important stuff';

const signature = ecdsa.sign(input, privateKey);
ecdsa.verify(input, signature, publicKey) // === true

License

MIT

Copyright (c) 2013 Brian J. Brennan

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Keywords

FAQs

Last updated on 15 Dec 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc