# signed-token

> signed crypto token

Latest version **2.0.0** (published 2022-11-12) · Unlicense license · 0 weekly downloads

## Install

```sh
npm install signed-token
pnpm add signed-token
yarn add signed-token
bun add signed-token
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2022-11-12 |
| First published | 2017-06-26 |
| Weekly downloads | 0 |
| License | Unlicense |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 12.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | commenthol |
| Maintainers | commenthol |
| Keywords | crypto, signed, token |

## Links

- npm: https://www.npmjs.com/package/signed-token
- Repository: https://github.com/commenthol/signed-token
- Homepage: https://github.com/commenthol/signed-token#readme
- Issues: https://github.com/commenthol/signed-token/issues
- npm.io page: https://npm.io/package/signed-token

## Dependencies (2)

- [url-safe-base64](https://npm.io/package/url-safe-base64.md) ^1.2.0
- [compare-timing-safe](https://npm.io/package/compare-timing-safe.md) ^2.1.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

- 2.0.0 (latest) — 2022-11-12
- 1.1.2 — 2017-07-16
- 1.1.1 — 2017-07-02
- 1.1.0 — 2017-06-26
- 1.0.0 — 2017-06-26

## README

# signed-token

> signed HMAC crypto token

[![NPM version](https://badge.fury.io/js/signed-token.svg)](https://www.npmjs.com/package/signed-token/)

Generation and validation of signed HMAC token with variable lengths. 
The token is [url-safe Base64](https://www.npmjs.com/package/url-safe-base64) encoded and can be used in urls as well as forms.

## TOC

* [Example](#example)
* [API](#api)
  * [`signedToken(secret, [opts])`](#signedtokensecret-opts)
  * [`create`](#create)
  * [`createSync`](#createsync)
  * [`verify`](#verify)
  * [`verifySync`](#verifysync)
* [Installation](#installation)
* [Tests](#tests)
* [LICENSE](#license)

## Example

Asynchronous using Promises

```js
const signedToken = require('signed-token')

const stfn = signedToken('my secret')
stfn.create()
.then((token) => stfn.verify(token))
.then((res) => console.log(res)) // res === token
```

Synchronous

```js
const signedToken = require('signed-token')

const stfn = signedToken('my secret')
const token = stfn.createSync()
const res = stfn.verifySync(token)
res === token
//> true
```

## API

### `signedToken(secret, [opts])`

creates a signedToken instance, wrapping `secret`

**Parameters**

| parameter              | type   | description                                          |
| ---------------------- | ------ | ---------------------------------------------------- |
| `secret`               | string | common secret                                        |
| `[opts]`               | object | _optional:_                                          |
| `[opts.digest=sha265]` | string | _optional:_ hmac digest                              |
| `[opts.commonlen=24]`  | number | _optional:_ length of random bytes for common length |
| `[opts.tokenlen=64]`   | number | _optional:_ length of token                          |

**Returns** `object`, `{create, verify, createSync, verifySync, hmac}`

### `create`

creates a signed token

**Returns** `Promise`, `{string}` signed token url safe base64 encoded

### `createSync`

sync generation of a signed token

**Returns** `string` signed token url safe base64 encoded

### `verify`

verify a signed token using secret

**Returns** `Promise`, `{string|undefined}` - token if it was correctly signed

### `verifySync`

sync validation of signed token

**Returns** `string,undefined`,
token if it was correctly signed

## Installation

Requires [nodejs](http://nodejs.org/) >= v12.0.0

```sh
$ npm install --save signed-token
```

## Tests

```sh
$ npm test
```

## LICENSE

UNLICENSE <https://unlicense.org>

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