# cose-js

> JavaScript COSE implementation

Latest version **0.9.0** (published 2023-09-22) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install cose-js
pnpm add cose-js
yarn add cose-js
bun add cose-js
```

## Health

**Score 33/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.9.0 |
| Published | 2023-09-22 |
| First published | 2016-07-28 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | separate (@types/cose-js) |
| Module format | CommonJS |
| Node | >=12.0 |
| Dependencies | 6 |
| Unpacked size | 827.4 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 29 |
| Author | Samuel Erdtman |
| Maintainers | samuelerdtman, samuel.erdtman |
| Keywords | COSE, Signing, MAC, Encrypt, IoT |

## Links

- npm: https://www.npmjs.com/package/cose-js
- Repository: https://github.com/erdtman/COSE-JS
- Homepage: https://github.com/erdtman/cose-js#readme
- Issues: https://github.com/erdtman/cose-js/issues
- npm.io page: https://npm.io/package/cose-js

## Dependencies (6)

- [cbor](https://npm.io/package/cbor.md) ^8.1.0
- [elliptic](https://npm.io/package/elliptic.md) ^6.4.0
- [node-rsa](https://npm.io/package/node-rsa.md) ^1.1.1
- [aes-cbc-mac](https://npm.io/package/aes-cbc-mac.md) ^1.0.1
- [any-promise](https://npm.io/package/any-promise.md) ^1.3.0
- [node-hkdf-sync](https://npm.io/package/node-hkdf-sync.md) ^1.0.0

## Alternatives

- [@gemini-wallet/core](https://npm.io/package/@gemini-wallet/core.md) — 515.6K weekly downloads
- [utility](https://npm.io/package/utility.md) — 416.6K weekly downloads
- [@primno/dpapi](https://npm.io/package/@primno/dpapi.md) — 7.2K weekly downloads
- [pi-readseek](https://npm.io/package/pi-readseek.md) — 3.7K weekly downloads
- [@emilia-protocol/verify](https://npm.io/package/@emilia-protocol/verify.md) — 1.1K weekly downloads

## Recent versions

- 0.9.0 (latest) — 2023-09-22
- 0.8.4 — 2022-02-12
- 0.8.3 — 2021-12-19
- 0.8.2 — 2021-11-07
- 0.8.0 — 2021-11-04
- 0.7.0 — 2021-09-27
- 0.6.0 — 2021-05-26
- 0.5.0 — 2021-05-19
- 0.4.2 — 2020-08-12
- 0.4.0 — 2019-09-15
- 0.3.1 — 2018-04-06
- 0.3.0 — 2017-12-08
- 0.2.3 — 2017-10-17
- 0.2.2 — 2017-10-17
- 0.2.1 — 2017-10-14
- … 5 more at https://npm.io/package/cose-js/versions

## README

[![Build Status](https://app.travis-ci.com/erdtman/cose-js.svg?branch=master)](https://app.travis-ci.com/erdtman/cose-js)
[![Coverage Status](https://coveralls.io/repos/github/erdtman/cose-js/badge.svg?branch=master)](https://coveralls.io/github/erdtman/cose-js?branch=master)
# cose-js
JavaScript implementation of [COSE](https://tools.ietf.org/html/rfc8152), [RFC8152](https://tools.ietf.org/html/rfc8152)
## MAC
```js
const cose = require('cose-js');
try {
  const plaintext = 'Important message!';
  const headers = {
    p: { alg: 'SHA-256_64' },
    u: { kid: 'our-secret' }
  };
  const recipent = {
    key: Buffer.from('231f4c4d4d3051fdc2ec0a3851d5b383', 'hex')
  };
  const buf = await cose.mac.create(headers, plaintext, recipent);
  console.log('MACed message: ' + buf.toString('hex'));
} catch (error) {
  console.log(error);
}
```
## Verify MAC
```js
const cose = require('cose-js');
try {
  const key = Buffer.from('231f4c4d4d3051fdc2ec0a3851d5b383', 'hex');
  const COSEMessage = Buffer.from('d18443a10104a1044a6f75722d73656372657472496d706f7274616e74206d65737361676521488894981d4aa5d614', 'hex');
  const buf = await cose.mac.read(COSEMessage, key);
  console.log('Verified message: ' + buf.toString('utf8'));
} catch (error) {
  console.log(error);
}
```
## Sign
```js
const cose = require('cose-js');
try {
  const plaintext = 'Important message!';
  const headers = {
    p: { alg: 'ES256' },
    u: { kid: '11' }
  };
  const signer = {
    key: {
      d: Buffer.from('6c1382765aec5358f117733d281c1c7bdc39884d04a45a1e6c67c858bc206c19', 'hex')
    }
  };
  const buf = await cose.sign.create(headers, plaintext, signer);
  console.log('Signed message: ' + buf.toString('hex'));
} catch (error) {
  console.log(error);
}
```
## Verify Signature
```js
const cose = require('cose-js');
try {
  const verifier = {
    key: {
      x: Buffer.from('143329cce7868e416927599cf65a34f3ce2ffda55a7eca69ed8919a394d42f0f', 'hex'),
      y: Buffer.from('60f7f1a780d8a783bfb7a2dd6b2796e8128dbbcef9d3d168db9529971a36e7b9', 'hex')
    }
  };
  const COSEMessage = Buffer.from('d28443a10126a10442313172496d706f7274616e74206d6573736167652158404c2b6b66dfedc4cfef0f221cf7ac7f95087a4c4245fef0063a0fd4014b670f642d31e26d38345bb4efcdc7ded3083ab4fe71b62a23f766d83785f044b20534f9', 'hex');
  const buf = await cose.sign.verify(COSEMessage, verifier);
  console.log('Verified message: ' + buf.toString('utf8'));
} catch (error) {
  console.log(error);
}
```
## Encrypt
```js
const cose = require('cose-js');
try {
  const plaintext = 'Secret message!';
  const headers = {
    p: { alg: 'A128GCM' },
    u: { kid: 'our-secret' }
  };
  const recipient = {
    key: Buffer.from('231f4c4d4d3051fdc2ec0a3851d5b383', 'hex')
  };
  const buf = await cose.encrypt.create(headers, plaintext, recipient);
  console.log('Encrypted message: ' + buf.toString('hex'));
} catch (error) {
  console.log(error);
}
```
## Decrypt
```js
const cose = require('cose-js');
try {
  const key = Buffer.from('231f4c4d4d3051fdc2ec0a3851d5b383', 'hex');
  const COSEMessage = Buffer.from('d8608443a10101a2044a6f75722d736563726574054c291a40271067ff57b1623c30581f23b663aaf9dfb91c5a39a175118ad7d72d416385b1b610e28b3b3fd824a397818340a040', 'hex');
  const buf = await cose.encrypt.read(COSEMessage, key);
  console.log('Protected message: ' + buf.toString('utf8'));
} catch (error) {
  console.log(error);
}
```
## Install
```
npm install cose-js --save
```
## Test
```
npm test
```

---
_Source: https://npm.io/package/cose-js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
