On Mon, Dec 16, 2013 at 9:00 AM, Mike <[email protected]> wrote:
>
> > - flexible setting of cipher algorithm (__callStatic)
> > - flexible setting of digest algorithm (__callStatic)
>
> Shudder, I don't like reading that, but I have not looked yet what that
> actually means, what are these {Cipher,Hash}::anything() static
> placeholders for?
>
>
I wanted to implement more ways how to create Cipher or Hash objects. It's
up to the users what they choose.
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... :)
?>
As you can see the static way of creating is sometimes a bit nicer and more
readable. It also allows one expression statements for PHP 5.3 when you
want to retrieve final digest.
The similar API is available for Cipher so you can write something like:
Cipher::aes("cbc", 128)->encode("data", $key, $iv);
There are more examples in
https://github.com/bukka/php-crypto/blob/master/examples/cipher.php
Let me know if you have more questions!
Thanks for the feedback!
Regards
Jakub