VectorTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace RNCryptor;
  3. class VectorBase extends \PHPUnit_Framework_TestCase {
  4. /**
  5. * Base directory for the test vector files,
  6. * relative to __DIR__
  7. */
  8. const VECTOR_DIR = '/../../vendor/rncryptor/rncryptor-spec/vectors/CURRENT';
  9. public function testKdfVectors() {
  10. $vectors = $this->_getVectors('kdf');
  11. foreach ($vectors as $vector) {
  12. $cryptor = new Cryptor();
  13. $key = $cryptor->generateKey(
  14. $this->_prettyHexToBin($vector['salt_hex']),
  15. $vector['password'],
  16. $vector['version']
  17. );
  18. $this->assertEquals(
  19. $this->_prettyHexToBin($vector['key_hex']),
  20. $key
  21. );
  22. }
  23. }
  24. public function testKeyVectors() {
  25. $vectors = $this->_getVectors('key');
  26. foreach ($vectors as $vector) {
  27. $encryptor = new Encryptor();
  28. $encryptedB64 = $encryptor->encryptWithArbitraryKeys(
  29. $this->_prettyHexToBin($vector['plaintext_hex']),
  30. $this->_prettyHexToBin($vector['enc_key_hex']),
  31. $this->_prettyHexToBin($vector['hmac_key_hex']),
  32. $this->_prettyHexToBin($vector['iv_hex']),
  33. $vector['version']
  34. );
  35. $this->assertEquals(
  36. $vector['ciphertext_hex'],
  37. $this->_binToPrettyHex(base64_decode($encryptedB64))
  38. );
  39. }
  40. }
  41. public function testPasswordVectors() {
  42. $vectors = $this->_getVectors('password');
  43. foreach ($vectors as $vector) {
  44. $encryptor = new Encryptor();
  45. $encryptedB64 = $encryptor->encryptWithArbitrarySalts(
  46. $this->_prettyHexToBin($vector['plaintext_hex']),
  47. $vector['password'],
  48. $this->_prettyHexToBin($vector['enc_salt_hex']),
  49. $this->_prettyHexToBin($vector['hmac_salt_hex']),
  50. $this->_prettyHexToBin($vector['iv_hex']),
  51. $vector['version']
  52. );
  53. $this->assertEquals(
  54. $vector['ciphertext_hex'],
  55. $this->_binToPrettyHex(base64_decode($encryptedB64))
  56. );
  57. }
  58. }
  59. private function _prettyHexToBin($data) {
  60. return hex2bin(preg_replace("/[^a-z0-9]/i", '', $data));
  61. }
  62. private function _binToPrettyHex($data) {
  63. $hex = bin2hex($data);
  64. $prettyHex = '';
  65. foreach (str_split($hex, 8) as $index => $part) {
  66. $prettyHex .= ($index != 0 ? ' ' : '') . $part;
  67. }
  68. return $prettyHex;
  69. }
  70. private function _getVectors($filename) {
  71. $absolutePath = __DIR__ . '/' . self::VECTOR_DIR . '/' . $filename;
  72. if (!file_exists($absolutePath)) {
  73. throw new \Exception('No such file: ' . $absolutePath);
  74. }
  75. $index = -1;
  76. $tests = array();
  77. $fd = fopen($absolutePath, 'r');
  78. while (!feof($fd)) {
  79. $line = trim(fgets($fd));
  80. if (preg_match("/^\s*(\w+)\s*\:\s*(.*)/", $line, $match)) {
  81. $key = strtolower($match[1]);
  82. $value = trim($match[2]);
  83. if ($key == 'title') {
  84. $index++;
  85. }
  86. $tests[$index][$key] = $value;
  87. }
  88. }
  89. fclose($fd);
  90. return $tests;
  91. }
  92. }