Browse files

Adding a static method to Stream to create a hash of a stream. Deprec…

…ating static method to create md5 of entity body.
  • Loading branch information...
1 parent 332a5e8 commit bd09ab366ad744f60a6c316055fc4101fa0351d2 @mtdowling mtdowling committed Nov 8, 2012
Showing with 11 additions and 25 deletions.
  1. +5 −1 AbstractEntityBodyDecorator.php
  2. +6 −15 EntityBody.php
  3. +0 −9 ReadLimitEntityBody.php
View
6 AbstractEntityBodyDecorator.php
@@ -2,6 +2,8 @@
namespace Guzzle\Http;
+use Guzzle\Stream\Stream;
+
/**
* Abstract decorator used to wrap entity bodies
*/
@@ -101,7 +103,9 @@ public function getContentType()
*/
public function getContentMd5($rawOutput = false, $base64Encode = false)
{
- return $this->body->getContentMd5($rawOutput, $base64Encode);
+ $hash = Stream::getHash($this, 'md5', $rawOutput);
+
+ return $hash && $base64Encode ? base64_encode($hash) : $hash;
}
/**
View
21 EntityBody.php
@@ -153,7 +153,9 @@ public function getContentType()
*/
public function getContentMd5($rawOutput = false, $base64Encode = false)
{
- return self::calculateMd5($this, $rawOutput, $base64Encode);
+ $hash = self::getHash($this, 'md5', $rawOutput);
+
+ return $hash && $base64Encode ? base64_encode($hash) : $hash;
}
/**
@@ -164,23 +166,12 @@ public function getContentMd5($rawOutput = false, $base64Encode = false)
* @param bool $base64Encode Whether or not to base64 encode raw output (only if raw output is true)
*
* @return bool|string Returns an MD5 string on success or FALSE on failure
+ * @deprecated This will be deprecated soon
+ * @codeCoverageIgnore
*/
public static function calculateMd5(EntityBodyInterface $body, $rawOutput = false, $base64Encode = false)
{
- $pos = $body->ftell();
- if (!$body->seek(0)) {
- return false;
- }
-
- $ctx = hash_init('md5');
- while ($data = $body->read(1024)) {
- hash_update($ctx, $data);
- }
-
- $out = hash_final($ctx, (bool) $rawOutput);
- $body->seek($pos);
-
- return ((bool) $base64Encode && (bool) $rawOutput) ? base64_encode($out) : $out;
+ return $body->getContentMd5($rawOutput, $base64Encode);
}
/**
View
9 ReadLimitEntityBody.php
@@ -60,15 +60,6 @@ public function getContentLength()
}
/**
- * Returns the Content-MD5 of the limited subset of data
- * {@inheritdoc}
- */
- public function getContentMd5($rawOutput = false, $base64Encode = false)
- {
- return EntityBody::calculateMd5($this, $rawOutput, $base64Encode);
- }
-
- /**
* Allow for a bounded seek on the read limited entity body
* {@inheritdoc}
*/

0 comments on commit bd09ab3

Please sign in to comment.