Git Product home page Git Product logo

subtle-key-codec's Introduction

subtle-key-codec

Encodes/Decodes WebCrypto keys efficiently and easily into Uint8Arrays. Good when you want to safe space (QR Codes).

import { codec } from 'subtle-key-codec'

const key = // ... generated via WebCrypto API

const uint8Array = await codec.encode(key)
const key = await codec.decode(bytes)

Why use it?

  • To get a simple API that just works.
  • To have smaller QR codes.
  • It integrate with other Binary data libraries.

(more in the Comparison)

What is in the package?

  • An encoder/decoder to turn WebCrypto keys into Uint8Arrays.
  • ESM modules, CommonJS modules and typescript definitions.

Comparison

The WebCrypto API allows to import/export keys. But the API is rather difficult to use. Not every key supports every input/output format and getting a space-efficient representation of codec is not available.

As you can see above the API is simple. ๐Ÿ˜‰

It is possible to relativey turn any key into a Uint8Array with the native API like this:

const bytes = Buffer.from(
  JSON.stringify(
    await crypto.subtle.exportKey('jwk', key)
  )
).toString('base64')

However, the size of this is rather big. In the tests this library creates base64 strings that are 7.7% to 75% of the size!

This can make a big difference when you want to turn keys into QR Codes.

API

Beside the basic API, there are are few additional features that may help you to be faster or consume less memory.

BYOA and pre-determined length

By default the codec will create a new Uint8Array for you consume. But you can also pass-in a pre-created Uint8Array to the encoding process.

await codec.encode(key, new Uint8Array(1000))

This can be useful if you just want to add a key to a set of keys. But you may want to pass-in also the offset place it entirely in an array.

await codec.encode(key, new Uint8Array(1000), 2)

The example has an abritary size of 1000 but you can also figure out in advance how big the key is.

await codec.encodingLength(key)

Specific Codec

.encode will usually prefix the resulting Uint8Array with a type identifier. A value from 0~255 that specifies which particular key-codec is used to de-/encode a particular key.

 // this will tell you the constant number of that codec
const uint8 = codec.getKeyType(key)
// You can also get the entire codec
const keyCodec = codec.getKeyCodec(key)
// ... to use wthout the prefix.
const withoutIndexPrefix = keyCodec.encode(key)
// ... in both directions
key = keyCodec.decode(withoutIndexPrefix)

Use with older Node versions

This library has been tested with the [isomorphic-webcrypto][] library that works with older versions of Node.js. To use it you need to create a crypto object rather than using the default crypto object.

import { createCodec } from 'subtle-key-codec'
import crypto from 'isomorphic-webcrypto'

const codec = createCodec(crypto)

License

MIT

subtle-key-codec's People

Contributors

martinheidegger avatar

Stargazers

 avatar

Watchers

 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.