soatok / hash-crypt
dev-master
2019-03-29 15:10 UTC
Requires
- php: ^7
- paragonie/constant_time_encoding: ^2
- phpunit/phpunit: ^7|^8
- vimeo/psalm: ^3
This package is auto-updated.
Last update: 2024-08-29 04:31:49 UTC
README
警告! 这是一个实验性设计。绝不要在生产环境中使用。它是为了展示教育目的的一个概念而创建的。只使用libsodium。
将任何任意散列函数转换为AEAD加密的示例。
用法
<?php declare(strict_types=1); use ParagonIE\ConstantTime\Binary; use Soatok\HashCrypt\{ HashCrypt, Key }; $key = Key::generate(); $hashCrypt = new HashCrypt('sha256', $key); $message = 'This is a secret message'; $encrypted = $hashCrypt->encrypt($message); $decrypted = $hashCrypt->decrypt($encrypted); var_dump($encrypted === $decrypted); // bool(true) ### Messages can also have additional authenticated data attached to the ciphertext. ### This is used to calculate tha authentication tag, but is not included in the ### ciphertext message itself. $ciphertext2 = $hashCrypt->encrypt($message, 'additional authenticated data'); var_dump(Binary::safeStrlen($encrypted) === Binary::safeStrlen($ciphertext2)); // bool(true) try { $decrypted = $hashCrypt->decrypt($ciphertext2); } catch (\Soatok\HashCrypt\CryptoException $ex) { // Invalid message authentication code. echo $ex->getMessage(); exit(1); }
常见问题解答
SHA-256 不是加密。SHA-256 是一个散列函数。
我确实构建了一个基于 SHA-256 的 AEAD 加密。
我应该使用这个吗?
不。
你疯了吗?!
嗯,我是一个 兽人...