// The MIT License (MIT)
// 
// Copyright (c) 2017 Mehdi Sotoodeh, KryptoLogik Inc.
// 
// Permission is hereby granted, free of charge, to any person obtaining 
// a copy of this software and associated documentation files (the 
// "Software"), to deal in the Software without restriction, including 
// without limitation the rights to use, copy, modify, merge, publish, 
// distribute, sublicense, and/or sell copies of the Software, and to 
// permit persons to whom the Software is furnished to do so, subject to 
// the following conditions:
// 
// The above copyright notice and this permission notice shall be included 
// in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// 

var AP_ALGO_ED25519             = "ed255";    // ed25519-hmac-sha512
var AP_ALGO_ECDSA_SECP256R1     = "p256";     // secp256r1-hmac-sha256
var AP_ALGO_ECDSA_SECP256K1     = "k256";     // secp256k1-hmac-sha256

var AP_TAG_NEW_USER             = "nu";
var AP_TAG_CHANGE_PASSWORD      = "cp";
var AP_TAG_RESET_PASSWORD       = "rp";
var AP_TAG_USER_LOGIN           = "li";
var AP_TAG_UPDATE_TOKEN         = "ut";
var AP_TAG_CHALLENGE_RESPONSE   = "cr";

function strToU8Array(str) {
    var n = 0, c, bytes = new Uint8Array(str.length * 3);
    for (var i = 0; i < str.length; i++) {
        c = str.charCodeAt(i);
        if (c < 0x0080) {
            bytes[n++] = c & 0x7F;
        } else if (c < 0x0800) {
            bytes[n++] = 0xC0 | ((c >>  6) & 0x1F);
            bytes[n++] = 0x80 | ((c >>  0) & 0x3F);
        } else {
            bytes[n++] = 0xE0 | ((c >> 12) & 0x0F);
            bytes[n++] = 0x80 | ((c >>  6) & 0x3F);
            bytes[n++] = 0x80 | ((c >>  0) & 0x3F);
        }
    }
    return bytes.slice(0, n);
}

// url-safe base64 encoder/decoder
var b64table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";

function b64encode(byteArray) {
    var accu = 0, bits = 0, o = "";
    for (var i = 0; i < byteArray.length; i++) {
        accu = (accu << 8) | (byteArray[i] & 0xff);
        bits += 8;

        while (bits >= 6) {
            bits -= 6;
            o = o + b64table[(accu >>> bits) & 0x3f];
        }
    }
    // flush remaining bits (if any)
    if (bits > 0) o = o + b64table[(accu << (6 - bits)) & 0x3f];
    return o;
}

function b64decode(b64string) {
    var accu = 0, bits = 0, n = 0, index = 0;
    var bytes = [];
    while (true) {
        while (bits < 8) {
            if (index >= b64string.length) return new Uint8Array(bytes);
            var c = b64table.indexOf(b64string.charAt(index++));
            if (c < 0 || c > 63) return new Uint8Array(bytes);
            accu = (accu << 6) + c;
            bits += 6;
        }

        bits -= 8;
        bytes[n++] = (accu >>> bits) & 0xff;
    }
}

@@ -1701,23 +1701,16 @@
 %0A    var
- n = 0,
  c, byte
@@ -1717,38 +1717,10 @@
 s = 
-new Uint8Array(str.length * 3)
+%5B%5D
 ;%0D%0A 
@@ -1842,24 +1842,22 @@
 ytes
-%5Bn++%5D = 
+.push(
 c & 0x7F
 ;%0D%0A 
@@ -1852,16 +1852,17 @@
 c & 0x7F
+)
 ;%0D%0A     
@@ -1907,24 +1907,22 @@
    bytes
-%5Bn++%5D = 
+.push(
 0xC0 %7C (
@@ -1938,16 +1938,17 @@
  & 0x1F)
+)
 ;%0D%0A     
@@ -1955,32 +1955,30 @@
        bytes
-%5Bn++%5D = 
+.push(
 0x80 %7C ((c %3E
@@ -1982,32 +1982,33 @@
 c %3E%3E  0) & 0x3F)
+)
 ;%0D%0A        %7D els
@@ -2029,24 +2029,22 @@
    bytes
-%5Bn++%5D = 
+.push(
 0xE0 %7C (
@@ -2060,16 +2060,17 @@
  & 0x0F)
+)
 ;%0D%0A     
@@ -2077,32 +2077,30 @@
        bytes
-%5Bn++%5D = 
+.push(
 0x80 %7C ((c %3E
@@ -2104,32 +2104,33 @@
 c %3E%3E  6) & 0x3F)
+)
 ;%0D%0A            b
@@ -2133,24 +2133,22 @@
    bytes
-%5Bn++%5D = 
+.push(
 0x80 %7C (
@@ -2164,16 +2164,17 @@
  & 0x3F)
+)
 ;%0D%0A     
@@ -2201,24 +2201,28 @@
 urn 
-bytes.slice(0, n
+new Uint8Array(bytes
 );%0D%0A

fix for strToU8Array