+ Changed the approach to an OOP model

This commit is contained in:
Daniel Legt 2021-02-28 10:58:55 +02:00
parent a1833a238c
commit 5ae3d8294b
1 changed files with 102 additions and 63 deletions

45
lib.php
View File

@ -1,4 +1,37 @@
<?php
/**
* @author Kato Twofold
* @copyright MIT
*
* The class has full support for encryption of strings, provides validation for those
* and makes sure they can be decrypted on the other end, the key is extremely important
* and you MUST keep track of it and not lose it as there is no way of getting it back.
*
*/
class kpcrypt {
// The key to use in the encryption process
private $key = null;
/**
* @param string $key [Optional] The key to use for encryption, if none is mentioned a random one will be generated
*/
public function __construct( string $key = null) {
// If no key is mentioned, generate one
if ( empty($key) ) {
$key = bin2hex(openssl_random_pseudo_bytes("64"));
}
}
/**
* Get the key that the instance is currently using, really useful for when you randomly generate it!
* @return string The key of the instance
*/
public function getKey() {
return $this->key;
}
/**
* @param string $data The data to be encrypted, this can only encrypt strings.
* @param string $key The key to use to encrypt the data, this key should be generated using the openssl_random_pseudo_bytes
@ -6,6 +39,8 @@
* @param bool $integrity_check Check if the encrypted data can also be decypted, this will take 2x more time to process the data, but will make 100% sure that the data is safe and can be decrypted.
*/
function encryptData(string $data, string $key, string $cipherMethod = 'AES-256-CBC', bool $integrity_check = TRUE) {
// Grab the key from self-reference
$key = $this->key;
// Lowercase the cipher
$cipherMethod = strtolower($cipherMethod);
@ -31,7 +66,7 @@ function encryptData( string $data, string $key, string $cipherMethod = 'AES-256
// Check if we should verify the integrity of the encryption
if ($integrity_check === TRUE) {
// Test for decryption validity
if ( md5(decryptData($encryptedData, $key, $cipherMethod)) === md5($data) ) {
if (md5($this->decryptData($encryptedData, $key, $cipherMethod)) === md5($data)) {
// Put the output in the result
return $encryptedData;
} else {
@ -42,7 +77,6 @@ function encryptData( string $data, string $key, string $cipherMethod = 'AES-256
// Simply return the results
return $encryptedData;
}
}
/**
@ -50,7 +84,9 @@ function encryptData( string $data, string $key, string $cipherMethod = 'AES-256
* @param string $key The key to use to encrypt the data, this key should be generated using the openssl_random_pseudo_bytes
* @param string $cipherMethod The cypher method to use in the encryption process, these can be checked using
*/
function decryptData( string $data, string $key, string $cipherMethod = 'AES-256-CBC' ) {
function decryptData(string $data, string $cipherMethod = 'AES-256-CBC') {
// Grab the key from self-reference
$key = $this->key;
// Lowercase the cipher
$cipherMethod = strtolower($cipherMethod);
@ -78,3 +114,6 @@ function decryptData( string $data, string $key, string $cipherMethod = 'AES-256
// Return the data
return $data;
}
}