mako\crypto\Mcrypt


Description


Mcrypt cryptography adapter.


Class methods


Toggle source

public __construct($config)


Constructor.


Parameters

Type Description
array Configuration
Return value

NULL

public function __construct(array $config)
{
	if(extension_loaded('mcrypt') === false)
	{
		throw new RuntimeException(vsprintf("%s(): Mcrypt is not available.", array(__METHOD__)));
	}
	
	$maxSize = mcrypt_get_key_size($config['cipher'], $config['mode']);
	
	if(mb_strlen($config['key']) > $maxSize)
	{
		$config['key'] = substr($config['key'], 0, $maxSize);
	}
	
	$this->cipher = $config['cipher'];
	$this->key    = $config['key'];
	$this->mode   = $config['mode'];
	$this->ivSize = mcrypt_get_iv_size($this->cipher, $this->mode);
}

Toggle source

public encrypt($string)


Encrypts data.


Parameters

Type Description
string String to encrypt
Return value

string

public function encrypt($string)
{
	$iv = mcrypt_create_iv($this->ivSize, MCRYPT_DEV_URANDOM);
	
	return base64_encode($iv . mcrypt_encrypt($this->cipher, $this->key, $string, $this->mode, $iv));
}

Toggle source

public decrypt($string)


Decrypts data.


Parameters

Type Description
string String to decrypt
Return value

string

public function decrypt($string)
{
	$string = base64_decode($string, true);
	
	if($string === false)
	{
		return false;
	}
	
	$iv = substr($string, 0, $this->ivSize);
	
	$string = substr($string, $this->ivSize);
	
	return rtrim(mcrypt_decrypt($this->cipher, $this->key, $string, $this->mode, $iv), "\0");
}