All files hotp.js

64.58% Statements 31/48
50% Branches 12/24
50% Functions 1/2
64.58% Lines 31/48
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70    2x 2x 2x 2x                                                       3x 3x   3x 1x 3x 2x 3x 3x 3x 3x 3x 3x 3x 3x 3x 24x 24x   3x 3x 3x   3x       3x 3x   3x 3x   3x   2x  
"use strict";
// https://tools.ietf.org/html/rfc4226
Object.defineProperty(exports, "__esModule", { value: true });
const crypto = require("crypto");
const doubleDigits = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9];
const digitsPower = [
    1,
    10,
    100,
    1000,
    10000,
    100000,
    1000000,
    10000000,
    100000000
];
function calcChecksum(num, digits) {
    let doubleDigit = true;
    let total = 0;
    while (0 < digits--) {
        let digit = num % 10;
        num /= 10;
        if (doubleDigit)
            digit = doubleDigits[digit];
        total += digit;
        doubleDigit = !doubleDigit;
    }
    let result = total % 10;
    if (result > 0)
        result = 10 - result;
    return result;
}
function default_1(parameters) {
    let { secret, movingFactor, codeDigits, addChecksum, truncationOffset, hmacAlgorithm } = parameters;
    Iif (!secret)
        throw new Error('no secret value');
    if (!movingFactor)
        movingFactor = 0;
    if (!codeDigits)
        codeDigits = 6;
    Eif (!addChecksum)
        addChecksum = false;
    Eif (!truncationOffset)
        truncationOffset = -1;
    Eif (!hmacAlgorithm)
        hmacAlgorithm = 'sha1';
    const digits = addChecksum ? codeDigits + 1 : codeDigits;
    const text = new Buffer(8);
    for (let i = text.length - 1; i >= 0; i--) {
        text[i] = movingFactor & 0xff;
        movingFactor >>= 8;
    }
    const hash = crypto.createHmac(hmacAlgorithm, secret).update(text).digest();
    let offset = hash[hash.length - 1] & 0xf;
    Iif (0 <= truncationOffset && truncationOffset < hash.length - 4)
        offset = truncationOffset;
    const binary = ((hash[offset] & 0x7f) << 24) |
        ((hash[offset + 1] & 0xff) << 16) |
        ((hash[offset + 2] & 0xff) << 8) |
        (hash[offset + 3] & 0xff);
    let otp = binary % digitsPower[codeDigits];
    Iif (addChecksum)
        otp = otp * 10 + calcChecksum(otp, codeDigits);
    let result = otp.toString();
    while (result.length < digits)
        result = '0' + result;
    return result;
}
exports.default = default_1;