Browse Source

Improved overall binary performance

master
Igor Zhukov 10 years ago
parent
commit
69c0216841
  1. 31
      app/js/lib/bin_utils.js
  2. 12
      app/js/lib/mtproto.js
  3. 2
      app/js/lib/mtproto_wrapper.js

31
app/js/lib/bin_utils.js

@ -112,10 +112,12 @@ function bytesXor (bytes1, bytes2) {
} }
function bytesToWords (bytes) { function bytesToWords (bytes) {
var len = bytes.length, var len = bytes.byteLength || bytes.length,
words = []; words = [], i;
if (bytes instanceof ArrayBuffer) {
for (var i = 0; i < len; i++) { bytes = new Uint8Array(bytes);
}
for (i = 0; i < len; i++) {
words[i >>> 2] |= bytes[i] << (24 - (i % 4) * 8); words[i >>> 2] |= bytes[i] << (24 - (i % 4) * 8);
} }
@ -154,6 +156,16 @@ function bytesToArrayBuffer (b) {
return (new Uint8Array(b)).buffer; return (new Uint8Array(b)).buffer;
} }
function bufferConcat(buffer1, buffer2) {
var l1 = buffer1.byteLength || buffer1.length,
l2 = buffer2.byteLength || buffer2.length;
var tmp = new Uint8Array(l1 + l2);
tmp.set(buffer1 instanceof ArrayBuffer ? new Uint8Array(buffer1) : buffer1, 0);
tmp.set(buffer2 instanceof ArrayBuffer ? new Uint8Array(buffer2) : buffer2, l1);
return tmp.buffer;
}
function bytesFromArrayBuffer (buffer) { function bytesFromArrayBuffer (buffer) {
var len = buffer.byteLength, var len = buffer.byteLength,
byteView = new Uint8Array(buffer), byteView = new Uint8Array(buffer),
@ -228,15 +240,20 @@ function rsaEncrypt (publicKey, bytes) {
} }
function aesEncrypt (bytes, keyBytes, ivBytes) { function aesEncrypt (bytes, keyBytes, ivBytes) {
// console.log('AES encrypt start', bytes.length/*, bytesToHex(keyBytes), bytesToHex(ivBytes)*/); var len = bytes.byteLength || bytes.length;
console.log(dT(), 'AES encrypt start', len/*, bytesToHex(keyBytes), bytesToHex(ivBytes)*/);
var needPadding = 16 - (bytes.length % 16); var needPadding = 16 - (len % 16);
if (needPadding > 0 && needPadding < 16) { if (needPadding > 0 && needPadding < 16) {
var padding = new Array(needPadding); var padding = new Array(needPadding);
(new SecureRandom()).nextBytes(padding); (new SecureRandom()).nextBytes(padding);
if (bytes instanceof ArrayBuffer) {
bytes = bufferConcat(bytes, padding);
} else {
bytes = bytes.concat(padding); bytes = bytes.concat(padding);
} }
}
var encryptedWords = CryptoJS.AES.encrypt(bytesToWords(bytes), bytesToWords(keyBytes), { var encryptedWords = CryptoJS.AES.encrypt(bytesToWords(bytes), bytesToWords(keyBytes), {
iv: bytesToWords(ivBytes), iv: bytesToWords(ivBytes),
@ -246,7 +263,7 @@ function aesEncrypt (bytes, keyBytes, ivBytes) {
var encryptedBytes = bytesFromWords(encryptedWords); var encryptedBytes = bytesFromWords(encryptedWords);
// console.log('AES encrypt finish'); console.log(dT(), 'AES encrypt finish');
return encryptedBytes; return encryptedBytes;
} }

12
app/js/lib/mtproto.js

@ -738,7 +738,7 @@ angular.module('izhukov.mtproto', ['izhukov.utils'])
message = { message = {
msg_id: messageID, msg_id: messageID,
seq_no: seqNo, seq_no: seqNo,
body: serializer.getBytes(), body: serializer.getBytes(true),
isAPI: true isAPI: true
}; };
@ -954,7 +954,7 @@ angular.module('izhukov.mtproto', ['izhukov.utils'])
if (!value || value >= currentTime) { if (!value || value >= currentTime) {
if (message = self.sentMessages[messageID]) { if (message = self.sentMessages[messageID]) {
messages.push(message); messages.push(message);
messagesByteLen += message.body.length + 32; messagesByteLen += (message.body.byteLength || message.body.length) + 32;
if (message.isAPI) { if (message.isAPI) {
hasApiCall = true; hasApiCall = true;
} }
@ -1009,7 +1009,7 @@ angular.module('izhukov.mtproto', ['izhukov.utils'])
inner: innerMessages inner: innerMessages
} }
message = angular.extend({body: container.getBytes()}, containerSentMessage); message = angular.extend({body: container.getBytes(true)}, containerSentMessage);
this.sentMessages[message.msg_id] = containerSentMessage; this.sentMessages[message.msg_id] = containerSentMessage;
@ -1076,10 +1076,14 @@ angular.module('izhukov.mtproto', ['izhukov.utils'])
MtpNetworker.prototype.getEncryptedMessage = function (bytes) { MtpNetworker.prototype.getEncryptedMessage = function (bytes) {
var self = this; var self = this;
console.log(dT(), 'Start encrypt', bytes.byteLength);
return CryptoWorker.sha1Hash(bytes).then(function (bytesHash) { return CryptoWorker.sha1Hash(bytes).then(function (bytesHash) {
console.log(dT(), 'after hash');
var msgKey = bytesHash.slice(-16); var msgKey = bytesHash.slice(-16);
return self.getMsgKeyIv(msgKey, true).then(function (keyIv) { return self.getMsgKeyIv(msgKey, true).then(function (keyIv) {
console.log(dT(), 'after msg key iv');
return CryptoWorker.aesEncrypt(bytes, keyIv[0], keyIv[1]).then(function (encryptedBytes) { return CryptoWorker.aesEncrypt(bytes, keyIv[0], keyIv[1]).then(function (encryptedBytes) {
console.log(dT(), 'Finish encrypt');
return { return {
bytes: encryptedBytes, bytes: encryptedBytes,
msgKey: msgKey msgKey: msgKey
@ -1111,7 +1115,7 @@ angular.module('izhukov.mtproto', ['izhukov.utils'])
data.storeInt(message.body.length, 'message_data_length'); data.storeInt(message.body.length, 'message_data_length');
data.storeRawBytes(message.body, 'message_data'); data.storeRawBytes(message.body, 'message_data');
return this.getEncryptedMessage(data.getBytes()).then(function (encryptedResult) { return this.getEncryptedMessage(data.getBuffer()).then(function (encryptedResult) {
// console.log(dT(), 'Got encrypted out message'/*, encryptedResult*/); // console.log(dT(), 'Got encrypted out message'/*, encryptedResult*/);
var request = new TLSerialization({startMaxLength: encryptedResult.bytes.length + 256}); var request = new TLSerialization({startMaxLength: encryptedResult.bytes.length + 256});
request.storeIntBytes(self.authKeyID, 64, 'auth_key_id'); request.storeIntBytes(self.authKeyID, 64, 'auth_key_id');

2
app/js/lib/mtproto_wrapper.js

@ -558,7 +558,7 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
file_id: fileID, file_id: fileID,
file_part: part, file_part: part,
file_total_parts: totalParts, file_total_parts: totalParts,
bytes: bytesFromArrayBuffer(e.target.result) bytes: e.target.result
}, { }, {
startMaxLength: partSize + 256, startMaxLength: partSize + 256,
fileUpload: true fileUpload: true

Loading…
Cancel
Save