JavaScript implementation of the SHA-1


/ Published in: JavaScript
Save to your folder(s)

This is the javascript file for code which implements the Secure Hash Algorithm 1 as defined in FIPS 180-1 published April 17, 1995.


Copy this code and paste it in your HTML
  1. /*
  2.  * The JavaScript implementation of the Secure Hash Algorithm 1
  3.  *
  4.  * Copyright (c) 2008 Takanori Ishikawa <[email protected]>
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  * notice, this list of conditions and the following disclaimer.
  13.  *
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  * notice, this list of conditions and the following disclaimer in the
  16.  * documentation and/or other materials provided with the distribution.
  17.  *
  18.  * 3. Neither the name of the authors nor the names of its contributors
  19.  * may be used to endorse or promote products derived from this
  20.  * software without specific prior written permission.
  21.  *
  22.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  28.  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  31.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33.  */
  34. /**
  35.  * This is the javascript file for code which implements
  36.  * the Secure Hash Algorithm 1 as defined in FIPS 180-1 published April 17, 1995.
  37.  *
  38.  * Author: Takanori Ishikawa <[email protected]>
  39.  * Copyright: Takanori Ishikawa 2008
  40.  * License: BSD License (see above)
  41.  *
  42.  * NOTE:
  43.  * Only 8-bit string is supported, please use encodeURIComponent() function
  44.  * if you want to hash multibyte string.
  45.  *
  46.  * Supported Browsers:
  47.  * [Win] IE 6, Firefox 2
  48.  * [Mac] Safari 3, Firefox 2
  49.  *
  50.  * Usage:
  51.  * var hexdigest = new SHA1("Hello.").hexdigest(); // "9b56d519ccd9e1e5b2a725e186184cdc68de0731"
  52.  *
  53.  * See Also:
  54.  * FIPS 180-1 - Secure Hash Standard
  55.  * http://www.itl.nist.gov/fipspubs/fip180-1.htm
  56.  *
  57.  */
  58.  
  59. var SHA1 = (function(){
  60.  
  61. /**
  62.   * Spec is the BDD style test utilities.
  63.   */
  64. var Spec = {
  65. /** Replace the Spec.describe function with empty function if false. */
  66. enabled: true,
  67.  
  68. /** Indicates whether object 'a' is "equal to" 'b'. */
  69. equals: function(a, b) {
  70. if (a instanceof Array && b instanceof Array) {
  71. if (a.length != b.length) return false;
  72. for (var i = 0; i < a.length; i++) if (!Spec.equals(a[i], b[i])) return false;
  73. return true;
  74. }
  75. if ((a != null && b != null) && (typeof a == "object" && typeof b == "object")) {
  76. for (var i in a) if (!Spec.equals(a[i], b[i])) return false;
  77. return true;
  78. }
  79. return (a == b);
  80. },
  81.  
  82. /** equivalent to xUint's assert */
  83. should: function(expection, message) {
  84. Spec.currentIndicator++;
  85. if (!expection) {
  86. var warning = [
  87. "[Spec failed",
  88. Spec.currentTitle ? " (" + Spec.currentTitle + ")] " : "] ",
  89. (message || (Spec.currentMessage + " " + Spec.currentIndicator) || "")
  90. ].join("");
  91.  
  92. alert(warning);
  93. throw warning;
  94. }
  95. return !!expection;
  96. },
  97.  
  98. /** Write your specification by using describe method. */
  99. describe: function(title, spec) {
  100. Spec.currentTitle = title;
  101. for (var name in spec) {
  102. Spec.currentMessage = name;
  103. Spec.currentIndicator = 0;
  104. spec[name]();
  105. Spec.currentIndicator = null;
  106. }
  107. Spec.currentMessage = Spec.currentTitle = null;
  108. },
  109. Version: "0.1"
  110. };
  111.  
  112. // Other BDD style stuffs.
  113. Spec.should.equal = function(a, b, message) { return Spec.should(Spec.equals(a, b), message); };
  114. Spec.should.not = function(a, message) { return Spec.should(!a, message); };
  115. Spec.should.not.equal = function(a, b, message) { return Spec.should(!Spec.equals(a, b), message); };
  116. if (!Spec.enabled) Spec.describe = function(){};
  117.  
  118.  
  119. // self test
  120. Spec.describe("Spec object", {
  121. "should": function() {
  122. Spec.should(true);
  123. Spec.should(1);
  124. },
  125. "should.not": function() {
  126. Spec.should.not(false);
  127. Spec.should.not(0);
  128. },
  129. "should.equal": function() {
  130. Spec.should.equal(null, null);
  131. Spec.should.equal("", "");
  132. Spec.should.equal(12345, 12345);
  133. Spec.should.equal([0,1,2], [0,1,2]);
  134. Spec.should.equal([0,1,[0,1,2]], [0,1,[0,1,2]]);
  135. Spec.should.equal({}, {});
  136. Spec.should.equal({x:1}, {x:1});
  137. Spec.should.equal({x:[1]}, {x:[1]});
  138. },
  139. "should.not.equal": function() {
  140. Spec.should.not.equal([1,2,3], [1,2,3,4]);
  141. Spec.should.not.equal({x:1}, [1,2,3,4]);
  142. }
  143. });
  144.  
  145.  
  146. // -----------------------------------------------------------
  147. // Utilities
  148. // -----------------------------------------------------------
  149. // int32 -> hexdigits string (e.g. 0x123 -> '00000123')
  150. function strfhex32(i32) {
  151. i32 &= 0xffffffff;
  152. if (i32 < 0) i32 += 0x100000000;
  153. var hex = Number(i32).toString(16);
  154. if (hex.length < 8) hex = "00000000".substr(0, 8 - hex.length) + hex;
  155. return hex;
  156. }
  157. Spec.describe("sha1", {
  158. "strfhex32": function() {
  159. Spec.should.equal(strfhex32(0x0), "00000000");
  160. Spec.should.equal(strfhex32(0x123), "00000123");
  161. Spec.should.equal(strfhex32(0xffffffff), "ffffffff");
  162. }
  163. });
  164.  
  165. // int32 -> string (e.g. 123 -> '00000000 00000000 00000000 01111011')
  166. function strfbits(i32) {
  167. if (typeof arguments.callee.ZERO32 == 'undefined') {
  168. arguments.callee.ZERO32 = new Array(33).join("0");
  169. }
  170.  
  171. var bits = Number(i32).toString(2);
  172. // '0' padding
  173. if (bits.length < 32) bits = arguments.callee.ZERO32.substr(0, 32 - bits.length) + bits;
  174. // split by 8 bits
  175. return bits.replace(/(\d{8})/g, '$1 ')
  176. .replace(/^\s*(.*?)\s*$/, '$1');
  177. }
  178. Spec.describe("sha1", {
  179. "strfbits": function() {
  180. Spec.should.equal(strfbits(0), "00000000 00000000 00000000 00000000");
  181. Spec.should.equal(strfbits(1), "00000000 00000000 00000000 00000001");
  182. Spec.should.equal(strfbits(123), "00000000 00000000 00000000 01111011");
  183. }
  184. });
  185.  
  186.  
  187. // -----------------------------------------------------------
  188. // SHA-1
  189. // -----------------------------------------------------------
  190. // Returns Number(32bit unsigned integer) array size to fit for blocks (512-bit strings)
  191. function padding_size(nbits) {
  192. var n = nbits + 1 + 64
  193. return 512 * Math.ceil(n / 512) / 32;
  194. }
  195. Spec.describe("sha1", {
  196. "padding_size": function() {
  197. Spec.should.equal(padding_size(0), 16);
  198. Spec.should.equal(padding_size(1), 16);
  199. Spec.should.equal(padding_size(512 - 64 - 1), 16);
  200. Spec.should.equal(padding_size(512 - 64), 32);
  201. }
  202. });
  203.  
  204. // 8bit string -> uint32[]
  205. function word_array(m) {
  206. var nchar = m.length;
  207. var size = padding_size(nchar * 8);
  208. var words = new Array(size);
  209. for (var i = 0, j = 0; i < nchar; ) {
  210. words[j++] = ((m.charCodeAt(i++) & 0xff) << 24) |
  211. ((m.charCodeAt(i++) & 0xff) << 16) |
  212. ((m.charCodeAt(i++) & 0xff) << 8) |
  213. ((m.charCodeAt(i++) & 0xff))
  214. }
  215. while (j < size) words[j++] = 0;
  216. return words;
  217. }
  218. Spec.describe("sha1", {
  219. "word_array": function() {
  220. Spec.should.equal(word_array(""), [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
  221. Spec.should.equal(word_array("1234")[0], 0x31323334);
  222. }
  223. });
  224.  
  225. function write_nbits(words, length, nbits) {
  226. if (nbits > 0xffffffff) {
  227. var lo = nbits & 0xffffffff;
  228. if (lo < 0) lo += 0x100000000;
  229. words[length - 1] = lo;
  230. words[length - 2] = (nbits - lo) / 0x100000000;
  231. } else {
  232. words[length - 1] = nbits;
  233. words[length - 2] = 0x0;
  234. }
  235. return words;
  236. }
  237. Spec.describe("sha1", {
  238. "write_nbits": function() {
  239. Spec.should.equal(write_nbits([0, 0], 2, 1), [0, 1]);
  240. Spec.should.equal(write_nbits([0, 0], 2, 0xffffffff), [0, 0xffffffff]);
  241. Spec.should.equal(write_nbits([0, 0], 2, 0x100000000), [1, 0]);
  242. Spec.should.equal(write_nbits([0, 0], 2, 0x1ffffffff), [1, 0xffffffff]);
  243. Spec.should.equal(write_nbits([0, 0], 2, 0x12300000000), [0x123, 0]);
  244. Spec.should.equal(write_nbits([0, 0], 2, 0x123abcdef12), [0x123, 0xabcdef12]);
  245. }
  246. });
  247.  
  248. function padding(words, nbits) {
  249. var i = Math.floor(nbits / 32);
  250.  
  251. words[i] |= (1 << (((i + 1) * 32) - nbits - 1));
  252. write_nbits(words, padding_size(nbits), nbits);
  253. return words;
  254. }
  255.  
  256. function digest(words) {
  257. var i = 0, t = 0;
  258. var H = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0];
  259.  
  260. while (i < words.length) {
  261. var W = new Array(80);
  262.  
  263. // (a)
  264. for (t = 0; t < 16; t++) W[t] = words[i++];
  265.  
  266. // (b)
  267. for (t = 16; t < 80; t++) {
  268. var w = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
  269. W[t] = (w << 1) | (w >>> 31);
  270. }
  271.  
  272. // (c)
  273. var A = H[0], B = H[1], C = H[2], D = H[3], E = H[4];
  274.  
  275. // (d) TEMP = S5(A) + ft(B,C,D) + E + Wt + Kt;
  276. // E = D; D = C; C = S30(B); B = A; A = TEMP;
  277. for (t = 0; t < 80; t++) {
  278. var tmp = ((A << 5) | (A >>> 27)) + E + W[t];
  279.  
  280. if (t >= 0 && t <= 19) tmp += ((B & C) | ((~B) & D)) + 0x5a827999;
  281. else if (t >= 20 && t <= 39) tmp += (B ^ C ^ D) + 0x6ed9eba1;
  282. else if (t >= 40 && t <= 59) tmp += ((B & C) | (B & D) | (C & D)) + 0x8f1bbcdc;
  283. else if (t >= 60 && t <= 79) tmp += (B ^ C ^ D) + 0xca62c1d6;
  284.  
  285. E = D; D = C; C = ((B << 30) | (B >>> 2)); B = A; A = tmp;
  286. }
  287.  
  288. // (e) H0 = H0 + A, H1 = H1 + B, H2 = H2 + C, H3 = H3 + D, H4 = H4 + E.
  289. H[0] = (H[0] + A) & 0xffffffff;
  290. H[1] = (H[1] + B) & 0xffffffff;
  291. H[2] = (H[2] + C) & 0xffffffff;
  292. H[3] = (H[3] + D) & 0xffffffff;
  293. H[4] = (H[4] + E) & 0xffffffff;
  294. if (H[0] < 0) H[0] += 0x100000000;
  295. if (H[1] < 0) H[1] += 0x100000000;
  296. if (H[2] < 0) H[2] += 0x100000000;
  297. if (H[3] < 0) H[3] += 0x100000000;
  298. if (H[4] < 0) H[4] += 0x100000000;
  299. }
  300.  
  301. return H;
  302. }
  303.  
  304. // message: 8bit string
  305. var SHA1 = function(message) {
  306. this.message = message;
  307. }
  308.  
  309. SHA1.prototype = {
  310. digest: function() {
  311. var nbits = this.message.length * 8;
  312. var words = padding(word_array(this.message), nbits);
  313. return digest(words);
  314. },
  315.  
  316. hexdigest: function() {
  317. var digest = this.digest();
  318. for (var i = 0; i < digest.length; i++) digest[i] = strfhex32(digest[i]);
  319. return digest.join("");
  320. }
  321. };
  322.  
  323. Spec.describe("sha1", {
  324. "SHA1#hexdigest": function() {
  325. Spec.should.equal(new SHA1("").hexdigest(), "da39a3ee5e6b4b0d3255bfef95601890afd80709");
  326. Spec.should.equal(new SHA1("1").hexdigest(), "356a192b7913b04c54574d18c28d46e6395428ab");
  327. Spec.should.equal(new SHA1("Hello.").hexdigest(), "9b56d519ccd9e1e5b2a725e186184cdc68de0731");
  328. Spec.should.equal(new SHA1("9b56d519ccd9e1e5b2a725e186184cdc68de0731").hexdigest(), "f042dc98a62cbad68dbe21f11bbc1e9d416d2bf6");
  329. Spec.should.equal(new SHA1("MD5abZRVSXZVRcasdfasdddddddddddddddds+BNRJFSLKJFN+SEONBBJFJXLKCJFSE)RUNVXDLILKVJRN)#NVFJ)WVFWRW#)NVS$Q=$dddddddddddddWV;no9wurJFSE)RUNVXDLILKVJRN)#NVFJ)WVFWRW#)NVS$Q=$dddddddddddddWV;no9wurJFSE)RUNVXDLILKVJRN)#NVFJ)WVFWRW#)NVS$Q=$dddddddddddddWV;no9wurJFSE)RUNVXDLILKVJRN)#NVFJ)WVFWRW#)NVS$Q=$dddddddddddddWV;no9wuraddddddasdfasdfd").hexdigest(), "662dbf4ebc9cdb4224766e87634e5ba9e6de672b");
  330. }
  331. });
  332.  
  333. return SHA1;
  334. })();

URL: http://www.metareal.org/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.