Begin adding support for browser

This commit is contained in:
Skylar Ittner 2021-07-08 23:29:14 -06:00
parent 8e73504214
commit a56ea40640
3 changed files with 128 additions and 61 deletions

View File

@ -274,9 +274,8 @@ function calculateSHA256HashOfString(str) {
} }
function openPublicKeyFile() { function openPublicKeyFile() {
openFileDialog(function (path) { openFileDialog(function (path, html5file) {
var keyfile = getFileAsString(path); var importpk = function (keyfile) {
kbpgp.KeyManager.import_from_armored_pgp({ kbpgp.KeyManager.import_from_armored_pgp({
armored: keyfile armored: keyfile
}, function (err, pubkeymgr) { }, function (err, pubkeymgr) {
@ -287,6 +286,18 @@ function openPublicKeyFile() {
alert("Error loading public key: " + err); alert("Error loading public key: " + err);
} }
}); });
};
if (typeof nw != 'undefined') {
var keyfile = getFileAsString(path);
importpk(keyfile);
} else {
var fileReader = new FileReader();
fileReader.onload = function (e) {
importpk(e.target.result);
}
fileReader.readAsText(html5file);
}
}, ".asc"); }, ".asc");
} }

View File

@ -27,7 +27,7 @@ function openFileDialog(callback, accept) {
dialog.setAttribute("accept", accept); dialog.setAttribute("accept", accept);
} }
dialog.onchange = function () { dialog.onchange = function () {
callback(dialog.value); callback(dialog.value, this.files[0]);
} }
dialog.dispatchEvent(new MouseEvent("click", { dialog.dispatchEvent(new MouseEvent("click", {
"view": window, "view": window,

View File

@ -56,9 +56,8 @@ function analyzeSignedPDF() {
return; return;
} }
closePDF(false); closePDF(false);
openFileDialog(function (path) { openFileDialog(function (path, html5file) {
var pdf = Buffer.from(getFileAsUint8Array(path).buffer); var analyze = function (pdf) {
var splitindex = pdf.indexOf("-----BEGIN PGP MESSAGE-----"); var splitindex = pdf.indexOf("-----BEGIN PGP MESSAGE-----");
if (splitindex == -1) { if (splitindex == -1) {
alert("Selected file does not contain any recognized signature data."); alert("Selected file does not contain any recognized signature data.");
@ -67,13 +66,13 @@ function analyzeSignedPDF() {
var pdfdata = pdf.slice(0, splitindex); var pdfdata = pdf.slice(0, splitindex);
var sigdata = pdf.slice(splitindex).toString(); var sigdata = pdf.slice(splitindex).toString();
var pdfhash = calculateSHA256HashOfString(pdfdata); var verify = function (pdfhash) {
loadKeyFromLocalStorage(function () { loadKeyFromLocalStorage(function () {
verifyMessage(sigdata, function (msg, fprint) { verifyMessage(sigdata, function (msg, fprint) {
parseAndDisplaySignature(msg, pdfhash, true, fprint); parseAndDisplaySignature(msg, pdfhash, true, fprint);
}, function (err) { }, function (err) {
console.error(err); console.error(err);
console.log(sigdata);
var base64 = sigdata.split("\n\n", 2)[1].split("\n-----END PGP MESSAGE-----")[0]; var base64 = sigdata.split("\n\n", 2)[1].split("\n-----END PGP MESSAGE-----")[0];
base64 = base64.substring(0, base64.lastIndexOf("\n")).replaceAll("\n", ""); base64 = base64.substring(0, base64.lastIndexOf("\n")).replaceAll("\n", "");
try { try {
@ -86,12 +85,55 @@ function analyzeSignedPDF() {
}); });
}); });
if (typeof nw != 'undefined') {
pdfjsLib.getDocument(pdf).promise.then(function (pdfDoc_) { pdfjsLib.getDocument(pdf).promise.then(function (pdfDoc_) {
pdfDoc = pdfDoc_; pdfDoc = pdfDoc_;
renderAllPages(pdfDoc); renderAllPages(pdfDoc);
pdfZoom("fitheight"); pdfZoom("fitheight");
}); });
} else {
var fileReader = new FileReader();
fileReader.onload = function () {
pdfjsLib.getDocument(new Uint8Array(this.result)).promise.then(function (pdfDoc_) {
pdfDoc = pdfDoc_;
renderAllPages(pdfDoc);
pdfZoom("fitheight");
});
};
fileReader.readAsArrayBuffer(html5file);
}
};
if (typeof nw != 'undefined') {
verify(calculateSHA256HashOfString(pdfdata));
} else {
window.crypto.subtle.digest("SHA-256", (new TextEncoder()).encode(pdfdata))
.then(hash => {
window.hash = hash;
// here hash is an arrayBuffer,
// so we'll connvert it to its hex version
let result = '';
const view = new DataView(hash);
for (let i = 0; i < hash.byteLength; i += 4) {
result += ('00000000' + view.getUint32(i).toString(16)).slice(-8);
}
verify(result);
});
}
};
if (typeof nw != 'undefined') {
// running in NW.js so we have Node
analyze(Buffer.from(getFileAsUint8Array(path).buffer));
} else {
// no Node :(
var fileReader = new FileReader();
fileReader.onload = function (e) {
analyze(e.target.result);
}
fileReader.readAsBinaryString(html5file);
}
}, ".pdf"); }, ".pdf");
} }
@ -143,6 +185,7 @@ then run the analyze tool again to prove if it was changed since notarization.")
$("#verifyModalDetailedInfoList").append('<li class="list-group-item"><i class="fas fa-map-marked-alt fa-fw"></i> State: ' + sanitizeHTMLString(msgparts["STATE"]).toUpperCase() + '</li>'); $("#verifyModalDetailedInfoList").append('<li class="list-group-item"><i class="fas fa-map-marked-alt fa-fw"></i> State: ' + sanitizeHTMLString(msgparts["STATE"]).toUpperCase() + '</li>');
} }
if (typeof msgparts["OTS"] == "string") { if (typeof msgparts["OTS"] == "string") {
try {
var bytearray = []; var bytearray = [];
var bytestrarray = msgparts["OTS"].match(/.{1,3}/g); var bytestrarray = msgparts["OTS"].match(/.{1,3}/g);
for (var i = 0; i < bytestrarray.length; i++) { for (var i = 0; i < bytestrarray.length; i++) {
@ -150,7 +193,11 @@ then run the analyze tool again to prove if it was changed since notarization.")
} }
const detached = OpenTimestamps.DetachedTimestampFile.fromHash(new OpenTimestamps.Ops.OpSHA256(), Uint8Array.from(Buffer.from(pdfhash, 'hex'))); const detached = OpenTimestamps.DetachedTimestampFile.fromHash(new OpenTimestamps.Ops.OpSHA256(), Uint8Array.from(Buffer.from(pdfhash, 'hex')));
const detachedOts = OpenTimestamps.DetachedTimestampFile.deserialize(bytearray); const detachedOts = OpenTimestamps.DetachedTimestampFile.deserialize(bytearray);
let options = {}; console.log(OpenTimestamps.info(detachedOts));
let options = {
ignoreBitcoinNode: true,
timeout: 5000
};
OpenTimestamps.verify(detachedOts, detached, options).then(verifyResult => { OpenTimestamps.verify(detachedOts, detached, options).then(verifyResult => {
console.log(verifyResult); console.log(verifyResult);
if (typeof verifyResult != "undefined") { if (typeof verifyResult != "undefined") {
@ -162,6 +209,9 @@ then run the analyze tool again to prove if it was changed since notarization.")
} }
} }
}); });
} catch (ex) {
console.error(ex);
}
} }
$("#verifyModalDetailedInfoList").append('<li class="list-group-item"><i class="far fa-file fa-fw"></i> Actual file hash: ' + pdfhash + '</li>'); $("#verifyModalDetailedInfoList").append('<li class="list-group-item"><i class="far fa-file fa-fw"></i> Actual file hash: ' + pdfhash + '</li>');
@ -287,6 +337,12 @@ function savePDF() {
function pdfZoom(str) { function pdfZoom(str) {
disableGuideBox(); disableGuideBox();
if ($("#page-canvas-container .page-canvas").length == 0) {
setTimeout(function () {
pdfZoom(str);
}, 100);
return;
}
var widthpx = $("#page-canvas-container .page-canvas").css("width").replace("px", "") * 1; var widthpx = $("#page-canvas-container .page-canvas").css("width").replace("px", "") * 1;
var zoomstep = 100; var zoomstep = 100;
console.log(widthpx); console.log(widthpx);