mirror of
https://github.com/LanceGin/jsotp.git
synced 2026-05-05 00:00:03 +08:00
add: some util function lack of unit test
This commit is contained in:
+90
-3
@@ -3,6 +3,93 @@
|
||||
* @author : Gin (gin.lance.inside@hotmail.com)
|
||||
*/
|
||||
|
||||
export class Util {
|
||||
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user