Home > Software design >  How do you output the encryption and decryption time in php
How do you output the encryption and decryption time in php

Time:04-06

I need to output the time taken for the encryption of aes and twofish algorithm on a plaintext in php

i cant find a method to do it.

CodePudding user response:

For example, you can do like this:

function encrypt($plaintext, $password) {
    $method = "AES-256-CBC";
    $key = hash('sha256', $password, true);
    $iv = openssl_random_pseudo_bytes(16);

    $ciphertext = openssl_encrypt($plaintext, $method, $key, OPENSSL_RAW_DATA, $iv);
    $hash = hash_hmac('sha256', $ciphertext . $iv, $key, true);

    return $iv . $hash . $ciphertext;
}

$time = -microtime(true);

for ($i=0; $i < mt_rand(1,100); $i  ) {
    $ct = encrypt("here_string", "here_password");
}

$time  = microtime(true);
echo "Time: ",sprintf('%f', $time),PHP_EOL;
  • Related