Skip to content

Instantly share code, notes, and snippets.

@jan-krueger
Last active May 4, 2016 11:17
Show Gist options
  • Save jan-krueger/137a70417d72f2fee09e to your computer and use it in GitHub Desktop.
Save jan-krueger/137a70417d72f2fee09e to your computer and use it in GitHub Desktop.
Simple encryption class.
<?php
namespace SweetCode\Cipher;
class Cipher {
private $secureKey;
private $cryptCipher = MCRYPT_RIJNDAEL_256;
private $cryptMode = MCRYPT_MODE_ECB;
private $vectorSize;
public function __construct($passphrase, $hash = 'SHA256') {
$this->secureKey = hash($hash, $passphrase, true);
$this->vectorSize = mcrypt_get_iv_size($this->cryptCipher, $this->cryptMode);
}
/**
* encrypts the given value
*
* @param string $value
* @return string
*/
public function encrypt($value) {
return (base64_encode(
mcrypt_encrypt(
$this->cryptCipher,
$this->secureKey,
$value,
$this->cryptMode,
mcrypt_create_iv(
$this->vectorSize,
MCRYPT_RAND
)
)));
}
/**
* decrypts the given encrypted value
*
* @param string $value
* @return string
*/
public function decrypt($value) {
return (mcrypt_decrypt(
$this->cryptCipher,
$this->secureKey,
base64_decode($value),
$this->cryptMode,
mcrypt_create_iv(
$this->vectorSize,
MCRYPT_RAND
)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment