-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
293 lines (264 loc) · 11.7 KB
/
index.html
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="referrer" content="no-referrer" />
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src-elem 'self' 'unsafe-inline'; script-src 'self' https://cdnjs.cloudflare.com 'unsafe-inline' 'unsafe-eval'; img-src 'self'" >
<meta http-equiv="Permissions-Policy" content="accelerometer=(), ambient-light-sensor=(), autoplay=(), battery=(), camera=(), cross-origin-isolated=(), display-capture=(), document-domain=(), encrypted-media=(), execution-while-not-rendered=(), execution-while-out-of-viewport=(), fullscreen=(), geolocation=(), gyroscope=(), keyboard-map=(), magnetometer=(), microphone=(), midi=(), navigation-override=(), payment=(), picture-in-picture=(), publickey-credentials-get=(), screen-wake-lock=(), sync-xhr=(), usb=(), web-share=(), xr-spatial-tracking=(), clipboard-read=(), clipboard-write=(), gamepad=(), speaker-selection=(), conversion-measurement=(), focus-without-user-activation=(), hid=(), idle-detection=(), interest-cohort=(), serial=(), sync-script=(), trust-token-redemption=(), unload=(), window-placement=(), vertical-scroll=()" >
<title>Psuedo-Anonymised ID Generator</title>
<link rel="icon" type="image/x-icon" href="favicon.png">
<style>
#options, #preview, #loading, #hashing {
display: none;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
footer {
margin-top: 20px;
text-align: center;
}
#privacyNotice {
color: red;
font-weight: bold;
margin-bottom: 10px;
text-align: center;
}
</style>
</head>
<body>
<div id="privacyNotice">
Please NOTE: No data from the CSV file leaves your device. All processing is done locally.
</div>
<h1>Psuedo-Anonymised ID Generator - from .csv</h1>
<form id="myForm">
<input type="file" id="csvFile" accept=".csv" />
<br />
<input type="submit" value="Process" />
</form>
<div id="loading">
<p>Loading data...</p>
</div>
<div id="hashing">
<p>Hashing data...</p>
</div>
<div id="options">
<h1>Field options</h1>
<div id="columnBoxes"></div>
</div>
<div id="preview">
<h1>Data preview</h1>
<p id="previewLbl">Showing 0 of 0</p>
<table id="previewTable">
<thead>
<tr id="previewHeadRow"></tr>
</thead>
<tbody id="previewTableBody"></tbody>
</table>
</div>
<div id="hashInputContainer">
<h2>Hash Input</h2>
<input type="text" id="hashInput" placeholder="Enter text to hash" />
<button onclick="hashInput()">Hash</button>
<p id="hashOutput"></p>
</div>
<footer>
<p>
<a href="https://github.com/cai4cai/sha1inbrowser/blob/main/README.md">Instructions for use</a> |
<a href="https://cai4cai.ml/terms/">Terms of Service</a> |
<a href="https://cai4cai.ml/privacy/">Privacy Policy</a> |
<a href="https://github.com/cai4cai/sha1inbrowser">View Source on GitHub</a>
</p>
</footer>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.9.0/d3.min.js" integrity="sha512-vc58qvvBdrDR4etbxMdlTt4GBQk1qjvyORR2nrsPsFPyrs+/u5c3+1Ct6upOgdZoIl7eq6k3a1UPDSNAQi/32A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
async function sha1(str) {
const enc = new TextEncoder();
const hash = await crypto.subtle.digest('SHA-1', enc.encode(str));
return Array.from(new Uint8Array(hash))
.map(v => v.toString(16).padStart(2, '0'))
.join('').slice(0, 10);
}
async function hashInput() {
const input = document.getElementById("hashInput").value;
const hash = await sha1(input);
document.getElementById("hashOutput").innerText = "Hash: " + hash;
}
</script>
<script>
function download(dataurl, filename) {
const link = document.createElement("a");
link.href = dataurl;
link.download = filename;
link.click();
}
function setOptionsVisible(visible) {
document.getElementById("options").style.display = (visible ? "block" : "none");
}
function setPreviewVisible(visible) {
document.getElementById("preview").style.display = (visible ? "block" : "none");
}
function setLoadingVisible(visible) {
document.getElementById("loading").style.display = (visible ? "block" : "none");
}
function setHashingVisible(visible) {
document.getElementById("hashing").style.display = (visible ? "block" : "none");
}
function clearTable() {
const table = document.getElementById("previewTable");
table.tBodies[0].innerHTML = "";
const headerRow = document.getElementById("previewHeadRow");
headerRow.innerHTML = "";
document.getElementById("columnBoxes").innerHTML = "";
}
</script>
<script>
const myForm = document.getElementById("myForm");
const csvFile = document.getElementById("csvFile");
const table = document.getElementById("previewTable");
const hashColour = "#a3ffff";
const excludeColour = "#ffa6a6";
const stripeWidth = 6;
myForm.addEventListener("submit", function (e) {
e.preventDefault();
const input = csvFile.files[0];
const reader = new FileReader();
reader.onload = async function (e) {
const text = e.target.result;
const data = d3.csvParse(text);
const dataWithHash = d3.csvParse(text);
const toHash = [];
const toHashAndExclude = [];
const toExclude = [];
data.columns.forEach(function(column) {
const value = document.getElementById("select_" + column).value;
switch (value) {
case "Hash":
toHash.push(column);
break;
case "Hash and exclude":
toHashAndExclude.push(column);
break;
case "Exclude":
toExclude.push(column);
break;
case "Keep":
// Keep option, do nothing
break;
}
});
for (let i = 0; i < data.length; i++) {
// Hash and add columns for "Hash" and "Hash and exclude" options
for (let j = 0; j < toHash.length; j++) {
const column = toHash[j];
const hashKey = data[i][column];
const hash = await sha1(hashKey);
data[i][`${column}_hash`] = hash;
dataWithHash[i][`${column}_hash`] = hash;
}
for (let j = 0; j < toHashAndExclude.length; j++) {
const column = toHashAndExclude[j];
const hashKey = data[i][column];
const hash = await sha1(hashKey);
data[i][`${column}_hash`] = hash;
dataWithHash[i][`${column}_hash`] = hash;
}
// Remove columns for "Exclude" and "Hash and exclude" options
toExclude.forEach(function(column) {
delete data[i][column];
});
toHashAndExclude.forEach(function(column) {
delete data[i][column];
});
}
let csvContent = "data:text/csv;charset=utf-8," + encodeURI(d3.csvFormat(data));
let originalCsvWithHash = "data:text/csv;charset=utf-8," + encodeURI(d3.csvFormat(dataWithHash));
download(csvContent, "unidentifiable.csv");
download(originalCsvWithHash, "original_with_hash.csv");
setHashingVisible(false);
};
setHashingVisible(true);
reader.readAsText(input);
});
function updateSelect(e) {
const select = e.target;
let name = select.id.slice(7);
const numCells = table.rows.length;
let value = select.value.toLowerCase();
let toHash = value.includes("hash");
let toExclude = value.includes("exclude");
for (let i = 0; i < numCells; i++) {
let id = `previewCell_${name}_${i}`;
let cell = document.getElementById(id);
let colour1 = toHash ? hashColour : "transparent";
let colour2 = toExclude ? excludeColour : "transparent";
cell.style.backgroundImage = `repeating-linear-gradient(58deg, ${colour1}, ${colour1} ${stripeWidth}px, ${colour2} ${stripeWidth}px, ${colour2} ${stripeWidth * 2}px)`;
}
}
csvFile.addEventListener("change", function (e) {
e.preventDefault();
const input = csvFile.files[0];
const selectsParent = document.getElementById("columnBoxes");
clearTable();
if (input == null) {
selectsParent.innerHTML = "";
setOptionsVisible(false);
setPreviewVisible(false);
return;
}
const reader = new FileReader();
reader.onload = async function (e) {
const text = e.target.result;
const data = d3.csvParse(text);
const previewTableBody = document.getElementById("previewTableBody");
const headerRow = document.getElementById("previewHeadRow");
for (let i = 0; i < data.columns.length; i++) {
let cell = headerRow.insertCell(i);
let column = data.columns[i];
cell.id = `previewCell_${column}_0`;
cell.innerHTML = column;
}
const toShow = Math.min(data.length, 10);
for (let i = 0; i < toShow; i++) {
const row = previewTableBody.insertRow(i);
for (let j = 0; j < data.columns.length; j++) {
let content = data[i][data.columns[j]];
if (content.length > 25) {
content = content.slice(0, 22).trim() + "...";
}
let cell = row.insertCell(j);
cell.id = `previewCell_${data.columns[j]}_${i + 1}`;
cell.innerHTML = content;
}
}
const options = ["Keep", "Exclude", "Hash", "Hash and exclude"];
data.columns.forEach(function(name) {
const label = document.createElement("label");
const id = "select_" + name;
label.for = id;
label.textContent = name + ": ";
selectsParent.appendChild(label);
const select = document.createElement("select");
select.id = id;
select.addEventListener("change", updateSelect)
selectsParent.appendChild(select);
options.forEach(function(optionName) {
const option = document.createElement("option");
option.value = optionName;
option.text = optionName;
select.appendChild(option);
});
selectsParent.appendChild(document.createElement("br"));
});
document.getElementById("previewLbl").innerHTML = "Showing " + toShow + " of " + data.length;
setOptionsVisible(true);
setPreviewVisible(true);
setLoadingVisible(false);
};
setLoadingVisible(true);
reader.readAsText(input);
});
</script>
</body>
</html>