X-Git-Url: https://git.stricted.de/?p=GitHub%2FStricted%2Fspeedport-hybrid-php-api.git;a=blobdiff_plain;f=CryptLib%2FKey%2FDerivation%2FKDF%2FKDF1.php;fp=CryptLib%2FKey%2FDerivation%2FKDF%2FKDF1.php;h=0f454231b5218c72ed260cd91d76eb4d5e82cb97;hp=0000000000000000000000000000000000000000;hb=14d4f286d33b631a93207e4d13086c0a3bc0df12;hpb=c9e082da5cc662b64a74d3770f13d1270068678f diff --git a/CryptLib/Key/Derivation/KDF/KDF1.php b/CryptLib/Key/Derivation/KDF/KDF1.php new file mode 100644 index 0000000..0f45423 --- /dev/null +++ b/CryptLib/Key/Derivation/KDF/KDF1.php @@ -0,0 +1,54 @@ + + * @copyright 2011 The Authors + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @version Build @@version@@ + */ + +namespace CryptLib\Key\Derivation\KDF; + +use CryptLib\Hash\Hash; + +/** + * An implementation of the RFC 10833-2 KDF1 Standard key derivation function + * + * @category PHPCryptLib + * @package Key + * @subpackage Derivation + * @author Anthony Ferrara + */ +class KDF1 + extends \CryptLib\Key\Derivation\AbstractDerivation + implements \CryptLib\Key\Derivation\KDF +{ + + /** + * Derive a key of the specified length based on the inputted secret + * + * @param string $secret The secret to base the key on + * @param int $length The length of the key to derive + * @param string $other Additional data to append to the key + * + * @return string The generated key + */ + public function derive($secret, $length, $other = '') { + $size = Hash::getHashSize($this->hash); + $len = ceil($length / $size); + $res = ''; + for ($i = 0; $i < $len; $i++) { + $tmp = pack('N', $i); + $res .= hash($this->hash, $secret . $tmp . $other, true); + } + return substr($res, 0, $length); + } + +} +