>  It's especially handy for Hash class. See following example (the
> expressions for each example do exactly the same thing)
>
> <?php
> use Crypto\Hash;
>
> // 1. just creating Hash object
> $hash = new Hash('sha256');
> $hash = Hash::sha256();
>
> // 2. creating object and updating context
> $hash = new Hash('sha256', $message);
> $hash = (new Hash('sha256'))->update($message); // PHP 5.4+
> $hash = Hash::sha256($message);
> $hash = Hash::sha256()->update($message);
>
> // 3. crating object, updating context and returning hex digest string
> $digest = (new Hash('sha256', $message))->hexdigest(); // PHP 5.4+
> $digest = (new Hash('sha256'))->update($message); // PHP 5.4+
> $digest = Hash::sha256($message)->hexdigest();
> $digest = Hash::sha256()->update($message)->hexdigest();
>
> // * If you see some similarities with Python hashlib, then you are right!
> I borrowed few ideas... :)
> ?>
>
>
I actually decided not to implement the first case from 2. and 3. example
(new Hash($algorithm, $message)) as the second param is not needed (update
can be used) and I plan to use that param for something else. I also forget
to return hexdigest from one case. :) So the correct example is:

<?php
use Crypto\Hash;

// 1. just creating Hash object
$hash = new Hash('sha256');
$hash = Hash::sha256();

// 2. creating object and updating context
$hash = (new Hash('sha256'))->update($message); // PHP 5.4+
$hash = Hash::sha256($message);
$hash = Hash::sha256()->update($message);

// 3. crating object, updating context and returning hex digest string
$digest = (new Hash('sha256'))->update($message)->hexdigest(); // PHP 5.4+
$digest = Hash::sha256($message)->hexdigest();
$digest = Hash::sha256()->update($message)->hexdigest();
?>

Thanks

Jakub

Reply via email to