Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add a new method to get the previous digit #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 52 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ The module was implement based on [RFC4226](https://tools.ietf.org/html/rfc4226)
```shell
npm install jsotp
```

### Module

All modules support:

```javascript
const jsotp = require('jsotp');
```

### Usage

#### Time-based OTPs
Expand All @@ -42,6 +42,7 @@ const jsotp = require('jsotp');
// Create TOTP object
const totp = jsotp.TOTP('BASE32ENCODEDSECRET');
totp.now(); // => 432143
totp.prev(); // => 666666

// Verify for current time
totp.verify(432143); // => true
Expand Down Expand Up @@ -81,67 +82,72 @@ const b32_secret = jsotp.Base32.random_gen();

#### • [jsotp.Base32.random_gen(length)](https://github.com/LanceGin/jsotp/blob/master/src/base32.js#L32)

param: length
type: int
default: 16
return: String
desc: the length of random base32 encoded string.
param: length
type: int
default: 16
return: String
desc: the length of random base32 encoded string.

#### • [jsotp.TOTP(secret)](https://github.com/LanceGin/jsotp/blob/master/src/jsotp.js#L48)
param: secret
type: string
return: TOTP
desc: generate TOTP instance.

param: secret
type: string
return: TOTP
desc: generate TOTP instance.

#### • [jsotp.TOTP.now()](https://github.com/LanceGin/jsotp/blob/master/src/totp.js#L38)

return: String
desc: get the one-time password with current time.

return: String
desc: get the one-time password with previous time.

#### • [jsotp.TOTP.prev()](https://github.com/LanceGin/jsotp/blob/master/src/totp.js#L46)

return: String
desc: get the one-time password with current time.

#### • [jsotp.TOTP.verify(totp)](https://github.com/LanceGin/jsotp/blob/master/src/totp.js#L70)

param: totp
type: string
return: Boolean
desc: verify the totp code.
param: totp
type: string
return: Boolean
desc: verify the totp code.

#### • [jsotp.TOTP.url_gen(issuer)](https://github.com/LanceGin/jsotp/blob/master/src/totp.js#L94)

param: issuer
type: string
return: string
desc: generate url with TOTP instance
param: issuer
type: string
return: string
desc: generate url with TOTP instance

#### • [jsotp.HOTP(secret)](https://github.com/LanceGin/jsotp/blob/master/src/jsotp.js#L47)
param: secret
type: string
return: HOTP
desc: generate HOTP instance.

param: secret
type: string
return: HOTP
desc: generate HOTP instance.

#### • [jsotp.HOTP.at(counter)](https://github.com/LanceGin/jsotp/blob/master/src/hotp.js#L24)

param: counter
type: int
return: String
desc: generate one-time password with counter.
param: counter
type: int
return: String
desc: generate one-time password with counter.

#### • [jsotp.HOTP.verify(hotp, count)](https://github.com/LanceGin/jsotp/blob/master/src/hotp.js#L50)
param: hotp
type: string
param: count
type: int
return: Boolean
desc: verify the hotp code.

param: hotp
type: string
param: count
type: int
return: Boolean
desc: verify the hotp code.

#### • [jsotp.HOTP.url_gen(issuer)](https://github.com/LanceGin/jsotp/blob/master/src/hotp.js#L69)

param: issuer
type: string
return: string
desc: generate url with HOTP instance
param: issuer
type: string
return: string
desc: generate url with HOTP instance

### Contribute

Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsotp",
"version": "1.0.3",
"name": "@taichi-yi/jsotp",
"version": "1.1.1",
"description": "Javascript One-Time Password module.",
"main": "lib/jsotp.js",
"directories": {
Expand All @@ -14,7 +14,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/LanceGin/jsotp.git"
"url": "git+https://github.com/taichiyi/jsotp.git"
},
"keywords": [
"otp",
Expand All @@ -26,9 +26,9 @@
"author": "lancegin",
"license": "MIT",
"bugs": {
"url": "https://github.com/LanceGin/jsotp/issues"
"url": "https://github.com/taichiyi/jsotp/issues"
},
"homepage": "https://github.com/LanceGin/jsotp#readme",
"homepage": "https://github.com/taichiyi/jsotp#readme",
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-core": "^6.25.0",
Expand Down
5 changes: 5 additions & 0 deletions src/totp.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export class TOTP extends OTP {
const digit = super.generate_otp(now);
return digit;
}
prev() {
return this.generate_otp(
(Date.now() / 1000 - this.interval | 0) / this.interval
)
}

/* *
* Verifies the OTP passed in against the current time OTP.
Expand Down