PABX da Simples IP
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

76 lines
2.4 KiB

<?php
/**
* Description of Cripty
*
* @author Lucas Awade
*/
class Crypt {
private $message;
private $key;
private $option;
private $tag;
private $cipher;
private $ivlen;
private $iv;
private $textcrypt;
const CONF_CIPHER_CRYPT = 'aes-256-cbc';
function __construct($key = null, $cipher = null, $option = OPENSSL_RAW_DATA, $tag = null) {
if (!$cipher) {
$this->cipher = self::CONF_CIPHER_CRYPT;
} else {
$this->cipher = $cipher;
}
$this->key = $key;
$this->option = $option;
$this->tag = $tag;
$this->openssl_crypt();
}
public function encrypt($message = null) {
$this->setMessage($message);
$encrypt = openssl_encrypt($this->message, $this->cipher, $this->key, $this->option, $this->iv);
$hashcode = hash_hmac('sha256', $encrypt, $this->key, true);
$this->textcrypt = base64_encode($this->iv . $hashcode . $encrypt);
return $this->textcrypt;
}
public function decrypt($textcrypt = null) {
$c = base64_decode($textcrypt);
$this->iv = substr($c, 0, $this->ivlen);
$hmac = substr($c, $this->ivlen, 32);
$ciphertext_raw = substr($c, $this->ivlen + 32);
$original_plaintext = openssl_decrypt($ciphertext_raw, $this->cipher, $this->key, $options = OPENSSL_RAW_DATA, $this->iv);
$calcmac = hash_hmac('sha256', $ciphertext_raw, $this->key, true);
if (hash_equals($hmac, $calcmac)) {
return $original_plaintext;
}
}
private function openssl_crypt() {
if (in_array(strtolower($this->cipher), openssl_get_cipher_methods())) {
$this->ivlen = openssl_cipher_iv_length($this->cipher);
$this->iv = openssl_random_pseudo_bytes($this->ivlen);
}
}
public function setKey($key) {
$this->key = $key;
}
public function setOption($option) {
$this->option = $option;
}
public function setTag($tag) {
$this->tag = $tag;
}
public function setMessage($message) {
$this->message = $message;
}
}