PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/main/php/security/checksum/DefaultDigestImpl.class.php

http://github.com/xp-framework/xp-framework
PHP | 75 lines | 33 code | 7 blank | 35 comment | 2 complexity | ca05cb5802ef1a87077298febe7b9356 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /* This class is part of the XP framework
  3. *
  4. * $Id$
  5. */
  6. uses('security.checksum.MessageDigestImpl');
  7. /**
  8. * Default digest implementation. Uses <tt>hash</tt> extension.
  9. *
  10. * @ext hash
  11. * @test xp://net.xp_framework.unittest.security.checksum.MD5DigestTest
  12. * @test xp://net.xp_framework.unittest.security.checksum.SHA1DigestTest
  13. * @test xp://net.xp_framework.unittest.security.checksum.CRC32DigestTest
  14. * @see xp://security.checksum.MessageDigestImpl
  15. * @see xp://security.checksum.MD5
  16. * @see xp://security.checksum.SHA1
  17. * @see xp://security.checksum.CRC32
  18. * @see php://hash_algos
  19. */
  20. class DefaultDigestImpl extends MessageDigestImpl {
  21. protected $handle= NULL;
  22. static function __static() {
  23. $self= new XPClass(__CLASS__);
  24. foreach (hash_algos() as $algo) {
  25. MessageDigest::register($algo, $self);
  26. }
  27. // Overwrite crc32b implementation if a buggy implementation is detected.
  28. // Workaround for http://bugs.php.net/bug.php?id=45028
  29. if ('0a1cb779' === hash('crc32b', 'AAAAAAAA')) {
  30. MessageDigest::register('crc32b', ClassLoader::defineClass(__CLASS__.'·CRC32bDigestImpl', $self->getName(), array(), '{
  31. public function doFinal() {
  32. $n= hexdec(hash_final($this->handle));
  33. return sprintf("%08x", (($n & 0xFF) << 24) + (($n & 0xFF00) << 8) + (($n & 0xFF0000) >> 8) + (($n >> 24) & 0xFF));
  34. }
  35. }'));
  36. }
  37. }
  38. /**
  39. * Initialize this implementation
  40. *
  41. * @param string algo
  42. * @throws lang.IllegalStateException
  43. */
  44. public function __construct($algo) {
  45. if (!($this->handle= hash_init($algo))) {
  46. throw new IllegalStateException('Could not initialize algorithm "'.$algo.'"');
  47. }
  48. }
  49. /**
  50. * Update hash with data
  51. *
  52. * @param string data
  53. */
  54. public function doUpdate($data) {
  55. hash_update($this->handle, $data);
  56. }
  57. /**
  58. * Finalizes digest and returns a checksum object
  59. *
  60. * @return string
  61. */
  62. public function doFinal() {
  63. $final= hash_final($this->handle);
  64. $this->handle= NULL;
  65. return $final;
  66. }
  67. }
  68. ?>