<?php
ini_set('display_errors', 1);

if (!class_exists('crm_hardlock')) require_once("common_cadline_libraries/crm_hardlock.class.php");

function generateRandomString($length = 10)
{
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

$privateKey = Webform::GetWebformPrivateKey();
$publicKeyArray = array(
    '3TzHxLcqMsHQ$GRQ2Rotnc',
    '1BnIg9v1b538GTnA7CTMCv',
    '2zAiExEw5VJQRR5b2itYnR',
    '3E9YY6E3pgGOGNEv30UFiF',
    '2lBv1XQeEHJu5ptjgK5iVm',
    '09Xofgf9KGJRzH8SSVUtg3',
    '1l5FZ1g932IOa6a9QPCB6d',
    '15_XLCyNEXIfcq697yRkBF'
);
$length = 20;
// véletlenszerűen választja ki a public key-t plusz az encode-decode method-ot

$datas = array();
for ($i = 1; $i <= $length; $i++) {
    array_push($datas, generateRandomString(200));
}

for ($i = 1; $i <= count($datas); $i++) {
    $encoded = '';
    $decoded = '';
    $publicKeyRand = generateRandomString(10);
    $encodeRand = /*rand(1, 2)*/ 2;
    $decodeRand = rand(1, 2);
    $publicKey = $publicKeyArray[$publicKeyRand];

    switch ($encodeRand) {
        case 1:
            $encoded = Webform::encode($privatekey, $publicKey, $datas[$i]);
            break;

        case 2:
            $encoded = twk_encode($privatekey, $publicKey, $datas[$i]);
            break;

        default:
            // code...
            break;
    }

    $debugString = 'Decode method: ' . $decodeRand . ' || ' . 'Encode method: ' . $encodeRand . ' || Public Key: ' . $publicKeyRand;
    $debugString .= ' || Private Key: ' . $privateKey . ' || Data: ' . $datas[$i] . ' || Encoded: ' . $encoded;

    crm_hardlock::AddToDebugTable($debugString, 'TWK_test');

    switch ($decodeRand) {
        case 1:
            $decoded = Webform::decode($privatekey, $publicKey, $encoded);
            break;

        case 2:
            $decoded = twk_decode($privatekey, $publicKey, $encoded);
            break;

        default:
            // code...
            break;
    }

    if ($decoded != $datas[$i]) {
        echo 'Wrong method';
    }
}

echo 'OK';











die();
/*  OLD METHOD  */
$oldDatas = array();
$startTimeOld = microtime(true);
for ($i = 1; $i <= $length; $i++) {
    array_push($oldDatas, generateRandomString(2000));
}

for ($i = 1; $i <= count($oldDatas); $i++) {
    $encoded = Webform::encode($privatekey, $publicKey, $oldDatas[$i]);
    $decoded = Webform::decode($privatekey, $publicKey, $encoded);

    if ($decoded != $oldDatas[$i]) {
        echo 'Wrong old method<br/>';
        echo 'Input data: ' . $oldDatas[$i] . ' || Decoded data: ' . $decoded;
        die();
    }
}
echo "Old method end:  " . number_format((microtime(true) - $startTimeOld), 4) . " Seconds <br/>";

/*  NEW METHOD  */
$newDatas = array();
$startTimeNew = microtime(true);
for ($i = 1; $i <= $length; $i++) {
    array_push($newDatas, generateRandomString(2000));
}

for ($i = 1; $i <= count($newDatas); $i++) {
    $encoded = twk_encode($privatekey, $publicKey, $newDatas[$i]);
    $decoded = twk_decode($privatekey, $publicKey, $encoded);

    if ($decoded != $newDatas[$i]) {
        echo 'Wrong new method<br/>';
        echo 'Input data: ' . $newDatas[$i] . ' || Decoded data: ' . $decoded;
        die();
    }
}
echo "New method end:  " . number_format((microtime(true) - $startTimeNew), 4) . " Seconds";
