From 6436b6d307f91d9746d93648e99a44cbf0d701a7 Mon Sep 17 00:00:00 2001 From: lancegin Date: Thu, 29 Jun 2017 17:22:55 +0800 Subject: [PATCH] upd: finish OTP class --- src/otp.js | 38 ++++++++++++++++++++++++++++++++++++-- test/otp_test.js | 9 +++++---- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/otp.js b/src/otp.js index 15428e5..9c7d950 100644 --- a/src/otp.js +++ b/src/otp.js @@ -2,6 +2,9 @@ * @module : OTP module to generate the password * @author : Gin (gin.lance.inside@hotmail.com) */ +const jsSHA = require("jssha"); +import { Base32 } from './base32'; +import { Util } from './util'; export class OTP { /** @@ -22,7 +25,7 @@ export class OTP { * only to be "sha1" * */ - constructor(secret, digits=6, digest="sha1") { + constructor(secret, digits=6, digest="SHA-1") { this.secret = secret; this.digits = digits; this.digest = digest; @@ -41,7 +44,38 @@ export class OTP { * @return {OTP} */ generate_otp(input) { - return "OTP.generate_otp"; + // generate HMAC object with SHA-1 digest + let hmacObj = new jsSHA(this.digest, "BYTES"); + // set hmac token + hmacObj.setHMACKey(Util.byte_secret(this.secret), "BYTES"); + // hamc encode the input param + hmacObj.update(Util.int_to_bytestring(input)); + + // get HMAC ans + let hmac = hmacObj.getHMAC("BYTES"); + + // transfer hmac to Array + let hmac_a = hmac.split(""); + + // calculate the init offset + let offset = hmac_a[hmac_a.length - 1].charCodeAt() & 0xf; + + // calculate the code + let code = ( + (hmac_a[offset].charCodeAt() & 0x7f) << 24 | + (hmac_a[offset + 1].charCodeAt() & 0xff) << 16 | + (hmac_a[offset + 2].charCodeAt() & 0xff) << 8 | + (hmac_a[offset + 3].charCodeAt() & 0xff) + ); + + // get the init str code + let str_code = (code % 10 ** this.digits).toString(); + + // rjust format + str_code = Util.rjust(str_code, this.digits); + + return str_code; } + } diff --git a/test/otp_test.js b/test/otp_test.js index 6100e67..079eeab 100644 --- a/test/otp_test.js +++ b/test/otp_test.js @@ -3,12 +3,13 @@ var assert = require('assert'); describe('OTP module test', function() { - var OTP = otp.OTP; - var a = new OTP("BASE32_ENCODED_SECRET"); + var a = new otp.OTP("J22U6B3WIWRRBTAV"); + + console.log(a.generate_otp(49957590)); describe('generate_otp() function', function() { - it("should print 'OTP.generate_otp'", function() { - assert.equal("OTP.generate_otp", a.generate_otp()) + it("should print '139878'", function() { + assert.equal("139878", a.generate_otp(49957590)) }); }); });