2017-06-28 18:30:14 +00:00
|
|
|
function arrayToHex(iv) {
|
2017-06-02 03:59:27 +00:00
|
|
|
let hexStr = '';
|
2017-06-09 17:44:12 +00:00
|
|
|
for (const i in iv) {
|
2017-06-02 03:59:27 +00:00
|
|
|
if (iv[i] < 16) {
|
|
|
|
hexStr += '0' + iv[i].toString(16);
|
|
|
|
} else {
|
|
|
|
hexStr += iv[i].toString(16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hexStr;
|
|
|
|
}
|
|
|
|
|
2017-06-28 18:30:14 +00:00
|
|
|
function hexToArray(str) {
|
|
|
|
const iv = new Uint8Array(str.length / 2);
|
2017-06-02 03:59:27 +00:00
|
|
|
for (let i = 0; i < str.length; i += 2) {
|
|
|
|
iv[i / 2] = parseInt(str.charAt(i) + str.charAt(i + 1), 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
return iv;
|
|
|
|
}
|
|
|
|
|
2017-06-21 20:23:36 +00:00
|
|
|
function notify(str) {
|
2017-06-20 19:18:14 +00:00
|
|
|
if (!('Notification' in window)) {
|
2017-06-21 20:23:36 +00:00
|
|
|
return;
|
|
|
|
} else if (Notification.permission === 'granted') {
|
2017-06-21 21:31:21 +00:00
|
|
|
new Notification(str);
|
2017-06-21 20:23:36 +00:00
|
|
|
} else if (Notification.permission !== 'denied') {
|
|
|
|
Notification.requestPermission(function(permission) {
|
2017-06-21 21:31:21 +00:00
|
|
|
if (permission === 'granted') new Notification(str);
|
|
|
|
});
|
2017-06-21 20:23:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-10 20:27:01 +00:00
|
|
|
function gcmCompliant() {
|
|
|
|
try {
|
|
|
|
return window.crypto.subtle.generateKey(
|
|
|
|
{
|
|
|
|
name: 'AES-GCM',
|
|
|
|
length: 128
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
['encrypt', 'decrypt']
|
|
|
|
).then(key => {
|
|
|
|
return window.crypto.subtle.encrypt(
|
|
|
|
{
|
|
|
|
name: 'AES-GCM',
|
|
|
|
iv: window.crypto.getRandomValues(new Uint8Array(12)),
|
2017-07-10 20:48:00 +00:00
|
|
|
additionalData: window.crypto.getRandomValues(new Uint8Array(6)),
|
|
|
|
tagLength: 128
|
2017-07-10 20:27:01 +00:00
|
|
|
},
|
|
|
|
key,
|
|
|
|
new ArrayBuffer(8)
|
|
|
|
)
|
|
|
|
.then(() => {
|
|
|
|
return Promise.resolve()
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
return Promise.reject()
|
|
|
|
})
|
|
|
|
}).catch(err => {
|
|
|
|
return Promise.reject();
|
|
|
|
})
|
|
|
|
} catch(err) {
|
|
|
|
return Promise.reject();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-02 03:59:27 +00:00
|
|
|
module.exports = {
|
2017-06-28 18:30:14 +00:00
|
|
|
arrayToHex,
|
|
|
|
hexToArray,
|
2017-07-10 20:27:01 +00:00
|
|
|
notify,
|
|
|
|
gcmCompliant
|
2017-06-02 03:59:27 +00:00
|
|
|
};
|