Created
March 23, 2023 14:46
-
-
Save tresf/432a3f0d52f9dcbe2028b5c8ae749c6d to your computer and use it in GitHub Desktop.
Signing using Deno + QZ Tray
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Author: A. Tres Finocchiaro | |
* License: Public Domain | |
*/ | |
import "https://raw.githubusercontent.com/qzind/tray/v2.2.2/js/qz-tray.js"; | |
import { encode, decode } from "https://deno.land/std/encoding/base64.ts"; | |
// Load certificate | |
qz.security.setCertificatePromise(function(resolve, reject) { | |
Deno.readTextFile("./digital-certificate.txt").then(text => { | |
resolve(text); | |
}).catch((err) => reject(err)); | |
}); | |
// Load private key | |
Deno.readTextFile("./private-key.pem").then(text => { | |
// Clean "-----BEGIN PRIVATE KEY-----" and newlines | |
let cleaned = text.split("-----")[2].replace(/\s+/g, ''); | |
let bytes = decode(cleaned); | |
crypto.subtle.importKey( | |
"pkcs8", | |
bytes, | |
{ name: "RSASSA-PKCS1-v1_5", hash: "SHA-512" }, | |
true, | |
["sign"] | |
).then(cryptoKey => { | |
qz.security.setSignatureAlgorithm("SHA512"); | |
qz.security.setSignaturePromise((toSign) => { | |
return (resolve, reject) => { | |
let encoder = new TextEncoder(); | |
crypto.subtle.sign( | |
"RSASSA-PKCS1-v1_5", | |
cryptoKey, | |
encoder.encode(toSign) | |
).then(signature => { | |
console.log(encode(signature)); | |
resolve(encode(signature)); | |
}); | |
}; | |
}); | |
}) | |
}).catch((err) => console.error(err)); | |
// Connect and print | |
qz.websocket.connect().then(() => { | |
return qz.printers.find("PDF"); | |
}).then((printer) => { | |
console.log(printer); | |
}).catch((err) => { | |
console.error(err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment