-
Notifications
You must be signed in to change notification settings - Fork 3
/
mini.html.bak
124 lines (98 loc) · 3.67 KB
/
mini.html.bak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title></title>
</head>
<body>
<textarea id="encoded" name="encoded" cols=100 rows=10></textarea><br>
<textarea id="decoded" name="decoded" cols=100 rows=10 value = "prova">hello</textarea><br>
<span id="plain1" name="plain1">-</span> -->
<span id ="cod" name="cod">-</span> -->
<span id="plain2" name="plain2">-</span><br>
<button onclick="test()">Test</button><br>
<button onclick="vai()">Vai</button><br>
<script src="pako.min.js"></script>
<script src="my_base45_2.js"></script>
<script src="cbor.js" type="text/javascript"></script>
<script>
// https://dev.to/lmillucci/javascript-how-to-decode-the-greenpass-qr-code-3dh0
function convert(decoded) {
text="";
for (var i = 0; i < decoded.length; i++) {
text += String.fromCharCode(decoded[i]);
}
console.log("Text=",text);
values = text.split(",");
console.log("values: " ,values);
final = "";
for (var i=0; i<values.length; i++) {
final += String.fromCharCode(values[i])
}
return final;
}
function test() {
source = document.getElementById("decoded").value;
console.log("source=",source);
document.getElementById("plain1").innerHTML = source;
var enc = new TextEncoder();
buff= enc.encode(source);
console.log("buff:" , buff);
encoded2 = encode(buff);
console.log("encoded2:" , encoded2);
document.getElementById("cod").innerHTML = encoded2;
source = encoded2;
console.log("Encoded source=",source);
decoded = decode(source).enc;
converted = convert(decoded);
console.log("Decoded=", decoded);
console.log("converted=", converted);
document.getElementById("plain2").innerHTML = converted;
}
function buf2hex(buffer) {
// https://stackoverflow.com/questions/34309988/byte-array-to-hex-string-conversion-in-javascript
var u = new Uint8Array(buffer),
a = new Array(u.length),
i = u.length;
while (i--) // map to hex
a[i] = (u[i] < 16 ? '0' : '') + u[i].toString(16);
u = null; // free memory
return a.join('');
};
function typedArrayToBuffer(array) {
// https://stackoverflow.com/questions/37228285/uint8array-to-arraybuffer
return array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset)
}
function vai() {
source = document.getElementById("encoded").value;
console.log("Encoded source=",source);
// Decode BASE45:
decoded = decode(source).enc;
// Convert in string for debug:
converted = convert(decoded);
document.getElementById("decoded").value = converted;
console.log("Decoded=",decoded);
console.log("converted=",converted);
// Unzip the decoded:
COSEbin = pako.inflate(decode(source).raw);
console.log("UNZbin:",COSEbin);
COSE = buf2hex(COSEbin);
console.log("UNZhex:",COSE);
var typedArray = new Uint8Array(COSE.match(/[\da-f]{2}/gi).map(function (h) {
return parseInt(h, 16)
})) // https://stackoverflow.com/questions/43131242/how-to-convert-a-hexadecimal-string-of-data-to-an-arraybuffer-in-javascript
var unzipped = typedArray.buffer;
[headers1, headers2, cbor_data, signature] = CBOR.decode(unzipped);
console.log("cbor_data=",cbor_data) ;
//cbor_dataStr = buf2hex(cbor_data);
//console.log("decodedStr=",decodedStr);
//cbor_dataArr=hexStringToArrayBuffer(cbor_dataStr);
cbor_dataArr = typedArrayToBuffer(cbor_data);
//console.log("cbor_dataArr=",cbor_dataArr);
greenpassData = CBOR.decode(cbor_dataArr);
console.log(greenpassData);
}
</script>
</body>
</html>