From b144a14efecd028dd3d7bab3064323ea57fb7da3 Mon Sep 17 00:00:00 2001 From: lancegin Date: Wed, 28 Jun 2017 21:03:18 +0800 Subject: [PATCH] add: basic function with comments --- src/base32.js | 6 ++++++ src/hotp.js | 44 +++++++++++++++++++++++++++++++++++++- src/otp.js | 39 ++++++++++++++++++++++++++++------ src/totp.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 139 insertions(+), 9 deletions(-) diff --git a/src/base32.js b/src/base32.js index 7032c64..05f7ff5 100644 --- a/src/base32.js +++ b/src/base32.js @@ -5,5 +5,11 @@ */ class Base32 { + static decode() { + console.log("static Base32.decode"); + } + static random_gen() { + console.log("static Base32.random_gen"); + } } \ No newline at end of file diff --git a/src/hotp.js b/src/hotp.js index c5ad118..ca8c694 100644 --- a/src/hotp.js +++ b/src/hotp.js @@ -6,5 +6,47 @@ import OTP from './otp'; class HOTP extends OTP { - + /** + * Generate the OTP with the given count + * + * @param {count} + * @type {int} + * @desc the OTP HMAC counter + * + * @return {OTP} + * + * @example + * ```javascript + * let hotp = jsotp.HOTP.gen('BASE32_ENCODED_SECRET'); + * hotp.at(0); // => 432143 + * ``` + */ + at(count) { + console.log("HOTP.at"); + } + + /** + * Verifies the OTP passed in against the current counter. + * + * @param {otp} + * @type {String} + * @desc the OTP waiting for checking + * + * @param {counter} + * @type {int} + * @desc the OTP HMAC counter + * + * @return {Boolean} + * + * @example + * ```javascript + * let hotp = jsotp.HOTP.gen('BASE32_ENCODED_SECRET'); + * hotp.at(0); // => 432143 + * hotp.verify(432143, 0); // => true + * hotp.verify(432143, 1); // => false + * ``` + */ + verify(otp, counter) { + console.log("HOTP.verify"); + } } \ No newline at end of file diff --git a/src/otp.js b/src/otp.js index d22d9c8..1c33994 100644 --- a/src/otp.js +++ b/src/otp.js @@ -4,17 +4,44 @@ */ export default class OTP { + /** + * This constructor will create OTP instance. + * + * @param {secret} + * @type {String} + * @desc random base32-encoded key, it is the + * key that be used to verify. + * + * @param {digits} + * @type {int} + * @desc the length of the one-time password, default to be 6 + * + * @param {digest} + * @type {String} + * @desc the key that be used to do HMAC encoding, dedault and + * only to be "sha1" + * + */ constructor(secret, digits=6, digest="sha1") { - /* - * This constructor will create OTP instance - */ this.secret = secret; this.digits = digits; this.digest = digest; - }; + } - generate_otp() { + /** + * When class HOTP or TOTP pass the input params to this + * function, it will generate the OTP object with params, + * the params may be counter or time. + * + * @param {input} + * @type {int} + * @desc input params to generate OTP object, maybe + * counter or time. + * + * @return {OTP} + */ + generate_otp(input) { console.log("OTP.generate_otp"); - }; + } } diff --git a/src/totp.js b/src/totp.js index ee0cee9..5951e28 100644 --- a/src/totp.js +++ b/src/totp.js @@ -3,6 +3,61 @@ * @author : Gin (gin.lance.inside@hotmail.com) */ - class TOTP { - +import OTP from './otp'; + + class TOTP extends OTP { + /** + * @param {interval} + * @type {int} + * @desc the time interval in seconds for OTP. + * This defaults to 30. + * + * @return {OTP} + */ + constructor(interval=30, ...args) { + this.interval = interval; + super(...args); + } + + /** + * Generate the OTP with current time. + * + * @return {OTP} + * + * @example + * ```javascript + * let totp = jsotp.TOTP.gen('BASE32_ENCODED_SECRET'); + * totp.now(); // => 432143 + * ``` + */ + now() { + console.log("TOTP.now"); + } + + /** + * Verifies the OTP passed in against the current time OTP. + * + * @param {otp} + * @type {String} + * @desc the OTP waiting for checking + * + * @param {time} + * @type {int or datetime} + * @desc Time to check OTP at (defaults to now) + * + * @return {Boolean} + * + * @example + * ```javascript + * let totp = jsotp.TOTP.gen('BASE32_ENCODED_SECRET'); + * totp.now(); // => 432143 + * // Verify for current time + * totp.verify(432143); // => true + * // Verify after 30s + * totp.verify(432143); // => false + * ``` + */ + verify(otp, time=null) { + console.log("TOTP.verify"); + } } \ No newline at end of file