From a6b7e7f5bb19e07bc78d261afe8712d19bb04fd9 Mon Sep 17 00:00:00 2001 From: lancegin Date: Fri, 30 Jun 2017 15:05:40 +0800 Subject: [PATCH] upd: group the classes and export to one --- src/jsotp.js | 39 +++++++++++++++++++++++++++++++++++---- test/jsotp_test.js | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 test/jsotp_test.js diff --git a/src/jsotp.js b/src/jsotp.js index 16868f8..3064314 100644 --- a/src/jsotp.js +++ b/src/jsotp.js @@ -5,16 +5,47 @@ * @Disc : a node module to generate and verify one-time passwords */ -import { OTP } from './otp'; import { TOTP } from './totp'; import { HOTP } from './hotp'; import { Base32 } from './base32'; import { Util } from './util'; +/** + * Generate and return HOTP object + * + * @param {secret} + * @type {String} + * @desc random base32-encoded key to generate OTP. + * + * @return {OTP} + */ +function hotp_gen(secret, digits=6, digest="SHA-1") { + let hotp = new HOTP(secret, digits, digest); + return hotp; +} + +/** + * Generate and return TOTP object + * + * @param {secret} + * @type {String} + * @desc random base32-encoded key to generate OTP. + * + * @param {interval} + * @type {int} + * @desc the time interval in seconds for OTP. + * This defaults to 30. + * + * @return {OTP} + */ +function totp_gen(secret, interval=30) { + let totp = new TOTP(secret, interval); + return totp; +} + export { - OTP, - TOTP, - HOTP, + hotp_gen as HOTP, + totp_gen as TOTP, Base32, Util }; \ No newline at end of file diff --git a/test/jsotp_test.js b/test/jsotp_test.js new file mode 100644 index 0000000..9575325 --- /dev/null +++ b/test/jsotp_test.js @@ -0,0 +1,40 @@ +let jsotp = require("../lib/jsotp"); +var assert = require('assert'); + +describe('jsotp class test', function() { + describe('is HOTP module import?', function() { + + let hotp = jsotp.HOTP("J22U6B3WIWRRBTAV"); + + it("should return object", function() { + assert.equal("object", typeof(hotp)) + }); + }); + + describe('is TOTP module import?', function() { + + let totp = jsotp.TOTP("J22U6B3WIWRRBTAV"); + + it("should return object", function() { + assert.equal("object", typeof(totp)) + }); + }); + + describe('is Base32 module import?', function() { + + let b32_random = jsotp.Base32.random_gen(); + + it("should return string", function() { + assert.equal("string", typeof(b32_random)) + }); + }); + + describe('is Util module import?', function() { + + let now = jsotp.Util.timecode(new Date(), 30); + + it("should return int", function() { + assert.equal("number", typeof(now)) + }); + }); +});