@crpdo/time
Version:
Provides methods for performing time-based operations including token verification, hash generation, and NTP calculations. It primarily deals with time-based one-time password (TOTP) functions
105 lines (77 loc) • 4.93 kB
Markdown
<a name="module_Time"></a>
## Time
Time utility module providing methods for performing time-based operations
including token verification, hash generation, and NTP calculations.
It primarily deals with time-based one-time password (TOTP) functions.
**Requires**: <code>module:'basd'</code>, <code>module:'@crpdo/crypto'</code>
* [Time](#module_Time)
* [~Time](#module_Time..Time)
* [.code(secret, [opts])](#module_Time..Time.code) ⇒ <code>string</code>
* [.hash(secret, [opts])](#module_Time..Time.hash) ⇒ <code>string</code>
* [.verify(token, secret, [opts])](#module_Time..Time.verify) ⇒ <code>boolean</code>
* [.ntp(t0, t1, t2, t3)](#module_Time..Time.ntp) ⇒ <code>Object</code>
* [.sync([results])](#module_Time..Time.sync) ⇒ <code>Object</code>
<a name="module_Time..Time"></a>
### Time~Time
**Kind**: inner class of [<code>Time</code>](#module_Time)
* [~Time](#module_Time..Time)
* [.code(secret, [opts])](#module_Time..Time.code) ⇒ <code>string</code>
* [.hash(secret, [opts])](#module_Time..Time.hash) ⇒ <code>string</code>
* [.verify(token, secret, [opts])](#module_Time..Time.verify) ⇒ <code>boolean</code>
* [.ntp(t0, t1, t2, t3)](#module_Time..Time.ntp) ⇒ <code>Object</code>
* [.sync([results])](#module_Time..Time.sync) ⇒ <code>Object</code>
<a name="module_Time..Time.code"></a>
#### Time.code(secret, [opts]) ⇒ <code>string</code>
Generates a time-based one-time password (TOTP).
**Kind**: static method of [<code>Time</code>](#module_Time..Time)
**Returns**: <code>string</code> - - The generated OTP.
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| secret | <code>string</code> | | The secret key used to generate the OTP. |
| [opts] | <code>Object</code> | <code>{}</code> | Optional parameters for OTP generation. |
| [opts.totpGap] | <code>number</code> | <code>TOTP_GAP</code> | The time duration for OTP generation (in seconds). |
| [opts.now] | <code>number</code> | | The current timestamp. |
<a name="module_Time..Time.hash"></a>
#### Time.hash(secret, [opts]) ⇒ <code>string</code>
Generates a hashed time-based code.
**Kind**: static method of [<code>Time</code>](#module_Time..Time)
**Returns**: <code>string</code> - - The hashed time-based code.
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| secret | <code>string</code> | | The secret key used to generate the OTP. |
| [opts] | <code>Object</code> | <code>{}</code> | Optional parameters for OTP generation. |
<a name="module_Time..Time.verify"></a>
#### Time.verify(token, secret, [opts]) ⇒ <code>boolean</code>
Verifies a given token.
**Kind**: static method of [<code>Time</code>](#module_Time..Time)
**Returns**: <code>boolean</code> - - `true` if the token is verified successfully, otherwise `false`.
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| token | <code>string</code> | | The token to be verified. |
| secret | <code>string</code> | | The secret key used for verification. |
| [opts] | <code>Object</code> | <code>{}</code> | Optional parameters for OTP verification. |
| [opts.now] | <code>number</code> | | The current timestamp. |
| [opts.totpGap] | <code>number</code> | <code>TOTP_GAP</code> | The time duration for OTP verification (in seconds). |
| [opts.hashed] | <code>boolean</code> | <code>false</code> | Indicates whether the token is hashed or not. |
| [opts.range] | <code>number</code> | <code>1</code> | The range for token verification. |
<a name="module_Time..Time.ntp"></a>
#### Time.ntp(t0, t1, t2, t3) ⇒ <code>Object</code>
Performs Network Time Protocol (NTP) calculations.
**Kind**: static method of [<code>Time</code>](#module_Time..Time)
**Returns**: <code>Object</code> - - An object containing the round trip time (`rtt`),
offset (`offset`), and delay (`delay`).
| Param | Type | Description |
| --- | --- | --- |
| t0 | <code>number</code> | The time at which the request was transmitted. |
| t1 | <code>number</code> | The time at which the request was received. |
| t2 | <code>number</code> | The time at which the response was transmitted. |
| t3 | <code>number</code> | The time at which the response was received. |
<a name="module_Time..Time.sync"></a>
#### Time.sync([results]) ⇒ <code>Object</code>
Calculates the average round trip time (`rtt`), offset (`offset`),
and delay (`delay`) from an array of results.
**Kind**: static method of [<code>Time</code>](#module_Time..Time)
**Returns**: <code>Object</code> - - An object containing the average `rtt`, `offset`, and `delay`.
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [results] | <code>Array.<Object></code> | <code>[]</code> | An array of result objects containing `rtt`, `offset`, and `delay` properties. |