PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/xp-framework/xp-framework
PHP | 120 lines | 27 code | 11 blank | 82 comment | 1 complexity | ff4d588d6946f722dac34a557ce52040 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.MessageDigest');
  7. /**
  8. * Checksum
  9. *
  10. * Usage [Getting an MD5 checksum from a string]
  11. * <code>
  12. * $md5= MD5::fromString('Hello world');
  13. * var_dump($md5->getValue());
  14. * </code>
  15. *
  16. * Usage [Getting an SHA1 checksum from a file]
  17. * <code>
  18. * $sha1= SHA1::fromFile(new File('dummy'));
  19. * var_dump($sha1->getValue());
  20. * </code>
  21. *
  22. * Usage [Verifying a CRC32 against a file]
  23. * <code>
  24. * $crc32= new CRC32(1140816021);
  25. * if (!$crc32->verify(CRC32::fromFile(new File('verify.me')))) {
  26. * echo 'Verify failed';
  27. * } else {
  28. * echo 'Verify OK';
  29. * }
  30. * </code>
  31. *
  32. * @purpose Abstract base class to all other checksums
  33. */
  34. class Checksum extends Object {
  35. public $value = '';
  36. /**
  37. * Constructor
  38. *
  39. * @param var value
  40. */
  41. public function __construct($value) {
  42. $this->value= (string)$value;
  43. }
  44. /**
  45. * Create a new checksum from a string. Override this
  46. * method in child classes!
  47. *
  48. * @param string str
  49. * @return security.checksum.Checksum
  50. */
  51. public static function fromString($str) { }
  52. /**
  53. * Returns message digest. Override this method in child classes!
  54. *
  55. * @return security.checksum.MessageDigestImpl
  56. */
  57. public static function digest() { }
  58. /**
  59. * Create a new checksum from a file object. Override this
  60. * method in child classes!
  61. *
  62. * @param io.File file
  63. * @return security.checksum.Checksum
  64. */
  65. public static function fromFile($file) { }
  66. /**
  67. * Retrieve the checksum's value
  68. *
  69. * @return var value
  70. */
  71. public function getValue() {
  72. return $this->value;
  73. }
  74. /**
  75. * Verify this checksum against another checksum
  76. *
  77. * @param security.checksum.Checksum sum
  78. * @return bool TRUE if these checksums match
  79. */
  80. public function verify(self $sum) {
  81. return $this->value === $sum->value;
  82. }
  83. /**
  84. * Check whether another object is equal to this
  85. *
  86. * @param lang.Generic cmp
  87. * @return bool TRUE if these checksums match
  88. */
  89. public function equals($cmp) {
  90. return $cmp instanceof $this && $this->verify($cmp);
  91. }
  92. /**
  93. * Returns a hashcode for this object
  94. *
  95. * @return string
  96. */
  97. public function hashCode() {
  98. return $this->value;
  99. }
  100. /**
  101. * Creates a string representation of this object
  102. *
  103. * @return string
  104. */
  105. public function toString() {
  106. return $this->getClassName().'('.$this->value.')';
  107. }
  108. }
  109. ?>