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

p256-auth

v1.1.4

Published

An HMAC-based authentication library.

Downloads

7

Readme

Welcome to P256-Auth!

P256-Auth is a simple library for handling ECDH keys and using them to produce HMAC secrets in the browser. Alternatively, the library can go all the way and compute the HMAC of a message using an internally derived secret. All you have to do is feed it your server's public key and tell it to generate or import a key pair. All operations are done strictly within the bounds of SubtleCrypto so that unwrapped private keys never touch the javascript.

Getting Started

The P256-Auth API is very straightforward. Just install it using npm install p256-auth, then you can begin. Keep in mind the library only works with two key formats: base64URL-encoded 65-byte public keys, and it's own proprietary external key format (JSON), which is used for exporting and importing key pairs between Authenticator instances--and they can be safely saved to file and used later on. Due to the asynchronous nature of SubtleCrypto, all API calls return promises except top-level functions like createAuthenticator(). See the wiki for full API documentation.

General Usage

var p256Auth = require('p256-auth');
var serverKey = getKeyFromYourServer(); // server-side public ECDH key

var authenticator = p256Auth.createAuthenticator();

authenticator.generateKeyPair().then(() => {
    Promise.all([authenticator.importServerKey(serverKey), authenticator.getPublic()]).then(([undefined, clientKey]) => {
        sendPublicKeyToServer(clientKey);

        var message = produceAMessageForYourServer(); // must be Uint8Array or string
        authenticator.computeHMAC(message).then(hmacSecret => {
            sendMessageToYourServer(hmacSecret, message);
        }); // fulfills with websafe-base64 string
    });
});

Exporting and importing keys

When you export keys, the private undergoes a wrapping process before being exported from the native SubtleCrypto API, and a Uint8Array password must be provided for wrapping. The reason the password is passed as a Uint8Array and not a string is because strings are heavily abstracted in Javascript, but the library needs to wipe the password from memory immediately following use to maximize security, which can only really be achieved with an ArrayBuffer or child type.

// Continued from above...
authenticator.exportKey(somePasswordToEncryptWith).then(extKey => { // password must be Uint8Array
    var authenticator2 = p256Auth.createAuthenticator();

    Promise.all([authenticator2.importKey(externalKeyPair, samePasswordNewInstance), authenticator2.importServerKey(serverKey)]).then(() => {
        authenticator2.computeHMAC(message).then(sameSecret => {
            sendMessageToYourServer(sameSecret, message);
        });
    });
});

Signing messages

While P256-Auth was primarily designed for HMAC usage, it also contains signing functionality.

var signer = p256Auth.createSigner();

signer.generateKeys().then(() => {
    signer.sign(someUint8Array).then(base64URLEncodedSignature => {
        // use the signature
    });
    signer.sign(someString, 'utf-8').then(anotherSignature => {
        // ...
    });
});

The Signer portion of the library also has import/export functionality, but its external keys must not be mixed with Authenticator keys. The reason for this is that SubtleCrypto is very strict about not using ECDH keys with ECDSA functions, and so forth, and wrapped keys know there algorithm internally--meaning if you try to import an ECDH key for signing you'll get a weird error.

// Continued from above...
signer.exportKey(somePasswordWhichMustBeUint8Array).then(extKey => {
    signer.importKey(extKey, samePasswordNewInstance).then(() => {
        // sign some stuff
    });
});

Use with Typescript

P256-Auth is packaged with types alongside the Javascript, so NPM and Typescript do the heavy lifting for you. Simply use import in place of var when requiring the package, and types will be linked automatically.

import p256Auth = require('p256-auth');

Testing

Extensive tests are provided to make sure P256-Auth runs properly on your platform. Because the library is security-oriented, your platform must implement SubtleCrypto. At the time of development, only Firefox and platforms built on the V8 engine (e.g. Chrome and Opera) implement enough of SubtleCrypto to run the library. To run the tests, simply use the NPM scripts included:

  1. Start the test server: npm start
  2. Run the Karma tests: npm test (in another terminal)