Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES6: Migrated "assets" #912

Merged
merged 20 commits into from
Mar 22, 2017
3 changes: 3 additions & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ extends:
"eslint:recommended"

globals:
_: true
$: true
amplify: true
ko: true
LRUCache: true
wire: true
z: true

rules:
Expand Down
52 changes: 0 additions & 52 deletions app/script/assets/Asset.coffee

This file was deleted.

65 changes: 65 additions & 0 deletions app/script/assets/Asset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Wire
* Copyright (C) 2016 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*/

'use strict';

window.z = window.z || {};
window.z.assets = z.assets || {};

z.assets.Asset = class Asset {

/*
Construct a new asset for the asset service.

@deprecated
@param {Object} config - Asset configuration
*/
constructor(config) {
this.correlation_id = config.correlation_id || z.util.create_random_uuid();
this.content_type = config.content_type;
this.array_buffer = config.array_buffer;
this.payload = {
conv_id: config.conversation_id,
correlation_id: this.correlation_id,
public: config.public || false,
tag: config.tag || 'medium',
inline: config.inline || false,
nonce: this.correlation_id,
md5: config.md5,
width: config.width,
height: config.height,
original_width: config.original_width || config.width,
original_height: config.original_height || config.width,
native_push: config.native_push || false,
};
}

/*
Create the content disposition header for the asset.
*/
get_content_disposition() {
const payload = ['zasset'];
for (let key in this.payload) {
const value = this.payload[key];
payload.push(`${key}=${value}`);
}
return payload.join(';');
}

};
76 changes: 0 additions & 76 deletions app/script/assets/AssetCrypto.coffee

This file was deleted.

82 changes: 82 additions & 0 deletions app/script/assets/AssetCrypto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Wire
* Copyright (C) 2016 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*/


'use strict';

window.z = window.z || {};
window.z.assets = z.assets || {};

z.assets.AssetCrypto = {

/*
@param {ArrayBuffer} key_bytes - AES key used for encryption
@param {ArrayBuffer} computed_sha256 - SHA-256 checksum of the ciphertext
@param {ArrayBuffer} ciphertext - Encrypted plaintext
*/
encrypt_aes_asset(plaintext) {
const iv = new Uint8Array(16);
let key = null;
let iv_ciphertext = null;
let computed_sha256 = null;

window.crypto.getRandomValues(iv);

return window.crypto.subtle.generateKey({name: 'AES-CBC', length: 256}, true, ['encrypt'])
.then(function(ckey) {
key = ckey;

return window.crypto.subtle.encrypt({name: 'AES-CBC', iv: iv.buffer}, key, plaintext);
}).then(function(ciphertext) {
iv_ciphertext = new Uint8Array(ciphertext.byteLength + iv.byteLength);
iv_ciphertext.set(iv, 0);
iv_ciphertext.set(new Uint8Array(ciphertext), iv.byteLength);

return window.crypto.subtle.digest('SHA-256', iv_ciphertext);
}).then(function(digest) {
computed_sha256 = digest;

return window.crypto.subtle.exportKey('raw', key);
}).then(key_bytes => [key_bytes, computed_sha256, iv_ciphertext.buffer]);
},

/*
@param {ArrayBuffer} key_bytes - AES key used for encryption
@param {ArrayBuffer} computed_sha256 - SHA-256 checksum of the ciphertext
@param {ArrayBuffer} ciphertext - Encrypted plaintext
*/
decrypt_aes_asset(ciphertext, key_bytes, reference_sha256) {
return window.crypto.subtle.digest('SHA-256', ciphertext)
.then(function(computed_sha256) {
const a = new Uint32Array(reference_sha256);
const b = new Uint32Array(computed_sha256);

if ((a.length === b.length) && a.every((x, i) => x === b[i])) {
return window.crypto.subtle.importKey('raw', key_bytes, 'AES-CBC', false, ['decrypt']);
}

throw new Error('Encrypted asset does not match its SHA-256 hash');
}).then(function(key) {
const iv = ciphertext.slice(0, 16);
const img_ciphertext = ciphertext.slice(16);
return window.crypto.subtle.decrypt({name: 'AES-CBC', iv}, key, img_ciphertext);
});
},

};
95 changes: 0 additions & 95 deletions app/script/assets/AssetMetaDataBuilder.coffee

This file was deleted.

Loading