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 🙏

© 2021 – Pkg Stats / Ryan Hefner

@exodus/bintoken

v0.1.1

Published

Binary encoding of named fixed-length buffers.

Downloads

26

Readme

Bintoken

A bintoken is an encoded binary message of a fixed length, meant to serve as a message which can be signed. Depending on the type byte, and who signed the bintoken, the message may hold different meanings.

For example, if the type indicates the message is an authentication token, and the bintoken is signed by a server's private key, this might provide a guarantee that the server verified some information at the time indicated in the bintoken.

The encoding format of the token is specified in-advance by instantiating the BintokenEncoding class exported by this package. The encoding is specified with property names and lengths for each property. The key names are sorted alphabetically when encoding and decoding bintokens.

The deterministic binary format simplifies signature verification. Its fixed length simplifies decoding & validation, and thus reduces the chance for malicious exploitation. Unlike with other formats such as JSON, there is only one way to encode a message within a bintoken encoding scheme.

Here is the generic serialization format of any bintoken encoding:

  • type (1 byte) : a version number which can be used to identify the context of the message.
  • [...payload properties]
  • time (4 bytes) : the timestamp (in unix seconds) when the message was created, encoded as a big-endian uint32.

Example

Here is an example of a bintoken encoding which encodes only a 32-byte public key as the payload:

const BintokenEncoding = require('@exodus/bintoken')

const tokenEncoding = new BintokenEncoding({ publicKey: 32, hash: 20 })

const token = tokenEncoding.toBuffer({
  type: 1,
  publicKey: randomBytes(32),
  hash: randomBytes(20),
})

/*
  // Erroneous use
  const token = tokenEncoding.toBuffer({ type: 1, publicKey: randomBytes(32), hash: randomBytes(20), otherProp: Buffer.alloc(0) })
  const token = tokenEncoding.toBuffer({ type: 1, publicKey: randomBytes(31), hash: randomBytes(20) })
  const token = tokenEncoding.toBuffer({ type: 1, publicKey: 'foo', hash: randomBytes(20) })
  const token = tokenEncoding.toBuffer({ type: 1 })
*/

const { type, time, publicKey, hash } = tokenEncoding.fromBuffer(token)

/*
  // Error, wrong length
  const { type, time, publicKey } = tokenEncoding.fromBuffer(
    Buffer.concat([token, Buffer.alloc(1)])
  )
*/

In this example, the serialization format is:

  • type (1 byte) : a version number which can be used to identify the context of the message.
  • hash (20) bytes : a hash over which the message holds meaning (as implied by the type).
  • public key (32 bytes) : the public key over which the message holds meaning (as implied by the type).
  • time (4 bytes) : the timestamp (in seconds) when the message was created, encoded as a big-endian uint32.

Notice that hash is written before publicKey in the payload, as the properties are sorted alphabetically before being written to, or read from, a bintoken. This means the keys you choose for your message must be the same for different applications using the same bintokens.