# simpleotp

> OTP implementation according to rfc 4996 and rfc 6238

Latest version **1.0.0** (published 2020-03-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install simpleotp
pnpm add simpleotp
yarn add simpleotp
bun add simpleotp
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2020-03-19 |
| First published | 2017-12-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >6.9.4 |
| Dependencies | 0 |
| Unpacked size | 15.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Xavier Renard |
| Maintainers | xavierfoxy |
| Keywords | otp, hotp, totp, hmac, rfc4996, rfc_6238 |

## Links

- npm: https://www.npmjs.com/package/simpleotp
- Repository: https://github.com/xarenard/simpleotp
- Homepage: https://github.com/xarenard/simpleotp#readme
- Issues: https://github.com/xarenard/simpleotp/issues
- npm.io page: https://npm.io/package/simpleotp

## Recent versions

- 1.0.0 (latest) — 2020-03-19
- 0.7.7 — 2020-03-15
- 0.7.6 — 2017-12-19
- 0.7.5 — 2017-12-15
- 0.7.4 — 2017-12-14
- 0.7.3 — 2017-12-14
- 0.7.1 — 2017-12-14

## README

# Simple OTP
Time-based and HMAC-based One-Time Password libraryfor node.js

- [About](#about)
- [Installation](#installation)
- [Usage](#usage)
    - [HOTP](#hotp)
        - [Example](#hotp_example)
        - [Configuration](#hotp_example)
    - [TOTP](#totp)
        - [Example](#totp_example)
        - [Configuration](#totp_example)
- [Test](#test)
- [References](#references)
- [License](#license)
<a name="about"></a>
## About

simpleotp is a simple :-) OTP library for node.js.

It provides an implementation of both rfc 4226 (HOTP) and rfc 6238 (TOTP).
<a name="installation"></a>
## Installation
```
npm install --save simpleotp
```
<a name="usage"></a>
## Usage
<a name="hotp"></a>
### HOTP

<a name="hotp_example"></a>

#### Example
```js
const otp = require('simpleotp');
const hotp = new otp.Hotp();

// generate a token
const token = hotp.createToken({secret:'12345678901234567890',counter:7});

// validate the token
const data = {token: token, secret:'12345678901234567890',counter: 7};
const valid = hotp.validate(data); //true
```
<a name="totp_configuration"></a>
#### Configuration

##### Constructor options


| Option    |  Value              | Description           |Default Value    |
| ------------ | ---------------------- | ----------------------- | ----------- 
|algorithm   | sha1,sha256,sha512   |  Algorithm to use     | sha1      |
|num_digits  | integer              | token length          |6          |
|encoding    | ascii                | Encoding of the secret| ascii     |


##### Create Token options

| Option    | Mandatory | Value                   | Description                            | Default value         |
|------------|-----------|---------------------------|----------------------------------------|-----------------|
|secret      | y         |string type                | Share secret to use                    | N/A             |
|counter     | y         |integer type               | The counter seed                       | N/A             |
|algorithm   | n         |'sha1','sha256' or 'sha512'| Algorithm to use                       | sha1            |
|num_digits  | n         |integer type               | token length                           | 6               |
|encoding    | n         |'ascii'                    | Encoding of the secret                 | ascii           |

##### Validate token options

| Option     | Mandatory | Value                     | Description                            | Default value          |
|------------|-----------|---------------------------|----------------------------------------|-----------------|
|token       | y         |string type                | The original token                     | N/A             |
|secret      | y         |string type                | Share secret to use                    | N/A             |
|counter     | y         |integer type               | The counter seed                       | N/A             |
|algorithm   | n         |'sha1','sha256' or 'sha512'| Algorithm to use                       | sha1            |
|num_digits  | n         |integer type               | token length                           | 6               |
|encoding    | n         |'ascii'                    | Encoding of the secret                 | ascii           |
       

### TOTP
<a name="totp_example"></a>
#### Example
```js
const otp = require('simpleotp');
const totp = new otp.Totp();

// generate the token
const token = totp.createToken({secret:'12345678901234567890',seconds :Date.now()/1000});

// validate the token
const data = {token: token, secret:'12345678901234567890',seconds :Date.now()/1000}
const valid = totp.validate(data)
console.log(valid); // true
```
<a name="totp_example"></a>
#### Configuration

##### Constructor options
| Option     |  Value              | Description                             |Default value          |
|------------|----------------------|-----------------------------------------|-----------------|
|algorithm   | 'sha1','sha256' or 'sha512'   | Algorithm to use                        |sha1            |
|num_digits  | integer              | token length                            |6                |
|encoding    | ascii                | Encoding of the secret                  |ascii           |
|step        | integer              | Number of the second the token is valid |30               |

##### Create Token options
| Option    | Mandatory | Value                    | Description                            | Default value         |
|------------|-----------|---------------------------|----------------------------------------|-----------------|
|secret      | y         | string type                | Share secret to use                    | N/A             |
|seconds     | y         | integer                   | time in seconds as counter              | Date.now()/1000 |
|step        | n         | integer                    | Number of the second the token is valid| 30              |
|algorithm   | n         | 'sha1','sha256' or 'sha512'| Algorithm to use                       | sha1            |
|num_digits  | n         | integer type               | token length                           | 6               |
|encoding    | n         | 'ascii'                    | Encoding of the secret                 | ascii           |

##### Validate token options
| Option     | Mandatory | Value                     | Description                            |Default value          |
|------------|-----------|---------------------------|----------------------------------------|-----------------|
|token       | y         | string type                | The original token                     | N/A             |
|secret      | y         | string type                | Share secret to use                    | N/A             |
|seconds     | Y         | integer                   | time in seconds as counter             | Date.now()/1000 |
|step        | n         | integer                    | Number of the second the token is valid| 30              |
|algorithm   | n         | 'sha1','sha256' or 'sha512'| Algorithm to use                       | sha1            |
|num_digits  | n         | integer type               | token length                           | 8               |
|encoding    | n         | 'ascii'                    | Encoding of the secret                 | ascii           |

<a name="tests"></a>
## Tests
```
npm test
```
<a name="references"></a>
## References
<a name="license"></a>
## License
`simpleotp` is [MIT licensed](./LICENSE)

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