var debugLocal = true

//tokenList = ['8bc766ae05120ad091e8e52c29e23c1e']

function arrayBufferToBase64(bytes) {
    var binary = '';
    var len = bytes.byteLength;
    for (var i = 0; i < len; i++) {
        binary += String.fromCharCode(bytes[i]);
    }
    return window.btoa(binary);
}

function hexStringToUint8Array(hexString) {
    if (hexString.length % 2 !== 0) {
        throw "Invalid hexString";
    }/*from  w w w.  j  av a 2s  . c  o  m*/
    var arrayBuffer = new Uint8Array(hexString.length / 2);

    for (var i = 0; i < hexString.length; i += 2) {
        var byteValue = parseInt(hexString.substr(i, 2), 16);
        if (isNaN(byteValue)) {
            throw "Invalid hexString";
        }
        arrayBuffer[i / 2] = byteValue;
    }

    return arrayBuffer;
}

var decrypt = function (key, iv, crypted) {
    key = CryptoJS.enc.Utf8.parse("sBWBAyOaGqkuEhCw");  //十六位十六进制数作为密钥
    iv = CryptoJS.enc.Utf8.parse('sBWBAyOaGqkuEhCw');   //十六位十六进制数作为密钥偏移量

    encrypted2 = arrayBufferToBase64(crypted)

    var decrypted = CryptoJS.AES.decrypt(encrypted2, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });

    var originalText = decrypted.toString();
    originalText = hexStringToUint8Array(originalText)

    console.log("originalText", originalText)

    return originalText
};

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) { return pair[1]; }
    }
    return (false);
}