misc-bdauvergne/qrcode-certificate/v0/qrcode-reader/index.html

90 lines
4.1 KiB
HTML

<html>
<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/js-nacl/1.4.0/nacl_factory.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Base64/1.1.0/base64.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/html5-qrcode"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>
<body>
<div class="container">
<label>SCAN QR CODE</label>
<div id="reader" style="width: 70vmin"></div>
<label>RESULT</label>
<pre id="text" style="font-size: 2rem; white-space: pre-wrap;"></pre>
</div>
</div>
<script>
var base64url_to_base64 = function(input) {
// Replace non-url compatible chars with base64 standard chars
input = input
.replace(/-/g, '+')
.replace(/_/g, '/');
// Pad out with standard base64 required padding characters
var pad = input.length % 4;
if(pad) {
if(pad === 1) {
throw new Error('InvalidLengthError: Input base64url string is the wrong length to determine padding');
}
input += new Array(5-pad).join('=');
}
return input;
}
function b64urldecode(base64url) {
var binary_string = window.atob(base64url_to_base64(base64url));
console.log('binary string', binary_string)
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes;
}
nacl_factory.instantiate(function (nacl) {
let key = '79a441b73a99ead8c4446aa36c973dd23787d459122fe2169232a07a181e37a9';
let uint8_key = nacl.from_hex(key);
function onScanSuccess(c, decodedResult) {
// handle the scanned code as you like, for example:
let pre = document.getElementById('text');
pre.textContent = 'Decoding "' + c + '"\n';
console.log('1');
let parts = c.split('.');
console.log('2');
if (parts.length != 2) {
pre.textContent += "ERROR wrong length";
return true;
}
try {
let b64_payload = parts[0];
let b64_signature = parts[1];
let payload = b64urldecode(b64_payload);
let signature = b64urldecode(b64_signature);
let decoder = new TextDecoder('utf-8');
const jsonString = decoder.decode(payload);
const signatureString = decoder.decode(signature);
const data = JSON.parse(jsonString)
console.log(signature);
console.log(nacl.to_hex(signature));
} catch (e) {
pre.textContent += 'ERROR ' + e;
}
if (! nacl.crypto_sign_verify_detached(signature, payload, uint8_key)) {
pre.textContent += "ERROR wrong signature";
return true;
}
pre.textContent = "signature OK\n" + JSON.stringify(data, null, " ");
return true;
}
let scanner = new Html5Qrcode("reader");
scanner.start({ facingMode: "environment" }, { fps: 10, qrbox: {width: 500, height: 500} }, onScanSuccess);
});
</script>
</body>
</html>