use 128-bit GCM
This commit is contained in:
parent
34c367c49f
commit
4cb34844aa
@ -1,12 +1,12 @@
|
|||||||
const EventEmitter = require('events');
|
const EventEmitter = require('events');
|
||||||
const { strToIv, strToUintArr } = require('./utils');
|
const { hexToArray } = require('./utils');
|
||||||
|
|
||||||
const Raven = window.Raven;
|
const Raven = window.Raven;
|
||||||
|
|
||||||
class FileReceiver extends EventEmitter {
|
class FileReceiver extends EventEmitter {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.salt = strToIv(location.pathname.slice(10, -1));
|
this.salt = hexToArray(location.pathname.slice(10, -1));
|
||||||
}
|
}
|
||||||
|
|
||||||
download() {
|
download() {
|
||||||
@ -55,7 +55,7 @@ class FileReceiver extends EventEmitter {
|
|||||||
{
|
{
|
||||||
kty: 'oct',
|
kty: 'oct',
|
||||||
k: location.hash.slice(1),
|
k: location.hash.slice(1),
|
||||||
alg: 'A256GCM',
|
alg: 'A128GCM',
|
||||||
ext: true
|
ext: true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -66,15 +66,13 @@ class FileReceiver extends EventEmitter {
|
|||||||
)
|
)
|
||||||
]).then(([fdata, key]) => {
|
]).then(([fdata, key]) => {
|
||||||
const salt = this.salt;
|
const salt = this.salt;
|
||||||
console.log(strToUintArr(fdata.aad));
|
|
||||||
|
|
||||||
return Promise.all([
|
return Promise.all([
|
||||||
window.crypto.subtle.decrypt(
|
window.crypto.subtle.decrypt(
|
||||||
{
|
{
|
||||||
name: 'AES-GCM',
|
name: 'AES-GCM',
|
||||||
iv: salt,
|
iv: salt,
|
||||||
tagLength: 128,
|
additionalData: hexToArray(fdata.aad)
|
||||||
additionalData: strToUintArr(fdata.aad)
|
|
||||||
},
|
},
|
||||||
key,
|
key,
|
||||||
fdata.data
|
fdata.data
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
const EventEmitter = require('events');
|
const EventEmitter = require('events');
|
||||||
const { ivToStr } = require('./utils');
|
const { arrayToHex } = require('./utils');
|
||||||
|
|
||||||
const Raven = window.Raven;
|
const Raven = window.Raven;
|
||||||
|
|
||||||
@ -41,8 +41,7 @@ class FileSender extends EventEmitter {
|
|||||||
window.crypto.subtle.generateKey(
|
window.crypto.subtle.generateKey(
|
||||||
{
|
{
|
||||||
name: 'AES-GCM',
|
name: 'AES-GCM',
|
||||||
length: 256,
|
length: 128
|
||||||
tagLength: 128
|
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
['encrypt', 'decrypt']
|
['encrypt', 'decrypt']
|
||||||
@ -61,7 +60,6 @@ class FileSender extends EventEmitter {
|
|||||||
{
|
{
|
||||||
name: 'AES-GCM',
|
name: 'AES-GCM',
|
||||||
iv: this.iv,
|
iv: this.iv,
|
||||||
tagLength: 128,
|
|
||||||
additionalData: this.aad
|
additionalData: this.aad
|
||||||
},
|
},
|
||||||
secretKey,
|
secretKey,
|
||||||
@ -73,13 +71,13 @@ class FileSender extends EventEmitter {
|
|||||||
.then(([encrypted, keydata]) => {
|
.then(([encrypted, keydata]) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const file = this.file;
|
const file = this.file;
|
||||||
const fileId = ivToStr(this.iv);
|
const fileId = arrayToHex(this.iv);
|
||||||
const dataView = new DataView(encrypted);
|
const dataView = new DataView(encrypted);
|
||||||
const blob = new Blob([dataView], { type: file.type });
|
const blob = new Blob([dataView], { type: file.type });
|
||||||
const fd = new FormData();
|
const fd = new FormData();
|
||||||
fd.append('fname', file.name);
|
fd.append('fname', file.name);
|
||||||
fd.append('data', blob, file.name);
|
fd.append('data', blob, file.name);
|
||||||
fd.append('aad', this.aad);
|
fd.append('aad', arrayToHex(this.aad));
|
||||||
|
|
||||||
const xhr = new XMLHttpRequest();
|
const xhr = new XMLHttpRequest();
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
function ivToStr(iv) {
|
function arrayToHex(iv) {
|
||||||
let hexStr = '';
|
let hexStr = '';
|
||||||
for (const i in iv) {
|
for (const i in iv) {
|
||||||
if (iv[i] < 16) {
|
if (iv[i] < 16) {
|
||||||
@ -11,8 +11,8 @@ function ivToStr(iv) {
|
|||||||
return hexStr;
|
return hexStr;
|
||||||
}
|
}
|
||||||
|
|
||||||
function strToIv(str) {
|
function hexToArray(str) {
|
||||||
const iv = new Uint8Array(12);
|
const iv = new Uint8Array(str.length / 2);
|
||||||
for (let i = 0; i < str.length; i += 2) {
|
for (let i = 0; i < str.length; i += 2) {
|
||||||
iv[i / 2] = parseInt(str.charAt(i) + str.charAt(i + 1), 16);
|
iv[i / 2] = parseInt(str.charAt(i) + str.charAt(i + 1), 16);
|
||||||
}
|
}
|
||||||
@ -33,13 +33,8 @@ function notify(str) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function strToUintArr(str) {
|
|
||||||
return new Uint8Array(str.split(",").map(x => parseInt(x)));
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
ivToStr,
|
arrayToHex,
|
||||||
strToIv,
|
hexToArray,
|
||||||
notify,
|
notify
|
||||||
strToUintArr
|
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user