7 const BASE64_ALPHABET =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
8 const APRMD5_ALPHABET =
'./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
17 public static function hash($mdp, $salt = null) {
20 $salt = substr($salt, 0, 8);
22 $context = $mdp.
'$apr1$'.$salt;
23 $binary = pack(
'H32', md5($mdp.$salt.$mdp));
25 $context .= substr($binary, 0, min(16,
$i));
27 $context .= (
$i & 1) ? chr(0) : $mdp[0];
28 $binary = pack(
'H32', md5($context));
30 $new = (
$i & 1) ? $mdp : $binary;
33 $new .= (
$i & 1) ? $binary : $mdp;
34 $binary = pack(
'H32', md5(
$new));
37 for (
$i = 0;
$i < 5;
$i++) {
41 $hash = $binary[
$i].$binary[$k].$binary[$j].$hash;
43 $hash = chr(0).chr(0).$binary[11].$hash;
45 strrev(substr(base64_encode($hash), 2)),
46 self::BASE64_ALPHABET,
49 return '$apr1$'.$salt.
'$'.$hash;
53 public static function salt() {
54 $alphabet = self::APRMD5_ALPHABET;
57 $offset = hexdec(bin2hex(openssl_random_pseudo_bytes(1))) % 64;
58 $salt .= $alphabet[$offset];
63 public static function check($plain, $hash) {
64 $parts = explode(
'$', $hash);
65 return self::hash($plain, $parts[2]) === $hash;
static hash($mdp, $salt=null)
static check($plain, $hash)
hash(StreamInterface $stream, $algo, $rawOutput=false)
Calculate a hash of a Stream.