Skip to content

Commit

Permalink
Improved SHA-1 performance with Rusha library
Browse files Browse the repository at this point in the history
  • Loading branch information
zhukov committed Oct 23, 2014
1 parent 94238f3 commit 1089af2
Show file tree
Hide file tree
Showing 6 changed files with 417 additions and 16 deletions.
1 change: 1 addition & 0 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@

<script type="text/javascript" src="vendor/jsbn/jsbn_combined.js"></script>
<script type="text/javascript" src="vendor/cryptoJS/crypto.js"></script>
<script type="text/javascript" src="vendor/rusha/rusha.js"></script>
<script type="text/javascript" src="vendor/zlib/gunzip.min.js"></script>
<script type="text/javascript" src="vendor/closure/long.js"></script>

Expand Down
7 changes: 4 additions & 3 deletions app/js/lib/bin_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,10 @@ function uintToInt (val) {
}

function sha1Hash (bytes) {
// console.log('SHA-1 hash start');
var hashBytes = sha1.hash(bytes, true);
// console.log('SHA-1 hash finish');
this.rushaInstance = this.rushaInstance || new Rusha(1024 * 1024);
// console.log(dT(), 'SHA-1 hash start', bytes.byteLength || bytes.length);
var hashBytes = bytesFromArrayBuffer(rushaInstance.rawDigest(bytes).buffer);
// console.log(dT(), 'SHA-1 hash finish');

return hashBytes;
}
Expand Down
3 changes: 2 additions & 1 deletion app/js/lib/crypto_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ importScripts(
'../../vendor/jsbn/jsbn_combined.js',
'../../vendor/leemon_bigint/bigint.js',
'../../vendor/closure/long.js',
'../../vendor/cryptoJS/crypto.js'
'../../vendor/cryptoJS/crypto.js',
'../../vendor/rusha/rusha.js'
);

onmessage = function (e) {
Expand Down
28 changes: 17 additions & 11 deletions app/js/lib/tl_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@ TLSerialization.prototype.getBuffer = function () {
return this.getArray().buffer;
};

TLSerialization.prototype.getBytes = function () {
TLSerialization.prototype.getBytes = function (typed) {
if (typed) {
var resultBuffer = new ArrayBuffer(this.offset);
var resultArray = new Uint8Array(resultBuffer);

resultArray.set(this.byteView.subarray(0, this.offset));

return resultArray;
}

var bytes = [];
for (var i = 0; i < this.offset; i++) {
bytes.push(this.byteView[i]);
Expand Down Expand Up @@ -141,9 +150,8 @@ TLSerialization.prototype.storeString = function (s, field) {
TLSerialization.prototype.storeBytes = function (bytes, field) {
this.debug && console.log('>>>', bytesToHex(bytes), (field || '') + ':bytes');

this.checkLength(bytes.length + 8);

var len = bytes.length;
var len = bytes.byteLength || bytes.length;
this.checkLength(len + 8);
if (len <= 253) {
this.byteView[this.offset++] = len;
} else {
Expand All @@ -152,9 +160,8 @@ TLSerialization.prototype.storeBytes = function (bytes, field) {
this.byteView[this.offset++] = (len & 0xFF00) >> 8;
this.byteView[this.offset++] = (len & 0xFF0000) >> 16;
}
for (var i = 0; i < len; i++) {
this.byteView[this.offset++] = bytes[i];
}
this.byteView.set(bytes, this.offset);
this.offset += len;

// Padding
while (this.offset % 4) {
Expand All @@ -177,14 +184,13 @@ TLSerialization.prototype.storeIntBytes = function (bytes, bits, field) {
};

TLSerialization.prototype.storeRawBytes = function (bytes, field) {
var len = bytes.length;
var len = bytes.byteLength || bytes.length;

this.debug && console.log('>>>', bytesToHex(bytes), (field || ''));
this.checkLength(len);

for (var i = 0; i < len; i++) {
this.byteView[this.offset++] = bytes[i];
}
this.byteView.set(bytes, this.offset);
this.offset += len;
};


Expand Down
Loading

0 comments on commit 1089af2

Please sign in to comment.