des.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * Ported from Flashlight VNC ActionScript implementation:
  3. * http://www.wizhelp.com/flashlight-vnc/
  4. *
  5. * Full attribution follows:
  6. *
  7. * -------------------------------------------------------------------------
  8. *
  9. * This DES class has been extracted from package Acme.Crypto for use in VNC.
  10. * The bytebit[] array has been reversed so that the most significant bit
  11. * in each byte of the key is ignored, not the least significant. Also the
  12. * unnecessary odd parity code has been removed.
  13. *
  14. * These changes are:
  15. * Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
  16. *
  17. * This software is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  20. *
  21. * DesCipher - the DES encryption method
  22. *
  23. * The meat of this code is by Dave Zimmerman <dzimm@widget.com>, and is:
  24. *
  25. * Copyright (c) 1996 Widget Workshop, Inc. All Rights Reserved.
  26. *
  27. * Permission to use, copy, modify, and distribute this software
  28. * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  29. * without fee is hereby granted, provided that this copyright notice is kept
  30. * intact.
  31. *
  32. * WIDGET WORKSHOP MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
  33. * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  34. * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  35. * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. WIDGET WORKSHOP SHALL NOT BE LIABLE
  36. * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  37. * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  38. *
  39. * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  40. * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  41. * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  42. * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  43. * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  44. * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  45. * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). WIDGET WORKSHOP
  46. * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  47. * HIGH RISK ACTIVITIES.
  48. *
  49. *
  50. * The rest is:
  51. *
  52. * Copyright (C) 1996 by Jef Poskanzer <jef@acme.com>. All rights reserved.
  53. *
  54. * Redistribution and use in source and binary forms, with or without
  55. * modification, are permitted provided that the following conditions
  56. * are met:
  57. * 1. Redistributions of source code must retain the above copyright
  58. * notice, this list of conditions and the following disclaimer.
  59. * 2. Redistributions in binary form must reproduce the above copyright
  60. * notice, this list of conditions and the following disclaimer in the
  61. * documentation and/or other materials provided with the distribution.
  62. *
  63. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  64. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  65. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  66. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  67. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  68. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  69. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  70. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  71. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  72. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  73. * SUCH DAMAGE.
  74. *
  75. * Visit the ACME Labs Java page for up-to-date versions of this and other
  76. * fine Java utilities: http://www.acme.com/java/
  77. */
  78. DES = {
  79. // Tables, permutations, S-boxes, etc.
  80. bytebit : [0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80],
  81. bigbyte : [ 0x800000, 0x400000, 0x200000, 0x100000,
  82. 0x080000, 0x040000, 0x020000, 0x010000, 0x008000, 0x004000,
  83. 0x002000, 0x001000, 0x000800, 0x000400, 0x000200, 0x000100,
  84. 0x000080, 0x000040, 0x000020, 0x000010, 0x000008, 0x000004,
  85. 0x000002, 0x000001],
  86. pc1 : [ 56, 48, 40, 32,
  87. 24, 16, 8, 0, 57, 49,
  88. 41, 33, 25, 17, 9, 1,
  89. 58, 50, 42, 34, 26, 18,
  90. 10, 2, 59, 51, 43, 35,
  91. 62, 54, 46, 38, 30, 22,
  92. 14, 6, 61, 53, 45, 37,
  93. 29, 21, 13, 5, 60, 52,
  94. 44, 36, 28, 20, 12, 4,
  95. 27, 19, 11, 3 ],
  96. totrot : [ 1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21,
  97. 23, 25, 27, 28],
  98. pc2 : [ 13, 16, 10, 23,
  99. 0, 4, 2, 27, 14, 5,
  100. 20, 9, 22, 18, 11, 3,
  101. 25, 7, 15, 6, 26, 19,
  102. 12, 1, 40, 51, 30, 36,
  103. 46, 54, 29, 39, 50, 44,
  104. 32, 47, 43, 48, 38, 55,
  105. 33, 52, 45, 41, 49, 35,
  106. 28, 31, ],
  107. SP1 : [ 0x01010400, 0x00000000, 0x00010000,
  108. 0x01010404, 0x01010004, 0x00010404, 0x00000004, 0x00010000,
  109. 0x00000400, 0x01010400, 0x01010404, 0x00000400, 0x01000404,
  110. 0x01010004, 0x01000000, 0x00000004, 0x00000404, 0x01000400,
  111. 0x01000400, 0x00010400, 0x00010400, 0x01010000, 0x01010000,
  112. 0x01000404, 0x00010004, 0x01000004, 0x01000004, 0x00010004,
  113. 0x00000000, 0x00000404, 0x00010404, 0x01000000, 0x00010000,
  114. 0x01010404, 0x00000004, 0x01010000, 0x01010400, 0x01000000,
  115. 0x01000000, 0x00000400, 0x01010004, 0x00010000, 0x00010400,
  116. 0x01000004, 0x00000400, 0x00000004, 0x01000404, 0x00010404,
  117. 0x01010404, 0x00010004, 0x01010000, 0x01000404, 0x01000004,
  118. 0x00000404, 0x00010404, 0x01010400, 0x00000404, 0x01000400,
  119. 0x01000400, 0x00000000, 0x00010004, 0x00010400, 0x00000000,
  120. 0x01010004],
  121. SP2 : [ 0x80108020, 0x80008000, 0x00008000,
  122. 0x00108020, 0x00100000, 0x00000020, 0x80100020, 0x80008020,
  123. 0x80000020, 0x80108020, 0x80108000, 0x80000000, 0x80008000,
  124. 0x00100000, 0x00000020, 0x80100020, 0x00108000, 0x00100020,
  125. 0x80008020, 0x00000000, 0x80000000, 0x00008000, 0x00108020,
  126. 0x80100000, 0x00100020, 0x80000020, 0x00000000, 0x00108000,
  127. 0x00008020, 0x80108000, 0x80100000, 0x00008020, 0x00000000,
  128. 0x00108020, 0x80100020, 0x00100000, 0x80008020, 0x80100000,
  129. 0x80108000, 0x00008000, 0x80100000, 0x80008000, 0x00000020,
  130. 0x80108020, 0x00108020, 0x00000020, 0x00008000, 0x80000000,
  131. 0x00008020, 0x80108000, 0x00100000, 0x80000020, 0x00100020,
  132. 0x80008020, 0x80000020, 0x00100020, 0x00108000, 0x00000000,
  133. 0x80008000, 0x00008020, 0x80000000, 0x80100020, 0x80108020,
  134. 0x00108000],
  135. SP3 : [ 0x00000208, 0x08020200, 0x00000000,
  136. 0x08020008, 0x08000200, 0x00000000, 0x00020208, 0x08000200,
  137. 0x00020008, 0x08000008, 0x08000008, 0x00020000, 0x08020208,
  138. 0x00020008, 0x08020000, 0x00000208, 0x08000000, 0x00000008,
  139. 0x08020200, 0x00000200, 0x00020200, 0x08020000, 0x08020008,
  140. 0x00020208, 0x08000208, 0x00020200, 0x00020000, 0x08000208,
  141. 0x00000008, 0x08020208, 0x00000200, 0x08000000, 0x08020200,
  142. 0x08000000, 0x00020008, 0x00000208, 0x00020000, 0x08020200,
  143. 0x08000200, 0x00000000, 0x00000200, 0x00020008, 0x08020208,
  144. 0x08000200, 0x08000008, 0x00000200, 0x00000000, 0x08020008,
  145. 0x08000208, 0x00020000, 0x08000000, 0x08020208, 0x00000008,
  146. 0x00020208, 0x00020200, 0x08000008, 0x08020000, 0x08000208,
  147. 0x00000208, 0x08020000, 0x00020208, 0x00000008, 0x08020008,
  148. 0x00020200],
  149. SP4 : [ 0x00802001, 0x00002081, 0x00002081,
  150. 0x00000080, 0x00802080, 0x00800081, 0x00800001, 0x00002001,
  151. 0x00000000, 0x00802000, 0x00802000, 0x00802081, 0x00000081,
  152. 0x00000000, 0x00800080, 0x00800001, 0x00000001, 0x00002000,
  153. 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002001,
  154. 0x00002080, 0x00800081, 0x00000001, 0x00002080, 0x00800080,
  155. 0x00002000, 0x00802080, 0x00802081, 0x00000081, 0x00800080,
  156. 0x00800001, 0x00802000, 0x00802081, 0x00000081, 0x00000000,
  157. 0x00000000, 0x00802000, 0x00002080, 0x00800080, 0x00800081,
  158. 0x00000001, 0x00802001, 0x00002081, 0x00002081, 0x00000080,
  159. 0x00802081, 0x00000081, 0x00000001, 0x00002000, 0x00800001,
  160. 0x00002001, 0x00802080, 0x00800081, 0x00002001, 0x00002080,
  161. 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002000,
  162. 0x00802080],
  163. SP5 : [ 0x00000100, 0x02080100, 0x02080000,
  164. 0x42000100, 0x00080000, 0x00000100, 0x40000000, 0x02080000,
  165. 0x40080100, 0x00080000, 0x02000100, 0x40080100, 0x42000100,
  166. 0x42080000, 0x00080100, 0x40000000, 0x02000000, 0x40080000,
  167. 0x40080000, 0x00000000, 0x40000100, 0x42080100, 0x42080100,
  168. 0x02000100, 0x42080000, 0x40000100, 0x00000000, 0x42000000,
  169. 0x02080100, 0x02000000, 0x42000000, 0x00080100, 0x00080000,
  170. 0x42000100, 0x00000100, 0x02000000, 0x40000000, 0x02080000,
  171. 0x42000100, 0x40080100, 0x02000100, 0x40000000, 0x42080000,
  172. 0x02080100, 0x40080100, 0x00000100, 0x02000000, 0x42080000,
  173. 0x42080100, 0x00080100, 0x42000000, 0x42080100, 0x02080000,
  174. 0x00000000, 0x40080000, 0x42000000, 0x00080100, 0x02000100,
  175. 0x40000100, 0x00080000, 0x00000000, 0x40080000, 0x02080100,
  176. 0x40000100],
  177. SP6 : [ 0x20000010, 0x20400000, 0x00004000,
  178. 0x20404010, 0x20400000, 0x00000010, 0x20404010, 0x00400000,
  179. 0x20004000, 0x00404010, 0x00400000, 0x20000010, 0x00400010,
  180. 0x20004000, 0x20000000, 0x00004010, 0x00000000, 0x00400010,
  181. 0x20004010, 0x00004000, 0x00404000, 0x20004010, 0x00000010,
  182. 0x20400010, 0x20400010, 0x00000000, 0x00404010, 0x20404000,
  183. 0x00004010, 0x00404000, 0x20404000, 0x20000000, 0x20004000,
  184. 0x00000010, 0x20400010, 0x00404000, 0x20404010, 0x00400000,
  185. 0x00004010, 0x20000010, 0x00400000, 0x20004000, 0x20000000,
  186. 0x00004010, 0x20000010, 0x20404010, 0x00404000, 0x20400000,
  187. 0x00404010, 0x20404000, 0x00000000, 0x20400010, 0x00000010,
  188. 0x00004000, 0x20400000, 0x00404010, 0x00004000, 0x00400010,
  189. 0x20004010, 0x00000000, 0x20404000, 0x20000000, 0x00400010,
  190. 0x20004010],
  191. SP7 : [ 0x00200000, 0x04200002, 0x04000802,
  192. 0x00000000, 0x00000800, 0x04000802, 0x00200802, 0x04200800,
  193. 0x04200802, 0x00200000, 0x00000000, 0x04000002, 0x00000002,
  194. 0x04000000, 0x04200002, 0x00000802, 0x04000800, 0x00200802,
  195. 0x00200002, 0x04000800, 0x04000002, 0x04200000, 0x04200800,
  196. 0x00200002, 0x04200000, 0x00000800, 0x00000802, 0x04200802,
  197. 0x00200800, 0x00000002, 0x04000000, 0x00200800, 0x04000000,
  198. 0x00200800, 0x00200000, 0x04000802, 0x04000802, 0x04200002,
  199. 0x04200002, 0x00000002, 0x00200002, 0x04000000, 0x04000800,
  200. 0x00200000, 0x04200800, 0x00000802, 0x00200802, 0x04200800,
  201. 0x00000802, 0x04000002, 0x04200802, 0x04200000, 0x00200800,
  202. 0x00000000, 0x00000002, 0x04200802, 0x00000000, 0x00200802,
  203. 0x04200000, 0x00000800, 0x04000002, 0x04000800, 0x00000800,
  204. 0x00200002],
  205. SP8 : [ 0x10001040, 0x00001000, 0x00040000,
  206. 0x10041040, 0x10000000, 0x10001040, 0x00000040, 0x10000000,
  207. 0x00040040, 0x10040000, 0x10041040, 0x00041000, 0x10041000,
  208. 0x00041040, 0x00001000, 0x00000040, 0x10040000, 0x10000040,
  209. 0x10001000, 0x00001040, 0x00041000, 0x00040040, 0x10040040,
  210. 0x10041000, 0x00001040, 0x00000000, 0x00000000, 0x10040040,
  211. 0x10000040, 0x10001000, 0x00041040, 0x00040000, 0x00041040,
  212. 0x00040000, 0x10041000, 0x00001000, 0x00000040, 0x10040040,
  213. 0x00001000, 0x00041040, 0x10001000, 0x00000040, 0x10000040,
  214. 0x10040000, 0x10040040, 0x10000000, 0x00040000, 0x10001040,
  215. 0x00000000, 0x10041040, 0x00040040, 0x10000040, 0x10040000,
  216. 0x10001000, 0x10001040, 0x00000000, 0x10041040, 0x00041000,
  217. 0x00041000, 0x00001040, 0x00001040, 0x00040040, 0x10000000,
  218. 0x10041000],
  219. // Key routines.
  220. encryptKeys : [],
  221. decryptKeys : [],
  222. // / Set the key.
  223. setKeys : function(key) {
  224. DES.encryptKeys = new Array(32);
  225. DES.decryptKeys = new Array(32);
  226. DES.deskey(key, true, DES.encryptKeys);
  227. DES.deskey(key, false, DES.decryptKeys);
  228. },
  229. // Turn an 8-byte key into internal keys.
  230. deskey : function(keyBlock, encrypting, KnL) {
  231. var i, j, l, m, n;
  232. var pc1m = new Array(56);
  233. var pcr = new Array(56);
  234. var kn = new Array(32);
  235. for (j = 0; j < 56; ++j) {
  236. l = DES.pc1[j];
  237. m = l & 07;
  238. pc1m[j] = ((keyBlock[l >>> 3] & DES.bytebit[m]) != 0) ? 1: 0;
  239. }
  240. for (i = 0; i < 16; ++i) {
  241. if (encrypting)
  242. m = i << 1;
  243. else
  244. m = (15- i) << 1;
  245. n = m + 1;
  246. kn[m] = kn[n] = 0;
  247. for (j = 0; j < 28; ++j) {
  248. l = j + DES.totrot[i];
  249. if (l < 28)
  250. pcr[j] = pc1m[l];
  251. else
  252. pcr[j] = pc1m[l - 28];
  253. }
  254. for (j = 28; j < 56; ++j) {
  255. l = j + DES.totrot[i];
  256. if (l < 56)
  257. pcr[j] = pc1m[l];
  258. else
  259. pcr[j] = pc1m[l - 28];
  260. }
  261. for (j = 0; j < 24; ++j) {
  262. if (pcr[DES.pc2[j]] != 0)
  263. kn[m] |= DES.bigbyte[j];
  264. if (pcr[DES.pc2[j + 24]] != 0)
  265. kn[n] |= DES.bigbyte[j];
  266. }
  267. }
  268. DES.cookey(kn, KnL);
  269. },
  270. cookey: function(raw, KnL) {
  271. var raw0, raw1;
  272. var rawi, KnLi;
  273. var i;
  274. for (i = 0, rawi = 0, KnLi = 0; i < 16; ++i) {
  275. raw0 = raw[rawi++];
  276. raw1 = raw[rawi++];
  277. KnL[KnLi] = (raw0 & 0x00fc0000) << 6;
  278. KnL[KnLi] |= (raw0 & 0x00000fc0) << 10;
  279. KnL[KnLi] |= (raw1 & 0x00fc0000) >>> 10;
  280. KnL[KnLi] |= (raw1 & 0x00000fc0) >>> 6;
  281. ++KnLi;
  282. KnL[KnLi] = (raw0 & 0x0003f000) << 12;
  283. KnL[KnLi] |= (raw0 & 0x0000003f) << 16;
  284. KnL[KnLi] |= (raw1 & 0x0003f000) >>> 4;
  285. KnL[KnLi] |= (raw1 & 0x0000003f);
  286. ++KnLi;
  287. }
  288. },
  289. // Block encryption routines.
  290. // / Encrypt a block of eight bytes.
  291. encrypt: function(clearText, clearOff, cipherText, cipherOff) {
  292. var tempInts = new Array(12);
  293. DES.squashBytesToInts(clearText, clearOff, tempInts, 0, 2);
  294. DES.des(tempInts, tempInts, DES.encryptKeys);
  295. DES.spreadIntsToBytes(tempInts, 0, cipherText, cipherOff, 2);
  296. },
  297. // / Decrypt a block of eight bytes.
  298. decrypt: function(cipherText, cipherOff, clearText, clearOff) {
  299. var tempInts = new Array(12);
  300. DES.squashBytesToInts(cipherText, cipherOff, tempInts, 0, 2);
  301. DES.des(tempInts, tempInts, DES.decryptKeys);
  302. DES.spreadIntsToBytes(tempInts, 0, clearText, clearOff, 2);
  303. },
  304. // The DES function.
  305. des: function(inInts, outInts, keys) {
  306. var fval, work, right, leftt;
  307. var round;
  308. var keysi = 0;
  309. leftt = inInts[0];
  310. right = inInts[1];
  311. work = ((leftt >>> 4) ^ right) & 0x0f0f0f0f;
  312. right ^= work;
  313. leftt ^= (work << 4);
  314. work = ((leftt >>> 16) ^ right) & 0x0000ffff;
  315. right ^= work;
  316. leftt ^= (work << 16);
  317. work = ((right >>> 2) ^ leftt) & 0x33333333;
  318. leftt ^= work;
  319. right ^= (work << 2);
  320. work = ((right >>> 8) ^ leftt) & 0x00ff00ff;
  321. leftt ^= work;
  322. right ^= (work << 8);
  323. right = (right << 1) | ((right >>> 31) & 1);
  324. work = (leftt ^ right) & 0xaaaaaaaa;
  325. leftt ^= work;
  326. right ^= work;
  327. leftt = (leftt << 1) | ((leftt >>> 31) & 1);
  328. for (round = 0; round < 8; ++round) {
  329. work = (right << 28) | (right >>> 4);
  330. work ^= keys[keysi++];
  331. fval = DES.SP7[work & 0x0000003f];
  332. fval |= DES.SP5[(work >>> 8) & 0x0000003f];
  333. fval |= DES.SP3[(work >>> 16) & 0x0000003f];
  334. fval |= DES.SP1[(work >>> 24) & 0x0000003f];
  335. work = right ^ keys[keysi++];
  336. fval |= DES.SP8[work & 0x0000003f];
  337. fval |= DES.SP6[(work >>> 8) & 0x0000003f];
  338. fval |= DES.SP4[(work >>> 16) & 0x0000003f];
  339. fval |= DES.SP2[(work >>> 24) & 0x0000003f];
  340. leftt ^= fval;
  341. work = (leftt << 28) | (leftt >>> 4);
  342. work ^= keys[keysi++];
  343. fval = DES.SP7[work & 0x0000003f];
  344. fval |= DES.SP5[(work >>> 8) & 0x0000003f];
  345. fval |= DES.SP3[(work >>> 16) & 0x0000003f];
  346. fval |= DES.SP1[(work >>> 24) & 0x0000003f];
  347. work = leftt ^ keys[keysi++];
  348. fval |= DES.SP8[work & 0x0000003f];
  349. fval |= DES.SP6[(work >>> 8) & 0x0000003f];
  350. fval |= DES.SP4[(work >>> 16) & 0x0000003f];
  351. fval |= DES.SP2[(work >>> 24) & 0x0000003f];
  352. right ^= fval;
  353. }
  354. right = (right << 31) | (right >>> 1);
  355. work = (leftt ^ right) & 0xaaaaaaaa;
  356. leftt ^= work;
  357. right ^= work;
  358. leftt = (leftt << 31) | (leftt >>> 1);
  359. work = ((leftt >>> 8) ^ right) & 0x00ff00ff;
  360. right ^= work;
  361. leftt ^= (work << 8);
  362. work = ((leftt >>> 2) ^ right) & 0x33333333;
  363. right ^= work;
  364. leftt ^= (work << 2);
  365. work = ((right >>> 16) ^ leftt) & 0x0000ffff;
  366. leftt ^= work;
  367. right ^= (work << 16);
  368. work = ((right >>> 4) ^ leftt) & 0x0f0f0f0f;
  369. leftt ^= work;
  370. right ^= (work << 4);
  371. outInts[0] = right;
  372. outInts[1] = leftt;
  373. },
  374. // Routines taken from other parts of the Acme utilities.
  375. // / Squash bytes down to ints.
  376. squashBytesToInts: function (inBytes, inOff, outInts, outOff, intLen) {
  377. for (var i = 0; i < intLen; ++i)
  378. outInts[outOff + i] = ((inBytes[inOff + i * 4] & 0xff) << 24)
  379. | ((inBytes[inOff + i * 4+ 1] & 0xff) << 16)
  380. | ((inBytes[inOff + i * 4+ 2] & 0xff) << 8)
  381. | (inBytes[inOff + i * 4+ 3] & 0xff);
  382. },
  383. // / Spread ints into unsigned bytes.
  384. spreadIntsToBytes: function (inInts, inOff, outBytes, outOff, intLen) {
  385. for (var i = 0; i < intLen; ++i) {
  386. outBytes[outOff + i * 4] = (inInts[inOff + i] >>> 24) % 256;
  387. outBytes[outOff + i * 4+ 1] = (inInts[inOff + i] >>> 16) % 256;
  388. outBytes[outOff + i * 4+ 2] = (inInts[inOff + i] >>> 8) % 256;
  389. outBytes[outOff + i * 4+ 3] = (inInts[inOff + i]) % 256;
  390. }
  391. /* Make unsigned */
  392. var idx;
  393. for (var i = 0; i < intLen; ++i) {
  394. for (var j = 0; j < 4; j++) {
  395. idx = outOff + i * 4 + j;
  396. if (outBytes[idx] < 0) {
  397. outBytes[idx] += 256;
  398. }
  399. }
  400. }
  401. }
  402. }