/src/lib/SambaDAV/MD5Filter.php

https://github.com/1afa/sambadav · PHP · 52 lines · 28 code · 6 blank · 18 comment · 1 complexity · fa94ec02d9d8f76e189efdf2f091068c MD5 · raw file

  1. <?php // $Format:SambaDAV: commit %h @ %cd$
  2. # Copyright (C) 2013, 2014 Bokxing IT, http://www.bokxing-it.nl
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as
  6. # published by the Free Software Foundation, either version 3 of the
  7. # License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #
  17. # Project page: <https://github.com/1afa/sambadav/>
  18. namespace SambaDAV;
  19. class MD5Filter extends \php_user_filter
  20. {
  21. private $ctx;
  22. public function
  23. onCreate ()
  24. {
  25. $this->ctx = hash_init('md5');
  26. return true;
  27. }
  28. public function
  29. onClose ()
  30. {
  31. // $this->params points to an instance of MD5FilterOutput,
  32. // owned by caller, that caller will use to read out the hash:
  33. $this->params->hash = hash_final($this->ctx);
  34. return true;
  35. }
  36. public function
  37. filter ($in, $out, &$consumed, $closing)
  38. {
  39. while ($bucket = stream_bucket_make_writeable($in)) {
  40. hash_update($this->ctx, $bucket->data);
  41. $consumed += $bucket->datalen;
  42. stream_bucket_append($out, $bucket);
  43. }
  44. return PSFS_PASS_ON;
  45. }
  46. }