1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65:
<?php
namespace CJPGDK\PhpHttpAuth\Hash;
/**
* Provides PHP methods for MD5 hashing
*
* this type of hasing is not supported by real htpasswd authentication
* added as a sample, and because it was need in an other project.
*
* @package CJPGDK
* @subpackage PhpHttpAuth
* @author Christian M. Jensen <cmj@cjpg.dk>
* @version 1.0.0
* @since 1.0.0
*/
trait Md5
{
/**
* format username and password array to a '.htpasswd' line
* @param array|string $user as array ['username' => , 'password'=>], or a user name with passwd
* @param null|string $hashedpwd
* @return string
*/
public function getMd5HtpasswdRow($user, $hashedpwd = null)
{
if (is_array($user)) {
return "{$user['username']}:{$user['password']}";
}
return "{$user}:{$hashedpwd}";
}
/**
* Create a user and password row.
* @param string $user
* @param string $pwd
* @return array ['username' => , 'password'=>]
*/
public function createUserMd5($user, $pwd)
{
return array('username' => $user, 'password' => $this->getMd5Hash($pwd));
}
/**
* Get an md5 hash
* @param string $plainpasswd
* @return string
*/
public function getMd5Hash($plainpasswd)
{
return md5($plainpasswd);
}
/**
* Validate an md5 hash
* @param string $plain
* @param string $hash
* @return boolean
*/
public function matchMd5Hash($plain, $hash)
{
return $this->getMd5Hash($plain) === $hash;
}
}