Browse Source

Supported native webCrypto for SHA-1

master
Igor Zhukov 10 years ago
parent
commit
6c56c69fd2
  1. 20
      app/js/lib/bin_utils.js
  2. 20
      app/js/lib/ng_utils.js

20
app/js/lib/bin_utils.js

@ -156,16 +156,6 @@ 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),
@ -178,6 +168,16 @@ function bytesFromArrayBuffer (buffer) {
return bytes; return bytes;
} }
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 longToInts (sLong) { function longToInts (sLong) {
var divRem = bigStringInt(sLong).divideAndRemainder(bigint(0x100000000)); var divRem = bigStringInt(sLong).divideAndRemainder(bigint(0x100000000));

20
app/js/lib/ng_utils.js

@ -486,7 +486,9 @@ angular.module('izhukov.utils', [])
var worker = window.Worker && new Worker('js/lib/crypto_worker.js') || false, var worker = window.Worker && new Worker('js/lib/crypto_worker.js') || false,
taskID = 0, taskID = 0,
awaiting = {}; awaiting = {},
webCrypto = window.crypto && (window.crypto.subtle || window.crypto.webkitSubtle) || window.msCrypto && window.msCrypto.subtle,
useSha1Crypto = webCrypto && webCrypto.digest !== undefined;
if (worker) { if (worker) {
worker.onmessage = function (e) { worker.onmessage = function (e) {
@ -521,6 +523,22 @@ angular.module('izhukov.utils', [])
return { return {
sha1Hash: function (bytes) { sha1Hash: function (bytes) {
if (useSha1Crypto) {
var deferred = $q.defer(),
buffer = bytes instanceof ArrayBuffer
? bytes
: bytesToArrayBuffer(bytes);
webCrypto.digest({name: 'SHA-1'}, buffer).then(function (digest) {
deferred.resolve(bytesFromArrayBuffer(digest));
}, function (e) {
console.error('Crypto digest error', e);
useSha1Crypto = false;
deferred.resolve(sha1Hash(bytes));
});
return deferred.promise;
}
if (worker && false) { // due overhead for data transfer if (worker && false) { // due overhead for data transfer
return performTaskWorker ('sha1-hash', {bytes: bytes}); return performTaskWorker ('sha1-hash', {bytes: bytes});
} }

Loading…
Cancel
Save