Editing typed arrays This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) function hex(val) { if (val >>> 0 !== val) { return " "; } if (val < 0x10) { return "0" + val.toString(16); } return val.toString(16); } function tohex(array) { var string = ""; for (var i = 0, l = array.length; i < l; i++) { string += hex(array[i]); } return string; } function stringToBuffer(string) { string = unescape(encodeURIComponent(string)); var length = string.length; var buf = new Uint8Array(length); for (var i = 0; i < length; i++) { buf[i] = string.charCodeAt(i); } return buf; }; var sh1 = (function(){ "use strict"; var wordsToHex = function(words) { var hex = [], by, b; for (b = 0; b < words.length * 32; b += 8){ by = (words[b >>> 5] >>> (24 - b % 32)) & 0xFF; hex.push((by >>> 4).toString(16)); hex.push((by & 0xF).toString(16)); }; return hex.join(''); }; // input is a Uint8Array bitstream of the data var sha1 = function(input){ var H = new Uint32Array([0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0]), m = [], l = input.length * 8, w = []; for (var i = 0, b = 0; i < l/8; i++, b += 8) m[b >>> 5] |= input[i] << (24 - b % 32); m[l >> 5] |= 0x80 << (24 - l % 32); m[((l + 64 >>> 9) << 4) + 15] = l; for (var i = 0; i < m.length; i += 16) { var a = H[0], b = H[1], c = H[2], d = H[3], e = H[4]; for (var j = 0; j < 80; j++) { if (j < 16) w[j] = m[i + j]; else { var n = w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16]; w[j] = (n << 1) | (n >>> 31); } var t = ((H[0] << 5) | (H[0] >>> 27)) + H[4] + (w[j] >>> 0) + ( j < 20 ? (H[1] & H[2] | ~H[1] & H[3]) + 0x5a827999 : j < 40 ? (H[1] ^ H[2] ^ H[3]) + 0x6ed9eba1 : j < 60 ? (H[1] & H[2] | H[1] & H[3] | H[2] & H[3]) - 0x70e44324 : (H[1] ^ H[2] ^ H[3]) - 0x359d3e2a); H[4] = H[3]; H[3] = H[2]; H[2] = (H[1] << 30) | (H[1] >>> 2); H[1] = H[0]; H[0] = t; } H[0] += a; H[1] += b; H[2] += c; H[3] += d; H[4] += e; } return new Uint32Array(H.buffer); } return sha1; })(); var sh2 = (function(){ "use strict"; var wordsToHex = function(words) { var hex = [], by, b; for (b = 0; b < words.length * 32; b += 8){ by = (words[b >>> 5] >>> (24 - b % 32)) & 0xFF; hex.push((by >>> 4).toString(16)); hex.push((by & 0xF).toString(16)); }; return hex.join(''); }; // input is a Uint8Array bitstream of the data var sha1 = function(input){ var H0 = 0x67452301, H1 = 0xefcdab89, H2 = 0x98badcfe, H3 = 0x10325476, H4 = 0xc3d2e1f0, m = [], l = input.length * 8, w = []; for (var i = 0, b = 0; i < l/8; i++, b += 8) m[b >>> 5] |= input[i] << (24 - b % 32); m[l >> 5] |= 0x80 << (24 - l % 32); m[((l + 64 >>> 9) << 4) + 15] = l; for (var i = 0; i < m.length; i += 16) { var a = H0, b = H1, c = H2, d = H3, e = H4; for (var j = 0; j < 80; j++) { if (j < 16) w[j] = m[i + j]; else { var n = w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16]; w[j] = (n << 1) | (n >>> 31); } var t = ((H0 << 5) | (H0 >>> 27)) + H4 + (w[j] >>> 0) + ( j < 20 ? (H1 & H2 | ~H1 & H3) + 0x5a827999 : j < 40 ? (H1 ^ H2 ^ H3) + 0x6ed9eba1 : j < 60 ? (H1 & H2 | H1 & H3 | H2 & H3) - 0x70e44324 : (H1 ^ H2 ^ H3) - 0x359d3e2a); H4 = H3; H3 = H2; H2 = (H1 << 30) | (H1 >>> 2); H1 = H0; H0 = t; } H0 += a; H1 += b; H2 += c; H3 += d; H4 += e; } var res = [H0, H1, H2, H3, H4]; return wordsToHex(res); } return sha1; })(); Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code (function (message) { var mess = stringToBuffer(message); mess = sh1(mess); return tohex(mess); })('test'); Test 2 Title Async (check if this is an asynchronous test) Code (function (message) { var mess = stringToBuffer(message); return mess = sh2(mess); })('test');