Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/Utils/crypt.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php

function cfoGetKey() {
function cfoGetSalt() {
if (defined('SECURE_AUTH_SALT')) {
return SECURE_AUTH_SALT;
}

$key = get_option(GLOBAL_CFO_NAME.'_encrypt_key');
if(!$key) {
$key = openssl_random_pseudo_bytes(10);
Expand All @@ -9,7 +13,11 @@ function cfoGetKey() {
return $key;
}

function cfoGetIV() {
function cfoGetInitVector() {
if (defined('AUTH_SALT')) {
return AUTH_SALT;
}

$iv = get_option(GLOBAL_CFO_NAME.'_encrypt_iv');
if(!$iv) {
$ivlen = openssl_cipher_iv_length('aes-256-cbc');
Expand All @@ -21,16 +29,16 @@ function cfoGetIV() {

function cfoEncryptInput($input) {
$sanitizedInput = sanitize_text_field($input);
$key = cfoGetKey();
$iv = cfoGetIV();
$salt = cfoGetSalt();
$iv = cfoGetInitVector();

$encryptedInput = openssl_encrypt($sanitizedInput, 'aes-256-cbc', $key, 0, $iv);
$encryptedInput = openssl_encrypt($sanitizedInput, 'aes-256-cbc', $salt, 0, $iv);
return $encryptedInput;
}

function cfoDecryptInput($encryptedValue) {
$key = cfoGetKey();
$iv = cfoGetIV();
$decryptedValue = openssl_decrypt($encryptedValue, 'aes-256-cbc', $key, 0, $iv);
$salt = cfoGetSalt();
$iv = cfoGetInitVector();
$decryptedValue = openssl_decrypt($encryptedValue, 'aes-256-cbc', $salt, 0, $iv);
return $decryptedValue;
}