Skip to content

Commit

Permalink
applies base64 encoding directly to the binary data instead of their …
Browse files Browse the repository at this point in the history
…hexadecimal representation
  • Loading branch information
schmittjoh authored and fabpot committed Oct 24, 2010
1 parent 6885f90 commit 3463f47
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
Expand Up @@ -26,7 +26,7 @@ class MessageDigestPasswordEncoder extends BasePasswordEncoder
*
* @param string $algorithm The digest algorithm to use
* @param Boolean $encodeHashAsBase64 Whether to base64 encode the password hash
* @param integer $iterations The number of iterations to use to stretch the password
* @param integer $iterations The number of iterations to use to stretch the password hash
*/
public function __construct($algorithm = 'sha256', $encodeHashAsBase64 = false, $iterations = 1)
{
Expand All @@ -45,14 +45,14 @@ public function encodePassword($raw, $salt)
}

$salted = $this->mergePasswordAndSalt($raw, $salt);
$digest = hash($this->algorithm, $salted);
$digest = hash($this->algorithm, $salted, true);

// "stretch" hash
for ($i = 1; $i < $this->iterations; $i++) {
$digest = hash($this->algorithm, $digest);
$digest = hash($this->algorithm, $digest, true);
}

return $this->encodeHashAsBase64 ? base64_encode($digest) : $digest;
return $this->encodeHashAsBase64 ? base64_encode($digest) : bin2hex($digest);
}

/**
Expand Down
Expand Up @@ -27,10 +27,10 @@ public function testEncodePassword()
$this->assertSame(hash('sha256', 'password'), $encoder->encodePassword('password', ''));

$encoder = new MessageDigestPasswordEncoder('sha256', true);
$this->assertSame(base64_encode(hash('sha256', 'password')), $encoder->encodePassword('password', ''));
$this->assertSame(base64_encode(hash('sha256', 'password', true)), $encoder->encodePassword('password', ''));

$encoder = new MessageDigestPasswordEncoder('sha256', false, 2);
$this->assertSame(hash('sha256', hash('sha256', 'password')), $encoder->encodePassword('password', ''));
$this->assertSame(hash('sha256', hash('sha256', 'password', true)), $encoder->encodePassword('password', ''));
}

/**
Expand Down

0 comments on commit 3463f47

Please sign in to comment.