Comparing version 1.1.0 to 1.2.0
@@ -8,4 +8,4 @@ /// <reference types="node" /> | ||
truncationOffset?: number; | ||
hmacAlgorithm?: string; | ||
hmacAlgorithm?: 'sha1' | 'sha256' | 'sha512'; | ||
} | ||
export default function (parameters: Parameters): string; |
@@ -47,4 +47,13 @@ "use strict"; | ||
hmacAlgorithm = 'sha1'; | ||
let secretLength; | ||
if (hmacAlgorithm === 'sha1') | ||
secretLength = 20; | ||
else if (hmacAlgorithm === 'sha256') | ||
secretLength = 32; | ||
else if (hmacAlgorithm === 'sha512') | ||
secretLength = 64; | ||
else | ||
throw new Error('algorithm not supported'); | ||
const digits = addChecksum ? codeDigits + 1 : codeDigits; | ||
const text = new Buffer(8); | ||
const text = Buffer.alloc(8); | ||
for (let i = text.length - 1; i >= 0; i--) { | ||
@@ -54,3 +63,3 @@ text[i] = movingFactor & 0xff; | ||
} | ||
const hash = crypto.createHmac(hmacAlgorithm, secret).update(text).digest(); | ||
const hash = crypto.createHmac(hmacAlgorithm.toLowerCase(), Buffer.alloc(secretLength, secret)).update(text).digest(); | ||
let offset = hash[hash.length - 1] & 0xf; | ||
@@ -57,0 +66,0 @@ if (0 <= truncationOffset && truncationOffset < hash.length - 4) |
/// <reference types="node" /> | ||
export interface Parameters { | ||
secret: string | Buffer; | ||
step?: number; | ||
time?: number; | ||
timestamp?: number; | ||
initialTime?: number; | ||
codeDigits?: number; | ||
addChecksum?: boolean; | ||
truncationOffset?: number; | ||
hmacAlgorithm?: string; | ||
hmacAlgorithm?: 'sha1' | 'sha256' | 'sha512'; | ||
} | ||
export default function (parameters: Parameters): string; |
@@ -6,10 +6,14 @@ "use strict"; | ||
function default_1(parameters) { | ||
let { secret, time, timestamp, codeDigits, addChecksum, truncationOffset, hmacAlgorithm } = parameters; | ||
let { secret, step, time, initialTime, codeDigits, hmacAlgorithm } = parameters; | ||
if (!secret) | ||
throw new Error('no secret value'); | ||
if (!step) | ||
step = 30; | ||
if (!time) | ||
time = 30; | ||
if (!timestamp) | ||
timestamp = new Date().getTime(); | ||
const movingFactor = Math.floor(timestamp / 1000 / time); | ||
time = new Date().getTime() / 1000; | ||
if (!initialTime) | ||
initialTime = 0; | ||
if (!hmacAlgorithm) | ||
hmacAlgorithm = 'sha512'; | ||
const movingFactor = Math.floor((time - initialTime) / step); | ||
return hotp_1.default({ | ||
@@ -19,4 +23,2 @@ secret, | ||
codeDigits, | ||
addChecksum, | ||
truncationOffset, | ||
hmacAlgorithm | ||
@@ -23,0 +25,0 @@ }); |
{ | ||
"name": "node-otp", | ||
"description": "Node.js One-Time Password", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "main": "./lib/index.js", |
@@ -10,3 +10,3 @@ # Node OTP | ||
### Features | ||
## Features | ||
- Zero Dependency | ||
@@ -17,3 +17,16 @@ - TypeScript Definitions | ||
### Examples | ||
--- | ||
## Installation | ||
```bash | ||
yarn add final-form | ||
``` | ||
or | ||
```bash | ||
npm install --save final-form | ||
``` | ||
## Examples | ||
```javascript | ||
@@ -27,5 +40,37 @@ otp.hotp({ | ||
otp.totp({ | ||
secret: '12345678901234567890', | ||
timestamp: new Date().getTime() | ||
secret: '12345678901234567890' | ||
}) | ||
``` | ||
``` | ||
## APIs | ||
### `hotp: (parameters: Parameters) => string` | ||
### `Parameters` | ||
#### `secret: string | Buffer` | ||
#### `movingFactor?: number` | ||
Default value of `movingFactor` is 0 | ||
#### `codeDigits?: number` | ||
Default value of `codeDigits` is 6 | ||
#### `addChecksum?: boolean` | ||
Default value of `addChecksum` is false | ||
#### `truncationOffset?: number` | ||
Default value of `truncationOffset` is -1 | ||
#### `hmacAlgorithm?: 'sha1' | 'sha256' | 'sha512'` | ||
Default value of `hmacAlgorithm` is sha1 | ||
--- | ||
### `totp: (parameters: Parameters) => string` | ||
### `Parameters` | ||
#### `secret: string | Buffer` | ||
#### `step?: number` | ||
Default value of `step` is 30 | ||
#### `time?: number` | ||
Default value of `time` is 6 | ||
#### `initialTime?: number` | ||
Default value of `initialTime` is 0 | ||
#### `codeDigits?: number` | ||
Default value of `codeDigits` is 6 | ||
#### `hmacAlgorithm?: 'sha1' | 'sha256' | 'sha512'` | ||
Default value of `hmacAlgorithm` is sha256 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
73
7516
10
132