-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxlsx-encoder.js
145 lines (120 loc) · 4.59 KB
/
xlsx-encoder.js
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
/* global XLSX btoa nacl saveAs Blob zxcvbn surpass */
(function(){
"use strict";
function getArrayBufferFromFile(file) {
return new Promise(function(resolve, reject) {
var reader = new FileReader();
reader.onload = function(e) {
return resolve(new Uint8Array(e.target.result));
};
reader.readAsArrayBuffer(file);
});
}
function xlsxBufferToUglyMemberList(buffer) {
var membershipWorkbook = XLSX.read(buffer, {type: 'array'});
return XLSX.utils.sheet_to_json(
membershipWorkbook.Sheets[membershipWorkbook.SheetNames[0]]);
}
var nameFields = ['first_name', 'middle_name', 'last_name', 'suffix',
'family_first_name', 'family_last_name'];
function monthName(date) {
return ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
][new Date(date || Date.now()).getMonth()];
}
function currentYear(date) {
return new Date().getFullYear().toString();
}
function isoDate(date) {
return new Date(date || Date.now()).toISOString().slice(0,10);
}
function uglyMemberObjectToMeetingMemberObject(ugly) {
var nameParts = [];
// add each present name component, in order
for (var i = 0; i < nameFields.length; ++i) {
if (/\S/.test(ugly[nameFields[i]] || '')) {
nameParts.push(ugly[nameFields[i]].trim());
}
}
return {
id: ugly.AK_id,
name: nameParts.join(' '),
expiry: isoDate(ugly.Xdate)
};
}
function uglyMemberListToMeetingMemberList(uglyList) {
return uglyList.map(uglyMemberObjectToMeetingMemberObject);
}
function xlsxBufferToMeetingMemberList(xlsxBuffer) {
return uglyMemberListToMeetingMemberList(
xlsxBufferToUglyMemberList(xlsxBuffer));
}
// remorselessly copied from https://github.com/dchest/tweetnacl-util-js/blob/master/nacl-util.js
function utf8StringToArrayBuffer (s) {
if (typeof s !== 'string') throw new TypeError('expected string');
var i, d = unescape(encodeURIComponent(s)), b = new Uint8Array(d.length);
for (i = 0; i < d.length; i++) b[i] = d.charCodeAt(i);
return b;
};
function objectToJsonArrayBuffer(obj) {
return utf8StringToArrayBuffer(JSON.stringify(obj));
}
var formElement = document.getElementById('form');
var eventNameInput = document.getElementById('event-name');
var xlsxFileInput = document.getElementById('xlsx-file');
var passphraseInput = document.getElementById('passphrase');
var strengthBar = document.getElementById('strength');
var downloadButton = document.getElementById('dl-button');
eventNameInput.value = monthName() + ' ' + currentYear() + ' General Meeting';
surpass(passphraseInput, {double: true});
var strengthBarBaseClass = "";
var strengthBarAdequateClass = "success";
var strengthBarInadequateClass = "error";
// min log10 guesses necessary to require "centuries" for a fast parallel hash
var adequateZxcvbnScore = 20;
strengthBar.max = adequateZxcvbnScore;
function updateButtonState() {
var zxcvbnScore = zxcvbn(passphraseInput.value).guesses_log10;
var passphraseIsAdequate = zxcvbnScore >= adequateZxcvbnScore;
strengthBar.value = zxcvbnScore;
strengthBar.className =
strengthBarBaseClass + (passphraseIsAdequate ?
strengthBarAdequateClass : strengthBarInadequateClass);
downloadButton.disabled = !(
xlsxFileInput.files[0] && passphraseInput.value && passphraseIsAdequate);
}
function createEventExportForMemberList(memberList) {
return {
name: eventNameInput.value,
created: new Date().toISOString(),
members: memberList
};
}
function defaultDatabaseFilename() {
return eventNameInput.value.toLowerCase()
.replace(/\s+/g,'-').replace(/\W+/g,'') + '_memberlist.json.sbox';
}
function downloadEncryptedDatabase() {
getArrayBufferFromFile(xlsxFileInput.files[0])
.then(xlsxBufferToMeetingMemberList)
.then(createEventExportForMemberList)
.then(objectToJsonArrayBuffer)
.then(function(memberListBuffer) {
var passphraseHashData = nacl.hash(
utf8StringToArrayBuffer(passphraseInput.value));
var secretKey = passphraseHashData.slice(0, nacl.secretbox.keyLength);
var secretNonce = passphraseHashData.slice(nacl.secretbox.keyLength,
nacl.secretbox.keyLength + nacl.secretbox.nonceLength);
var encryptedBlob = new Blob([
nacl.secretbox(memberListBuffer, secretNonce, secretKey)],
{type:'application/octet-stream'});
saveAs(encryptedBlob, defaultDatabaseFilename(), true);
});
}
xlsxFileInput.addEventListener('change', updateButtonState);
passphraseInput.addEventListener('input', updateButtonState);
formElement.addEventListener('submit', function (evt) {
evt.preventDefault();
return downloadEncryptedDatabase();
});
})();