hash32.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Implementation of 32-bit hashing functions.
  16. *
  17. * This is a direct port from the Google Java Hash class
  18. *
  19. */
  20. goog.provide('goog.crypt.hash32');
  21. goog.require('goog.crypt');
  22. /**
  23. * Default seed used during hashing, digits of pie.
  24. * See SEED32 in http://go/base.hash.java
  25. * @type {number}
  26. */
  27. goog.crypt.hash32.SEED32 = 314159265;
  28. /**
  29. * Arbitrary constant used during hashing.
  30. * See CONSTANT32 in http://go/base.hash.java
  31. * @type {number}
  32. */
  33. goog.crypt.hash32.CONSTANT32 = -1640531527;
  34. /**
  35. * Hashes a string to a 32-bit value.
  36. * @param {string} str String to hash.
  37. * @return {number} 32-bit hash.
  38. */
  39. goog.crypt.hash32.encodeString = function(str) {
  40. return goog.crypt.hash32.encodeByteArray(goog.crypt.stringToByteArray(str));
  41. };
  42. /**
  43. * Hashes a string to a 32-bit value, converting the string to UTF-8 before
  44. * doing the encoding.
  45. * @param {string} str String to hash.
  46. * @return {number} 32-bit hash.
  47. */
  48. goog.crypt.hash32.encodeStringUtf8 = function(str) {
  49. return goog.crypt.hash32.encodeByteArray(
  50. goog.crypt.stringToUtf8ByteArray(str));
  51. };
  52. /**
  53. * Hashes an integer to a 32-bit value.
  54. * @param {number} value Number to hash.
  55. * @return {number} 32-bit hash.
  56. */
  57. goog.crypt.hash32.encodeInteger = function(value) {
  58. // TODO(user): Does this make sense in JavaScript with doubles? Should we
  59. // force the value to be in the correct range?
  60. return goog.crypt.hash32.mix32_(
  61. {a: value, b: goog.crypt.hash32.CONSTANT32, c: goog.crypt.hash32.SEED32});
  62. };
  63. /**
  64. * Hashes a "byte" array to a 32-bit value using the supplied seed.
  65. * @param {Array<number>} bytes Array of bytes.
  66. * @param {number=} opt_offset The starting position to use for hash
  67. * computation.
  68. * @param {number=} opt_length Number of bytes that are used for hashing.
  69. * @param {number=} opt_seed The seed.
  70. * @return {number} 32-bit hash.
  71. */
  72. goog.crypt.hash32.encodeByteArray = function(
  73. bytes, opt_offset, opt_length, opt_seed) {
  74. var offset = opt_offset || 0;
  75. var length = opt_length || bytes.length;
  76. var seed = opt_seed || goog.crypt.hash32.SEED32;
  77. var mix = {
  78. a: goog.crypt.hash32.CONSTANT32,
  79. b: goog.crypt.hash32.CONSTANT32,
  80. c: seed
  81. };
  82. var keylen;
  83. for (keylen = length; keylen >= 12; keylen -= 12, offset += 12) {
  84. mix.a += goog.crypt.hash32.wordAt_(bytes, offset);
  85. mix.b += goog.crypt.hash32.wordAt_(bytes, offset + 4);
  86. mix.c += goog.crypt.hash32.wordAt_(bytes, offset + 8);
  87. goog.crypt.hash32.mix32_(mix);
  88. }
  89. // Hash any remaining bytes
  90. mix.c += length;
  91. switch (keylen) { // deal with rest. Some cases fall through
  92. case 11:
  93. mix.c += (bytes[offset + 10]) << 24;
  94. case 10:
  95. mix.c += (bytes[offset + 9] & 0xff) << 16;
  96. case 9:
  97. mix.c += (bytes[offset + 8] & 0xff) << 8;
  98. // the first byte of c is reserved for the length
  99. case 8:
  100. mix.b += goog.crypt.hash32.wordAt_(bytes, offset + 4);
  101. mix.a += goog.crypt.hash32.wordAt_(bytes, offset);
  102. break;
  103. case 7:
  104. mix.b += (bytes[offset + 6] & 0xff) << 16;
  105. case 6:
  106. mix.b += (bytes[offset + 5] & 0xff) << 8;
  107. case 5:
  108. mix.b += (bytes[offset + 4] & 0xff);
  109. case 4:
  110. mix.a += goog.crypt.hash32.wordAt_(bytes, offset);
  111. break;
  112. case 3:
  113. mix.a += (bytes[offset + 2] & 0xff) << 16;
  114. case 2:
  115. mix.a += (bytes[offset + 1] & 0xff) << 8;
  116. case 1:
  117. mix.a += (bytes[offset + 0] & 0xff);
  118. // case 0 : nothing left to add
  119. }
  120. return goog.crypt.hash32.mix32_(mix);
  121. };
  122. /**
  123. * Performs an inplace mix of an object with the integer properties (a, b, c)
  124. * and returns the final value of c.
  125. * @param {Object} mix Object with properties, a, b, and c.
  126. * @return {number} The end c-value for the mixing.
  127. * @private
  128. */
  129. goog.crypt.hash32.mix32_ = function(mix) {
  130. var a = mix.a, b = mix.b, c = mix.c;
  131. a -= b;
  132. a -= c;
  133. a ^= c >>> 13;
  134. b -= c;
  135. b -= a;
  136. b ^= a << 8;
  137. c -= a;
  138. c -= b;
  139. c ^= b >>> 13;
  140. a -= b;
  141. a -= c;
  142. a ^= c >>> 12;
  143. b -= c;
  144. b -= a;
  145. b ^= a << 16;
  146. c -= a;
  147. c -= b;
  148. c ^= b >>> 5;
  149. a -= b;
  150. a -= c;
  151. a ^= c >>> 3;
  152. b -= c;
  153. b -= a;
  154. b ^= a << 10;
  155. c -= a;
  156. c -= b;
  157. c ^= b >>> 15;
  158. mix.a = a;
  159. mix.b = b;
  160. mix.c = c;
  161. return c;
  162. };
  163. /**
  164. * Returns the word at a given offset. Treating an array of bytes a word at a
  165. * time is far more efficient than byte-by-byte.
  166. * @param {Array<number>} bytes Array of bytes.
  167. * @param {number} offset Offset in the byte array.
  168. * @return {number} Integer value for the word.
  169. * @private
  170. */
  171. goog.crypt.hash32.wordAt_ = function(bytes, offset) {
  172. var a = goog.crypt.hash32.toSigned_(bytes[offset + 0]);
  173. var b = goog.crypt.hash32.toSigned_(bytes[offset + 1]);
  174. var c = goog.crypt.hash32.toSigned_(bytes[offset + 2]);
  175. var d = goog.crypt.hash32.toSigned_(bytes[offset + 3]);
  176. return a + (b << 8) + (c << 16) + (d << 24);
  177. };
  178. /**
  179. * Converts an unsigned "byte" to signed, that is, convert a value in the range
  180. * (0, 2^8-1) to (-2^7, 2^7-1) in order to be compatible with Java's byte type.
  181. * @param {number} n Unsigned "byte" value.
  182. * @return {number} Signed "byte" value.
  183. * @private
  184. */
  185. goog.crypt.hash32.toSigned_ = function(n) {
  186. return n > 127 ? n - 256 : n;
  187. };