Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import CryptoJS from 'crypto-js'; | ||
|
||
/** | ||
* Crypto replacement using crypto-js | ||
* | ||
* @module otplib-cryptojs/crypto | ||
*/ | ||
class CryptoExpo { | ||
constructor(algorithm, secret) { | ||
this.algorithm = algorithm.toLowerCase(); | ||
this.secret = secret.toString('ascii'); | ||
} | ||
|
||
static createHmac(algorithm, hmacSecret) { | ||
return new CryptoExpo(algorithm, hmacSecret); | ||
} | ||
|
||
update(bufferHex) { | ||
this.hex = bufferHex.toString('hex'); | ||
return this; | ||
} | ||
|
||
getEncoder() { | ||
switch (this.algorithm) { | ||
case 'sha1': | ||
return CryptoJS.HmacSHA1; | ||
case 'sha256': | ||
return CryptoJS.HmacSHA256; | ||
case 'sha512': | ||
return CryptoJS.HmacSHA512; | ||
default: | ||
throw new Error( | ||
`Unsupported algorithm ${ | ||
this.algorithm | ||
}. Accepts: sha1, sha256, sha512` | ||
); | ||
} | ||
} | ||
|
||
digest() { | ||
const encoder = this.getEncoder(); | ||
const message = CryptoJS.enc.Hex.parse(this.hex); | ||
const strDigest = String(encoder(message, this.secret)); | ||
return Buffer.from(strDigest, 'hex'); | ||
} | ||
} | ||
|
||
export default CryptoExpo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import hotp from 'otplib-hotp'; | ||
import totp from 'otplib-totp'; | ||
import authenticator from 'otplib-authenticator'; | ||
import crypto from './crypto'; | ||
|
||
/** | ||
* otplib | ||
* | ||
* One-Time Password Library | ||
* | ||
* ```js | ||
* { | ||
* authenticator // instance | ||
* hotp // instance | ||
* totp // instance | ||
* } | ||
* ``` | ||
* | ||
* @module otplib | ||
* @since 3.0.0 | ||
*/ | ||
authenticator.defaultOptions = { crypto }; | ||
hotp.defaultOptions = { crypto }; | ||
totp.defaultOptions = { crypto }; | ||
|
||
export { authenticator, hotp, totp }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as otplib from './index'; | ||
import rfc6238 from 'tests/rfc6238'; | ||
|
||
describe('index', () => { | ||
rfc6238.table.forEach(row => { | ||
const id = `${row.algorithm} / ${row.epoch}`; | ||
test(`[${id}] otplib.totp`, () => { | ||
otplib.totp.options = { | ||
epoch: row.epoch, | ||
algorithm: row.algorithm, | ||
digits: 8 | ||
}; | ||
|
||
expect(otplib.totp.check(row.token, rfc6238.secret)).toBe(true); | ||
}); | ||
}); | ||
}); |