https://github.com/commenthol/signed-token
signed HMAC crypto token
https://github.com/commenthol/signed-token
Last synced: 16 days ago
JSON representation
signed HMAC crypto token
- Host: GitHub
- URL: https://github.com/commenthol/signed-token
- Owner: commenthol
- License: unlicense
- Created: 2017-06-26T19:02:08.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2022-11-12T11:12:03.000Z (over 3 years ago)
- Last Synced: 2026-01-01T06:31:00.352Z (6 months ago)
- Language: JavaScript
- Size: 30.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# signed-token
> signed HMAC crypto token
[](https://www.npmjs.com/package/signed-token/)
[](https://github.com/commenthol/signed-token/actions/workflows/ci.yml?query=branch%3Amaster)
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