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

cose-w

v0.1.9

Published

COSE WebAssembly

Downloads

37

Readme

npm version

cose-w

WebAssembly COSE RFC 8152 library to encode/decode COSE messages in JS.

See full documentation here.

COSE

COSE is a concise binary data format that protects the payload of the message with a set of cryptographic operations.

The COSE RFC 8152 specifies the following 6 types of COSE messages:

  • cose-sign1: A digitally signed COSE message with a single signer.
  • cose-sign: A digitally signed COSE message with a signers bucket.
  • cose-encrypt0: An encrypted COSE message with a single recipient.
  • cose-encrypt: An encrypted COSE message with a recipients bucket.
  • cose-mac0: A MAC tagged COSE message with a single recipient.
  • cose-encrypt: A MAC tagged COSE message with a recipients bucket.

Examples

The following examples, demonstrate how to encode and decode the basic COSE messages (cose-sign1, cose-encrypt0, cose-mac0), examples of other use cases and cose message types can be found in the respective documentation.

cose-sign1

Encode/decode cose-sign1.

Encode cose-sign1

var {
  CoseKey,
  CoseHeader,
  CoseMessage,
  Alg,
  Kty,
  Crv,
  KeyOp
} = require('cose-w');

// Message to sign, "This is the content."
let msg = Buffer.from('546869732069732074686520636F6E74656E742E', 'hex');

// Prepare cose-key
let key = new CoseKey();
key.kty = Kty.ec2;
key.alg = Alg.es256;
key.crv = Crv.p_256;
key.d = Buffer.from('57c92077664146e876760c9520d054aa93c3afb04e306705db6090308507b4d3', 'hex');
key.key_ops = [KeyOp.sign];

// Prepare cose-sign1 header
let header = new CoseHeader();
header.set_alg(Alg.es256, true, false);
header.set_kid([49, 49], true, false);

// Generate CoseSign and encode the cose-sign1 final message
let signer = CoseMessage.new_sign();
signer.header = header;
signer.payload = msg;
signer.key(key);
signer.secure_content(null);
let bytes = signer.encode(true);

Decode cose-sign1

var {
  CoseKey,
  CoseMessage,
  Alg,
  Kty,
  Crv,
  KeyOp
} = require('cose-w');

// Prepare cose-key
let key = new CoseKey();
key.kty = Kty.ec2;
key.alg = Alg.es256;
key.crv = Crv.p_256;
key.x = Buffer.from('bac5b11cad8f99f9c72b05cf4b9e26d244dc189f745228255a219a86d6a09eff', 'hex');
key.y = Buffer.from('20138bf82dc1b6d562be0fa54ab7804a3a64b6d72ccfed6b6fb6ed28bbfc117e', 'hex');
key.key_ops = [KeyOp.verify];

// Prepare CoseSign with the cose-sign1 bytes
let verify = CoseMessage.new_sign();
verify.bytes = Buffer.from("d28447a2012604423131a054546869732069732074686520636f6e74656e742e58405e84ce5812b0966e6919ff1ac15c030666bae902c0705d1e0a5fbac828437c63b0bb87a95a456835f4d115850adefcf0fd0a5c26027140c10d3e20a890c5eaa7", "hex");

// Init decoding and add key
verify.init_decoder(null);
verify.key(key);

// Verify cose-sign1
let payload = verify.decode(null, null);

cose-encrypt0

Encode/decode cose-encrypt0.

Encode cose-encrypt0

var {
  CoseKey,
  CoseHeader,
  CoseMessage,
  Alg,
  Kty,
  KeyOp
} = require('cose-w');

// Message to encrypt, "This is the content."
let msg = Buffer.from('546869732069732074686520636F6E74656E742E', 'hex');

// Prepare cose-key
let key = new CoseKey();
key.kty = Kty.symmetric;
key.alg = Alg.chacha20;
key.k = Buffer.from('849b57219dae48de646d07dbb533566e976686457c1491be3a76dcea6c427188', 'hex');
key.key_ops = [KeyOp.encrypt];

// Prepare cose-encrypt0 header
let header = new CoseHeader();
header.set_alg(Alg.chacha20, true, false);
header.set_iv(Buffer.from('89f52f65a1c580933b5261a7', 'hex'), true, false);

// Generate CoseEncrypt and encode the cose-encrypt0 final message
let enc = CoseMessage.new_encrypt();
enc.header = header;
enc.payload = msg;
enc.key(key);
enc.secure_content(null);
let bytes = enc.encode(true);

Decode cose-encrypt0

var {
  CoseKey,
  CoseMessage,
  Alg,
  Kty,
  KeyOp
} = require('cose-w');
var assert = require('assert');

// "This is the content."
let expected_msg = Buffer.from('546869732069732074686520636F6E74656E742E', 'hex');

// Prepare cose-key
let key = new CoseKey();
key.kty = Kty.symmetric;
key.alg = Alg.chacha20;
key.k = Buffer.from('849b57219dae48de646d07dbb533566e976686457c1491be3a76dcea6c427188', 'hex');
key.key_ops = [KeyOp.decrypt];

// Prepare CoseEncrypt with the cose-encrypt0 bytes
let dec = CoseMessage.new_encrypt();
dec.bytes = Buffer.from("d08352a2011818054c89f52f65a1c580933b5261a7a0582481c32c048134989007b3b5b932811ea410eeab15bd0de5d5ac5be03c84dce8c88871d6e9", "hex");

// Init decoding and add key
dec.init_decoder(null);
dec.key(key);

// Decrypt and verify cose-encrypt0 message
let msg = dec.decode(null);
assert.deepEqual(new Buffer.from(msg).toString(), expected_msg.toString());

cose-mac0

Encode/decode cose-mac0.

Encode cose-mac0

var {
  CoseKey,
  CoseHeader,
  CoseMessage,
  Alg,
  Kty,
  KeyOp
} = require('cose-w');

// Message to MAC, "This is the content."
let msg = Buffer.from('546869732069732074686520636F6E74656E742E', 'hex');

// Prepare cose-key
let key = new CoseKey();
key.kty = Kty.symmetric;
key.alg = Alg.aes_mac_256_128;
key.k = Buffer.from('849b57219dae48de646d07dbb533566e976686457c1491be3a76dcea6c427188', 'hex');
key.key_ops = [KeyOp.mac];

// Prepare cose-mac0 header
let header = new CoseHeader();
header.set_alg(Alg.aes_mac_256_128, true, false);

// Generate CoseMAC and encode the cose-mac0 final message
let mac = CoseMessage.new_mac();
mac.header = header;
mac.payload = msg;
mac.key(key);
mac.secure_content(null);
let bytes = mac.encode(true);

Decode cose-mac0

var {
  CoseKey,
  CoseMessage,
  Alg,
  Kty,
  KeyOp
} = require('cose-w');

// Prepare cose-key
let key = new CoseKey();
key.kty = Kty.symmetric;
key.alg = Alg.aes_mac_256_128;
key.k = Buffer.from('849b57219dae48de646d07dbb533566e976686457c1491be3a76dcea6c427188', 'hex');
key.key_ops = [KeyOp.mac_verify];

// Prepare CoseMAC with the cose-mac0 bytes
let verify = CoseMessage.new_mac();
verify.bytes = Buffer.from("d18444a101181aa054546869732069732074686520636f6e74656e742e50403152cc208c1d501e1dc2a789ae49e4", "hex");

// Init decoding and add key
verify.init_decoder(null);
verify.key(key);

// Verify cose-mac0 tag
let payload = verify.decode(null);

License

This project, cose-w, is licensed by the MIT License.

Note

This project is under development and it has not been tested yet.