JavaScript generic ASN.1 parser (mirror)
at github-7 3.2 kB view raw
1/*jshint browser: true, strict: true, globalstrict: true, indent: 4, immed: true, latedef: true, undef: true, regexdash: false */ 2/*global Hex, Base64, ASN1 */ 3"use strict"; 4 5var reHex = /^\s*(?:[0-9A-Fa-f][0-9A-Fa-f]\s*)+$/, 6 hash = null; 7function id(elem) { 8 return document.getElementById(elem); 9} 10function toHTML(obj) { 11 return String(obj).replace(/</g, "&lt;"); 12} 13function decode(der) { 14 var tree = id('tree'); 15 var dump = id('dump'); 16 tree.innerHTML = ''; 17 dump.innerHTML = ''; 18 try { 19 var asn1 = ASN1.decode(der), 20 hex = asn1.toHexString(); 21 tree.appendChild(asn1.toDOM()); 22 if (id('wantHex').checked) 23 dump.appendChild(asn1.toHexDOM()); 24 if (id('pem').value === '') 25 id('pem').value = hex; 26 // update URL hash 27 if (hex.length < 10240) 28 hash = window.location.hash = '#' + hex; 29 } catch (e) { 30 tree.innerHTML = toHTML(e); 31 } 32 return false; 33} 34function decodeArea() { 35 try { 36 var pem = id('pem').value; 37 var der = reHex.test(pem) ? Hex.decode(pem) : Base64.unarmor(pem); 38 decode(der); 39 } catch (e) { 40 id('tree').innerHTML = toHTML(e); 41 id('dump').innerHTML = ''; 42 } 43 return false; 44} 45function decodeBinaryString(str) { 46 var der; 47 try { 48 if (reHex.test(str)) 49 der = Hex.decode(str); 50 else if (Base64.re.test(str)) 51 der = Base64.unarmor(str); 52 else 53 der = str; 54 decode(der); 55 } catch (e) { 56 id('tree').innerHTML = 'Cannot decode file.'; 57 id('dump').innerHTML = ''; 58 } 59 return false; 60} 61function clearAll() { 62 id('pem').value = ''; 63 id('tree').innerHTML = ''; 64 id('dump').innerHTML = ''; 65 hash = window.location.hash = ''; 66 return false; 67} 68// this is only used if window.FileReader 69function read(f) { 70 id('pem').value = ''; // clear text area, will get hex content 71 var r = new FileReader(); 72 r.onloadend = function () { 73 if (r.error) { 74 alert("Your browser couldn't read the specified file (error code " + r.error.code + ")."); 75 } else 76 decodeBinaryString(r.result); 77 }; 78 r.readAsBinaryString(f); 79} 80function load() { 81 var file = id('file'); 82 if (file.files.length === 0) { 83 alert("Select a file to load first."); 84 return false; 85 } 86 read(file.files[0]); 87 return false; 88} 89function loadFromHash() { 90 if (window.location.hash && window.location.hash != hash) { 91 hash = window.location.hash; 92 id('pem').value = hash.substring(1); 93 decodeArea(); 94 } 95} 96function stop(e) { 97 e.stopPropagation(); 98 e.preventDefault(); 99} 100function dragAccept(e) { 101 stop(e); 102 if (e.dataTransfer.files.length > 0) 103 read(e.dataTransfer.files[0]); 104} 105window.onload = function () { 106 if ('onhashchange' in window) 107 window.onhashchange = loadFromHash; 108 loadFromHash(); 109 document.ondragover = stop; 110 document.ondragleave = stop; 111 if ('FileReader' in window) { 112 id('file').style.display = 'block'; 113 id('file').onchange = load; 114 document.ondrop = dragAccept; 115 } 116};