123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace RNCryptor;
- class VectorBase extends \PHPUnit_Framework_TestCase {
- /**
- * Base directory for the test vector files,
- * relative to __DIR__
- */
- const VECTOR_DIR = '/../../vendor/rncryptor/rncryptor-spec/vectors/CURRENT';
- public function testKdfVectors() {
- $vectors = $this->_getVectors('kdf');
- foreach ($vectors as $vector) {
-
- $cryptor = new Cryptor();
- $key = $cryptor->generateKey(
- $this->_prettyHexToBin($vector['salt_hex']),
- $vector['password'],
- $vector['version']
- );
-
- $this->assertEquals(
- $this->_prettyHexToBin($vector['key_hex']),
- $key
- );
- }
- }
- public function testKeyVectors() {
- $vectors = $this->_getVectors('key');
- foreach ($vectors as $vector) {
-
- $encryptor = new Encryptor();
- $encryptedB64 = $encryptor->encryptWithArbitraryKeys(
- $this->_prettyHexToBin($vector['plaintext_hex']),
- $this->_prettyHexToBin($vector['enc_key_hex']),
- $this->_prettyHexToBin($vector['hmac_key_hex']),
- $this->_prettyHexToBin($vector['iv_hex']),
- $vector['version']
- );
-
- $this->assertEquals(
- $vector['ciphertext_hex'],
- $this->_binToPrettyHex(base64_decode($encryptedB64))
- );
- }
- }
- public function testPasswordVectors() {
- $vectors = $this->_getVectors('password');
- foreach ($vectors as $vector) {
- $encryptor = new Encryptor();
- $encryptedB64 = $encryptor->encryptWithArbitrarySalts(
- $this->_prettyHexToBin($vector['plaintext_hex']),
- $vector['password'],
- $this->_prettyHexToBin($vector['enc_salt_hex']),
- $this->_prettyHexToBin($vector['hmac_salt_hex']),
- $this->_prettyHexToBin($vector['iv_hex']),
- $vector['version']
- );
-
- $this->assertEquals(
- $vector['ciphertext_hex'],
- $this->_binToPrettyHex(base64_decode($encryptedB64))
- );
- }
- }
- private function _prettyHexToBin($data) {
- return hex2bin(preg_replace("/[^a-z0-9]/i", '', $data));
- }
- private function _binToPrettyHex($data) {
- $hex = bin2hex($data);
- $prettyHex = '';
- foreach (str_split($hex, 8) as $index => $part) {
- $prettyHex .= ($index != 0 ? ' ' : '') . $part;
- }
- return $prettyHex;
- }
- private function _getVectors($filename) {
- $absolutePath = __DIR__ . '/' . self::VECTOR_DIR . '/' . $filename;
- if (!file_exists($absolutePath)) {
- throw new \Exception('No such file: ' . $absolutePath);
- }
- $index = -1;
- $tests = array();
- $fd = fopen($absolutePath, 'r');
- while (!feof($fd)) {
- $line = trim(fgets($fd));
-
- if (preg_match("/^\s*(\w+)\s*\:\s*(.*)/", $line, $match)) {
- $key = strtolower($match[1]);
- $value = trim($match[2]);
-
- if ($key == 'title') {
- $index++;
- }
-
- $tests[$index][$key] = $value;
- }
- }
- fclose($fd);
-
- return $tests;
- }
-
- }
|