import { lib, algo, enc, HmacSHA256, PBKDF2 } from 'crypto-js';
import { hexStringToBuffer } from '@iotize/common/byte-converter';
import { KaitaiStreamWriter } from '@iotize/common/byte-stream';
import { ShaHasher } from '@iotize/common/crypto';

var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spread = (this && this.__spread) || function () {
    for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
    return ar;
};
var CryptoHelper = /** @class */ (function () {
    function CryptoHelper() {
    }
    CryptoHelper.sanitizeInput = function (input) {
        if (input instanceof Uint8Array) {
            return CryptoHelper.byteArrayToWordArray(input);
        }
        else {
            return input;
        }
    };
    CryptoHelper.byteArrayToWordArray = function (ba) {
        var wa = [];
        for (var i = 0; i < ba.length; i++) {
            wa[(i / 4) | 0] |= ba[i] << (24 - 8 * i);
        }
        var result = lib.WordArray.create(wa);
        result.sigBytes = ba.length;
        return result;
    };
    CryptoHelper.getAlgoImpl = function (type) {
        switch (type) {
            case 'sha1':
                return algo.SHA1;
            case 'sha256':
                return algo.SHA256;
            default:
                throw new Error('Unknown algo type: ' + type);
        }
    };
    CryptoHelper.wordToByteArray = function (word, length) {
        var ba = [];
        if (length > 0)
            ba.push(word >>> 24);
        if (length > 1)
            ba.push((word >>> 16) & 0xff);
        if (length > 2)
            ba.push((word >>> 8) & 0xff);
        if (length > 3)
            ba.push(word & 0xff);
        return Uint8Array.from(ba);
    };
    CryptoHelper.libWordArrayToByteArray = function (libWordArray, length) {
        if (!length) {
            length = libWordArray.sigBytes;
        }
        var wordArray = libWordArray.words;
        var result = [];
        var i = 0;
        while (length > 0) {
            var bytes = CryptoHelper.wordToByteArray(wordArray[i], Math.min(4, length));
            length -= bytes.length;
            result.push(bytes);
            i++;
        }
        return KaitaiStreamWriter.mergeArrays.apply(KaitaiStreamWriter, __spread(result));
    };
    CryptoHelper.wordArrayToByteArray = function (input) {
        return hexStringToBuffer(input.toString(enc.Hex));
    };
    return CryptoHelper;
}());

function hmacSHA256(data, key) {
    var wordArray = CryptoHelper.sanitizeInput(data);
    var keyArray = CryptoHelper.sanitizeInput(key);
    var result = HmacSHA256(wordArray, keyArray);
    return CryptoHelper.wordArrayToByteArray(result);
}

/**
 * PBKDF2 function (pssword-based key derivation function)
 * @param input
 * @param salt
 * @iterations
 * @param keySize number of 32 bit words
 */
function pbkdf2(input, salt, iterations, keySize) {
    var wordInput = CryptoHelper.sanitizeInput(input);
    var wordSalt = CryptoHelper.sanitizeInput(salt);
    var options = {
        iterations: iterations,
        keySize: keySize,
        hasher: algo.SHA256
    };
    if (options.keySize === undefined) {
        delete options.keySize;
    }
    var hash = PBKDF2(wordInput, wordSalt, options);
    return CryptoHelper.wordArrayToByteArray(hash);
}

var ALGO_TYPE = 'sha1';
var ITERATION_NUMBER = 4096;
var KEY_SIZE = 128;
var DEFAULT_SALT = 'n0 salt in 1oTi2e'; // TODO read from iotize ...
var passwordHasher = new ShaHasher({
    type: ALGO_TYPE,
    iterations: ITERATION_NUMBER,
    salt: DEFAULT_SALT,
    keySize: KEY_SIZE
});

/**
 * Generated bundle index. Do not edit.
 */

export { hmacSHA256, passwordHasher, pbkdf2 };
//# sourceMappingURL=iotize-tap-crypto.js.map
