PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/hphp/runtime/ext/hash/ext_hash.php

http://github.com/facebook/hiphop-php
PHP | 362 lines | 128 code | 20 blank | 214 comment | 17 complexity | e4fe4b5a7e4f7bfe3db374d1a6ed8ee3 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?hh // partial
  2. /**
  3. * hash() - http://php.net/function.hash
  4. *
  5. * @param string $algo - Name of selected hashing algorithm
  6. * (i.e. "md5", "sha256", "haval160,4", etc..)
  7. * @param string $data - Message to be hashed.
  8. * @param bool $raw_output - When set to TRUE, outputs raw binary data.
  9. * FALSE outputs lowercase hexits.
  10. *
  11. * @return string - The calculated message digest as lowercase hexits
  12. * unless raw_output is set to true in which case the
  13. * raw binary representation of the message digest is
  14. * returned.
  15. * On error, FALSE is returned.
  16. */
  17. <<__Native,__IsFoldable, __Rx>>
  18. function hash(string $algo, string $data,
  19. bool $raw_output = false): mixed;
  20. /**
  21. * hash_algos() - http://php.net/function.hash-algos
  22. *
  23. * @return array - A numerically indexed array containing the list of
  24. * supported hashing algorithms.
  25. */
  26. <<__Native,__IsFoldable, __Rx>>
  27. function hash_algos(): varray<string>;
  28. /**
  29. * hash_file() - http://php.net/function.hash-file
  30. *
  31. * @param string $algo - Name of selected hashing algorithm
  32. * (i.e. "md5", "sha256", "haval160,4", etc..)
  33. * @param string $filename - File whose contents are to be hashed.
  34. * @param bool $raw_output - When set to TRUE, outputs raw binary data.
  35. * FALSE outputs lowercase hexits.
  36. *
  37. * @return string - The calculated message digest as lowercase hexits
  38. * unless raw_output is set to true in which case the
  39. * raw binary representation of the message digest is
  40. * returned.
  41. * On error, FALSE is returned.
  42. */
  43. <<__Native>>
  44. function hash_file(string $algo, string $filename,
  45. bool $raw_output = false): mixed;
  46. /**
  47. * hash_final() - http://php.net/function.hash-final
  48. *
  49. * @param resource $context - Hashing context returned by hash_init().
  50. * @param bool $raw_output - When set to TRUE, outputs raw binary data.
  51. * FALSE outputs lowercase hexits.
  52. *
  53. * @return string - Returns a string containing the calculated message
  54. * digest as lowercase hexits unless raw_output is set
  55. * to true in which case the raw binary representation
  56. * of the message digest is returned.
  57. */
  58. <<__Native>>
  59. function hash_final(resource $context, bool $raw_output = false): mixed;
  60. /**
  61. * hash() - http://php.net/function.hash
  62. *
  63. * @param string $algo - Name of selected hashing algorithm
  64. * (i.e. "md5", "sha256", "haval160,4", etc..)
  65. * @param mixed $data - Message to be hashed. Will be cast to a string
  66. * @param string $key - Shared secret key used for generating the
  67. * HMAC variant of the message digest.
  68. * @param bool $raw_output - When set to TRUE, outputs raw binary data.
  69. * FALSE outputs lowercase hexits.
  70. *
  71. * @return string - The calculated message digest as lowercase hexits
  72. * unless raw_output is set to true in which case the
  73. * raw binary representation of the message digest is
  74. * returned.
  75. * On error, FALSE is returned.
  76. */
  77. <<__IsFoldable>>
  78. function hash_hmac(string $algo,
  79. mixed $data,
  80. string $key,
  81. ?bool $raw_output = false): mixed {
  82. // hash_init() doesn't allow empty keys (for good reason)
  83. // but hash_hmac() needs to support them.
  84. // Rely on the fact that HMAC keys are null padded
  85. // making a key of "\0" equivalent to ""
  86. $ctx = hash_init($algo, HASH_HMAC, ($key !== '') ? $key : "\0");
  87. if (!$ctx) {
  88. return false;
  89. }
  90. hash_update($ctx, (string) $data);
  91. return hash_final($ctx, $raw_output);
  92. }
  93. /**
  94. * hash_hmac_file() - http://php.net/function.hash-hmac-file
  95. *
  96. * @param string $algo - Name of selected hashing algorithm
  97. * (i.e. "md5", "sha256", "haval160,4", etc..)
  98. * @param string $filename - File who's contents are to be hashed.
  99. * @param string $key - Shared secret key used for generating the
  100. * HMAC variant of the message digest.
  101. * @param bool $raw_output - When set to TRUE, outputs raw binary data.
  102. * FALSE outputs lowercase hexits.
  103. *
  104. * @return string - The calculated message digest as lowercase hexits
  105. * unless raw_output is set to true in which case the
  106. * raw binary representation of the message digest is
  107. * returned.
  108. * On error, FALSE is returned.
  109. */
  110. function hash_hmac_file(string $algo,
  111. string $filename,
  112. string $key,
  113. ?bool $raw_output = false): mixed {
  114. // hash_init() doesn't allow empty keys (for good reason)
  115. // but hash_hmac_file() needs to support them.
  116. // Rely on the fact that HMAC keys are null padded
  117. // making a key of "\0" equivalent to ""
  118. $ctx = hash_init($algo, HASH_HMAC, ($key !== '') ? $key : "\0");
  119. if (!$ctx) {
  120. return false;
  121. }
  122. hash_update_file($ctx, $filename);
  123. return hash_final($ctx, $raw_output);
  124. }
  125. /**
  126. * hash_init() - http://php.net/function.hash-init
  127. *
  128. * @param string $algo - Name of selected hashing algorithm
  129. * (i.e. "md5", "sha256", "haval160,4", etc..)
  130. * @param int $options - Optional settings for hash generation,
  131. * currently supports only one option:
  132. * HASH_HMAC. When specified, the key
  133. * must be specified.
  134. * @param string $key - When HASH_HMAC is specified for options,
  135. * a shared secret key to be used with the HMAC
  136. * hashing method must be supplied in this parameter.
  137. *
  138. * @return resrouce - Returns a Hashing Context resource for use with
  139. * hash_update(), hash_update_stream(), hash_update_file(),
  140. * and hash_final().
  141. * Returns FALSE on failure.
  142. */
  143. <<__Native>>
  144. function hash_init(string $algo, int $options = 0,
  145. string $key = ""): mixed;
  146. /**
  147. * hash_update() - http://php.net/function.hash-update
  148. *
  149. * @param resource $context - Hashing context returned by hash_init().
  150. * @param string $data - Message to be included in the hash digest.
  151. *
  152. * @return bool - Returns TRUE on success, FALSE on failure.
  153. */
  154. <<__Native>>
  155. function hash_update(resource $context, string $data): bool;
  156. /**
  157. * hash_update_file() - http://php.net/function.hash-update-file
  158. *
  159. * @param resource $context - Hashing context returned by hash_init().
  160. * @param string $filename - URL describing location of file to be hashed.
  161. * @param resource $stream_context - fopen() steam context.
  162. *
  163. * @return bool - Returns TRUE on success, FALSE on failure
  164. */
  165. function hash_update_file(mixed $context, string $filename,
  166. mixed $stream_context = null): bool {
  167. $fp = fopen($filename, 'r', false, $stream_context);
  168. if (!$fp) {
  169. return false;
  170. }
  171. while (strlen($data = fread($fp, 1024))) {
  172. if (!hash_update($context, $data)) {
  173. return false;
  174. }
  175. }
  176. fclose($fp);
  177. return true;
  178. }
  179. /**
  180. * hash_update_stream - http://php.net/function.hash-update-stream
  181. *
  182. * @param resource $context - Hashing context returned by hash_init().
  183. * @param resource $handle - Open file handle as returned by any
  184. * stream creation function.
  185. * @param int $maxlen - Maximum number of characters to copy
  186. * from handle into the hashing context.
  187. *
  188. * @return int - Actual number of bytes added to the hashing context
  189. * from handle.
  190. */
  191. function hash_update_stream(mixed $context, mixed $handle,
  192. int $maxlen = -1): int {
  193. $didread = 0;
  194. while ($maxlen) {
  195. $chunk = fread($handle, ($maxlen > 0) ? $maxlen : 1024);
  196. $len = strlen($chunk);
  197. if (!$len) {
  198. return $didread;
  199. }
  200. if (!hash_update($context, $chunk)) {
  201. fseek($handle, -$len, SEEK_CUR);
  202. return $didread;
  203. }
  204. $didread += $len;
  205. $maxlen -= $len;
  206. }
  207. return $didread;
  208. }
  209. /**
  210. * hash_copy - Copy hashing context
  211. *
  212. * @param resource $context - Hashing context returned by hash_init().
  213. *
  214. * @return resource - Returns a copy of Hashing Context resource.
  215. */
  216. <<__Native>>
  217. function hash_copy(resource $context): mixed;
  218. /**
  219. * hash_pbkdf2 - http://php.net/function.hash-pbkdf2.php
  220. * RFC 2898 - http://www.ietf.org/rfc/rfc2898.txt
  221. *
  222. * @param string $algo - Name of selected hashing algorithm
  223. * (i.e. "md5", "sha256", "haval160,4", etc..)
  224. * @param string $password - The password to use for the derivation.
  225. * @param string $salt - The salt to use for the derivation. This value
  226. * should be generated randomly.
  227. * @param int $iterations - The number of internal iterations to perform
  228. * for the derivation.
  229. * @param int $length - The length of the output string. If raw_output is
  230. * TRUE this corresponds to the byte-length of the
  231. * derived key, if raw_output is FALSE this
  232. * corresponds to twice the byte-length of the
  233. * derived key (as every byte of the key is returned
  234. * as two hexits).
  235. * If 0 is passed, the entire output of the supplied
  236. * algorithm is used.
  237. * @param bool $raw_output - When set to TRUE, outputs raw binary data.
  238. * FALSE outputs lowercase hexits.
  239. *
  240. * @return string - A string containing the derived key as lowercase hexits
  241. * unless raw_output is set to TRUE in which case the raw
  242. * binary representation of the derived key is returned.
  243. */
  244. <<__IsFoldable>>
  245. function hash_pbkdf2(string $algo, string $password, string $salt,
  246. int $iterations, int $length = 0,
  247. bool $raw_output = false): mixed {
  248. $algo = strtolower($algo);
  249. if (!in_array($algo, hash_algos())) {
  250. error_log("\nWarning: hash_pbkdf2(): Unknown hashing algorithm: ".
  251. $algo);
  252. return false;
  253. }
  254. if ($iterations <= 0) {
  255. error_log("\nWarning: hash_pbkdf2(): Iterations must be a positive".
  256. " integer: ".$iterations);
  257. return false;
  258. }
  259. if ($length < 0) {
  260. error_log("\nWarning: hash_pbkdf2(): Length must be greater than or ".
  261. "equal to 0: ".$length);
  262. return false;
  263. }
  264. $result = "";
  265. $hash_length = strlen(hash($algo, "", true));
  266. if (!$length) {
  267. $length = $hash_length;
  268. if (!$raw_output) {
  269. // It's a bit weird
  270. $length *= 2;
  271. }
  272. }
  273. $key_blocks = ceil($length / $hash_length);
  274. for ($i = 1; $i <= $key_blocks; $i++) {
  275. // Note: $i encoded with most siginificant octet first.
  276. $xor = hash_hmac($algo, $salt.pack("N", $i), $password, true);
  277. $prev = $xor;
  278. for ($j = 1; $j < $iterations; $j++) {
  279. $prev = hash_hmac($algo, $prev, $password, true);
  280. $xor ^= $prev;
  281. }
  282. $result .= $xor;
  283. }
  284. if ($raw_output) {
  285. return substr($result, 0, $length);
  286. } else {
  287. return substr(bin2hex($result), 0, $length);
  288. }
  289. return $result;
  290. }
  291. /**
  292. * hash_equals - Compare two strings in constant time
  293. *
  294. * Note that this function is meant to be slower than a standard
  295. * strcmp which short-circuits the comparison once the result is
  296. * known.
  297. *
  298. * This function is meant to take the same amount of time
  299. * (based on user_string length) regardless of the length or
  300. * contents of known_string.
  301. *
  302. * @param string $known - Fixed string to be compared against
  303. * @param string $user - User-supplied string to check
  304. *
  305. * @return bool - Whether $known == $user
  306. */
  307. // While this function could be marked __IsFoldable, __Rx, doing so would defeat
  308. // the purpose of having a comparison function which takes a fixed amount
  309. // of time.
  310. <<__Native>>
  311. function hash_equals(mixed $known, mixed $user): bool;
  312. /**
  313. * furchash_hphp_ext
  314. *
  315. * @param string $key - The key to hash
  316. * @param int $len - Number of bytes to use from the hash
  317. * @param int $npart - The number of buckets
  318. *
  319. * @return int - A number in the range of 0-(nPart-1)
  320. */
  321. <<__Native, __IsFoldable, __Rx>>
  322. function furchash_hphp_ext(string $key, int $len, int $npart): int;
  323. /**
  324. * furchash_hphp_ext_supported
  325. *
  326. * @return bool - True
  327. */
  328. <<__IsFoldable, __Rx>>
  329. function furchash_hphp_ext_supported(): bool {
  330. return true;
  331. }
  332. /**
  333. * hphp_murmurhash
  334. *
  335. * @param string $key - The key to hash
  336. * @param int $len - Number of bytes to use from the key
  337. * @param int $seed - The seed to use for hashing
  338. *
  339. * @return - The Int64 hash of the first len input characters
  340. */
  341. <<__Native, __IsFoldable, __Rx>>
  342. function hphp_murmurhash(string $key, int $len, int $seed): int;