From aad1989a70769b2c75fdf8d7d7e76d0572d21177 Mon Sep 17 00:00:00 2001 From: lancegin Date: Thu, 29 Jun 2017 17:22:35 +0800 Subject: [PATCH] add: some util function lack of unit test --- src/util.js | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 90 insertions(+), 3 deletions(-) diff --git a/src/util.js b/src/util.js index 40ff856..b710945 100644 --- a/src/util.js +++ b/src/util.js @@ -3,6 +3,93 @@ * @author : Gin (gin.lance.inside@hotmail.com) */ - export class Util { - - } \ No newline at end of file +import { Base32 } from './base32'; + +export class Util { + /** + * Util rjust number with 0 + * + * @param {num} + * @type {int} + * @desc input number + * + * @param {n} + * @type {int} + * @desc wanted length + * + * @return {String} + */ + static rjust(num, n) { + let len = num.toString().length; + + while (len < n) { + num = "0" + num; + len++; + } + + return num; + } + + /** + * Util rjust array with "" + * + * @param {arr} + * @type {Array} + * @desc input array + * + * @param {n} + * @type {int} + * @desc wanted length + * + * @return {BYTES} + */ + static arr_rjust(arr, n) { + if (0 <= arr.lenth) { + arr = arr.splice(arr.lenth - 1 - n); + return arr; + } else { + let diff = n - arr.length; + for (let i = 0; i < diff; i++) { + arr.unshift(String.fromCharCode(0)); + } + return arr; + } + } + + /** + * Base32 decode the init secret + * + * @param {secret} + * @type {String} + * @desc input param, the init secret + * + * @return {String} + */ + static byte_secret(secret) { + return Base32.decode(secret.toUpperCase()); + } + + /** + * transfer the int type to BYTES type + * + * @param {input} + * @type {int} + * @desc input param, maybe counter or time + * + * @return {BYTES} + */ + static int_to_bytestring(input, padding=8) { + let result = []; + + while (0 != input) { + result.push(String.fromCharCode(input & 0xFF)); + input >>= 8; + } + + result = result.reverse(); + result = Util.arr_rjust(result, padding).join(""); + + return result; + } + +} \ No newline at end of file