PageRenderTime 51ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/PasswordLib/Hash/Hash.php

http://github.com/ircmaxell/PHP-PasswordLib
PHP | 360 lines | 258 code | 9 blank | 93 comment | 3 complexity | cee657c2d3b2dea582b432e1991cdf33 MD5 | raw file
  1. <?php
  2. /**
  3. * A hash utility data mapper class
  4. *
  5. * This class's purpose is to store information about hash algorithms that is
  6. * otherwise unavailable during runtime. Some information is available (such
  7. * as the output size), but is included anyway for performance and completeness
  8. * reasons.
  9. *
  10. * PHP version 5.3
  11. *
  12. * @category PHPPasswordLib
  13. * @package Hash
  14. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  15. * @copyright 2011 The Authors
  16. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  17. * @version Build @@version@@
  18. */
  19. namespace PasswordLib\Hash;
  20. /**
  21. * A hash utility data mapper class
  22. *
  23. * This class's purpose is to store information about hash algorithms that is
  24. * otherwise unavailable during runtime. Some information is available (such
  25. * as the output size), but is included anyway for performance and completeness
  26. * reasons.
  27. *
  28. * PHP version 5.3
  29. *
  30. * @category PHPPasswordLib
  31. * @package Hash
  32. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  33. */
  34. class Hash {
  35. /**
  36. * This array contains information about each hash function available to PHP
  37. * at the present time. Block sizes are not available from functions, so they
  38. * must be hard coded.
  39. *
  40. * The "secure" indicates the strength of the hash and whether or not any known
  41. * cryptographic attacks exist for the hash function. This will only apply when
  42. * using the hash functions for situations that require cryptographic strength
  43. * such as message signing. For other uses the insecure ones can have valid
  44. * uses.
  45. *
  46. * @var array An array of information about each supported hash function
  47. */
  48. protected static $hashInfo = array(
  49. 'md2' => array(
  50. 'HashSize' => 128,
  51. 'BlockSize' => 128,
  52. 'secure' => false,
  53. ),
  54. 'md4' => array(
  55. 'HashSize' => 128,
  56. 'BlockSize' => 512,
  57. 'secure' => false,
  58. ),
  59. 'md5' => array(
  60. 'HashSize' => 128,
  61. 'BlockSize' => 512,
  62. 'secure' => false,
  63. ),
  64. 'sha1' => array(
  65. 'HashSize' => 160,
  66. 'BlockSize' => 512,
  67. 'secure' => false,
  68. ),
  69. 'sha224' => array(
  70. 'HashSize' => 224,
  71. 'BlockSize' => 512,
  72. 'secure' => true,
  73. ),
  74. 'sha256' => array(
  75. 'HashSize' => 256,
  76. 'BlockSize' => 512,
  77. 'secure' => true,
  78. ),
  79. 'sha384' => array(
  80. 'HashSize' => 384,
  81. 'BlockSize' => 1024,
  82. 'secure' => true,
  83. ),
  84. 'sha512' => array(
  85. 'HashSize' => 512,
  86. 'BlockSize' => 1024,
  87. 'secure' => true,
  88. ),
  89. 'ripemd128' => array(
  90. 'HashSize' => 128,
  91. 'BlockSize' => 512,
  92. 'secure' => true,
  93. ),
  94. 'ripemd160' => array(
  95. 'HashSize' => 160,
  96. 'BlockSize' => 512,
  97. 'secure' => true,
  98. ),
  99. 'ripemd256' => array(
  100. 'HashSize' => 256,
  101. 'BlockSize' => 512,
  102. 'secure' => true,
  103. ),
  104. 'ripemd320' => array(
  105. 'HashSize' => 320,
  106. 'BlockSize' => 512,
  107. 'secure' => true,
  108. ),
  109. 'whirlpool' => array(
  110. 'HashSize' => 512,
  111. 'BlockSize' => 512,
  112. 'secure' => true,
  113. ),
  114. 'tiger128,3' => array(
  115. 'HashSize' => 128,
  116. 'BlockSize' => 512,
  117. 'secure' => true,
  118. ),
  119. 'tiger160,3' => array(
  120. 'HashSize' => 160,
  121. 'BlockSize' => 512,
  122. 'secure' => true,
  123. ),
  124. 'tiger192,3' => array(
  125. 'HashSize' => 192,
  126. 'BlockSize' => 512,
  127. 'secure' => true,
  128. ),
  129. 'tiger128,4' => array(
  130. 'HashSize' => 128,
  131. 'BlockSize' => 512,
  132. 'secure' => true,
  133. ),
  134. 'tiger160,4' => array(
  135. 'HashSize' => 160,
  136. 'BlockSize' => 512,
  137. 'secure' => true,
  138. ),
  139. 'tiger192,4' => array(
  140. 'HashSize' => 192,
  141. 'BlockSize' => 512,
  142. 'secure' => true,
  143. ),
  144. 'snefru' => array(
  145. 'HashSize' => 256,
  146. 'BlockSize' => 512,
  147. 'secure' => false,
  148. ),
  149. 'snefru256' => array(
  150. 'HashSize' => 256,
  151. 'BlockSize' => 512,
  152. 'secure' => false,
  153. ),
  154. 'gost' => array(
  155. 'HashSize' => 256,
  156. 'BlockSize' => 256,
  157. 'secure' => false,
  158. ),
  159. 'adler32' => array(
  160. 'HashSize' => 32,
  161. 'BlockSize' => 16,
  162. 'secure' => false,
  163. ),
  164. 'crc32' => array(
  165. 'HashSize' => 32,
  166. 'BlockSize' => 32,
  167. 'secure' => false,
  168. ),
  169. 'crc32b' => array(
  170. 'HashSize' => 32,
  171. 'BlockSize' => 32,
  172. 'secure' => false,
  173. ),
  174. 'salsa10' => array(
  175. 'HashSize' => 512,
  176. 'BlockSize' => 512,
  177. 'secure' => true,
  178. ),
  179. 'salsa20' => array(
  180. 'HashSize' => 512,
  181. 'BlockSize' => 512,
  182. 'secure' => true,
  183. ),
  184. 'haval128,3' => array(
  185. 'HashSize' => 128,
  186. 'BlockSize' => 1024,
  187. 'secure' => false,
  188. ),
  189. 'haval160,3' => array(
  190. 'HashSize' => 160,
  191. 'BlockSize' => 1024,
  192. 'secure' => false,
  193. ),
  194. 'haval192,3' => array(
  195. 'HashSize' => 192,
  196. 'BlockSize' => 1024,
  197. 'secure' => false,
  198. ),
  199. 'haval224,3' => array(
  200. 'HashSize' => 224,
  201. 'BlockSize' => 1024,
  202. 'secure' => false,
  203. ),
  204. 'haval256,3' => array(
  205. 'HashSize' => 256,
  206. 'BlockSize' => 1024,
  207. 'secure' => false,
  208. ),
  209. 'haval128,4' => array(
  210. 'HashSize' => 128,
  211. 'BlockSize' => 1024,
  212. 'secure' => false,
  213. ),
  214. 'haval160,4' => array(
  215. 'HashSize' => 160,
  216. 'BlockSize' => 1024,
  217. 'secure' => false,
  218. ),
  219. 'haval192,4' => array(
  220. 'HashSize' => 192,
  221. 'BlockSize' => 1024,
  222. 'secure' => false,
  223. ),
  224. 'haval224,4' => array(
  225. 'HashSize' => 224,
  226. 'BlockSize' => 1024,
  227. 'secure' => false,
  228. ),
  229. 'haval256,4' => array(
  230. 'HashSize' => 256,
  231. 'BlockSize' => 1024,
  232. 'secure' => false,
  233. ),
  234. 'haval128,5' => array(
  235. 'HashSize' => 128,
  236. 'BlockSize' => 1024,
  237. 'secure' => false,
  238. ),
  239. 'haval160,5' => array(
  240. 'HashSize' => 160,
  241. 'BlockSize' => 1024,
  242. 'secure' => false,
  243. ),
  244. 'haval192,5' => array(
  245. 'HashSize' => 192,
  246. 'BlockSize' => 1024,
  247. 'secure' => false,
  248. ),
  249. 'haval224,5' => array(
  250. 'HashSize' => 224,
  251. 'BlockSize' => 1024,
  252. 'secure' => false,
  253. ),
  254. 'haval256,5' => array(
  255. 'HashSize' => 256,
  256. 'BlockSize' => 1024,
  257. 'secure' => false,
  258. ),
  259. 'joaat' => array(
  260. 'HashSize' => 32,
  261. 'BlockSize' => 64,
  262. 'secure' => false,
  263. ),
  264. 'fnv132' => array(
  265. 'HashSize' => 32,
  266. 'BlockSize' => 32,
  267. 'secure' => false,
  268. ),
  269. 'fnv164' => array(
  270. 'HashSize' => 64,
  271. 'BlockSize' => 64,
  272. 'secure' => false,
  273. ),
  274. );
  275. /**
  276. * Get the block size of the specified function in bytes
  277. *
  278. * @param string $hash The hash function to look up
  279. *
  280. * @return int The number of bytes in the block function
  281. */
  282. public static function getBlockSize($hash) {
  283. return static::getBlockSizeInBits($hash) / 8;
  284. }
  285. /**
  286. * Get the block size of the specified function in bits
  287. *
  288. * @param string $hash The hash function to look up
  289. *
  290. * @return int The number of bits in the block function
  291. */
  292. public static function getBlockSizeInBits($hash) {
  293. if (isset(static::$hashInfo[$hash]['BlockSize'])) {
  294. return static::$hashInfo[$hash]['BlockSize'];
  295. }
  296. return 0;
  297. }
  298. /**
  299. * Get the output size of the specified function in bytes
  300. *
  301. * @param string $hash The hash function to look up
  302. *
  303. * @return int The number of bytes outputted by the hash function
  304. */
  305. public static function getHashSize($hash) {
  306. return static::getHashSizeInBits($hash) / 8;
  307. }
  308. /**
  309. * Get the output size of the specified function in bits
  310. *
  311. * @param string $hash The hash function to look up
  312. *
  313. * @return int The number of bits outputted by the hash function
  314. */
  315. public static function getHashSizeInBits($hash) {
  316. if (isset(static::$hashInfo[$hash]['HashSize'])) {
  317. return static::$hashInfo[$hash]['HashSize'];
  318. }
  319. return 0;
  320. }
  321. /**
  322. * Check to see if the hash function specified is available
  323. *
  324. * @param string $hash The hash function to look up
  325. *
  326. * @return boolean If the hash function is available in this version of PHP
  327. */
  328. public static function isAvailable($hash) {
  329. return in_array($hash, hash_algos());
  330. }
  331. /**
  332. * Check to see if the specified hash function is secure enough for
  333. * cryptographic uses
  334. *
  335. * The "secure" indicates the strength of the hash and whether or not any known
  336. * cryptographic attacks exist for the hash function. This will only apply when
  337. * using the hash functions for situations that require cryptographic strength
  338. * such as message signing. For other uses the insecure ones can have valid
  339. * uses.
  340. *
  341. * @param string $hash The hash function to look up
  342. *
  343. * @return bolean If the function is secure
  344. */
  345. public static function isSecure($hash) {
  346. if (isset(static::$hashInfo[$hash])) {
  347. return static::$hashInfo[$hash]['secure'];
  348. }
  349. return false;
  350. }
  351. }