// Missing functions for using hwcrypto.js library

function checkEstIdPlugin(cb) {
  hwcrypto.use("chrome").then(function(result) {
    if (result) {
      hwcrypto.debug().then(function(result) {
        if (-1 !== result.indexOf('failing')) {
          hwcrypto.use("npapi").then(cb);
          return;
        }
        cb(true);
      });
      return;
    }
    hwcrypto.use("npapi").then(cb);
  });
}

function _hex2array(str) {
  if (typeof str == "string") {
    var ret = new Uint8Array(Math.floor(str.length / 2));
    var i = 0;
    str.replace(/(..)/g, function(str) {
      ret[i++] = parseInt(str, 16);
    });
    return ret;
  }
}

function _array2hex(args) {
  var ret = "";
  for (var i = 0; i < args.length; i++) ret += (args[i] < 16 ? "0" : "") + args[i].toString(16);
  return ret;
}
@@ -134,16 +134,17 @@
     if (
+!
 result) 
@@ -143,24 +143,53 @@
 result) %7B%0A  
+    return tryNpapi();%0A    %7D%0A
     hwcrypto
@@ -217,26 +217,24 @@
 n(result) %7B%0A
-  
       if (-1
@@ -279,44 +279,322 @@
     
-  hwcrypto.use(%22npapi%22).then(cb);%0A  
+return tryNpapi();%0A      %7D%0A      cb(true);%0A    %7D);%0A  %7D);%0A  function checkNavigatorPlugin(mimeType) %7B%0A    if (!navigator %7C%7C !navigator.plugins) %7B%0A      return false;%0A    %7D%0A    for (var i = 0; i %3C navigator.plugins.length; i++) %7B%0A      if (navigator.plugins%5Bi%5D%5B'0'%5D && navigator.plugins%5Bi%5D%5B'0'%5D.type === mimeType) %7B%0A
     
@@ -603,24 +603,29 @@
   return
+ true
 ;%0A      
   %7D%0A    
@@ -620,60 +620,211 @@
     
+%7D%0A  
  
-
  %7D%0A    
-    cb(true);%0A      %7D);%0A      return;%0A    %7D%0A
+return false;%0A  %7D%0A  function tryNpapi() %7B%0A    var isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') %3E -1;%0A    if (!isChrome %7C%7C checkNavigatorPlugin('application/x-digidoc')) %7B%0A  
     
@@ -855,21 +855,55 @@
 en(cb);%0A
+  
   %7D
-);
+ else %7B%0A      cb(false);%0A    %7D%0A  %7D
 %0A%7D%0A%0Afunc
@@ -1294,8 +1294,620 @@
 n ret;%0A%7D
+%0A%0Afunction hex2char(hex) %7B%0A  hex = hex.match(/%5B0-9a-f%5D%7B2%7D/igm);%0A  if (!hex) %7B return %22%22; %7D%0A%0A  hex = hex.map(function(el)%7B%0A    return String.fromCharCode(parseInt(el,16));%0A  %7D);%0A  return hex.join(%22%22);%0A%7D%0A%0Afunction hex2b64(hex) %7B%0A  return btoa(hex2char(hex));%0A%7D%0A%0Afunction toCertFormat(data) %7B%0A  data = hex2b64(data);%0A  var lineLength = 64;%0A  var result = %22-----BEGIN CERTIFICATE-----%5Cn%22;%0A  for (var i = 0; i %3C data.length; i += lineLength) %7B%0A    result += i %3E= data.length - lineLength ? data.substring(i) : data.substring(i, i + lineLength) + %22%5Cn%22;%0A  %7D%0A  result += %22%5Cn-----END CERTIFICATE-----%22;%0A  return result;%0A%7D

Improve hwcrypto-missing.js to avoid trying NPAPI if it does not exist.