PacryptTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. class PaCryptTest extends \PHPUnit\Framework\TestCase {
  3. public function testMd5Crypt() {
  4. $hash = _pacrypt_md5crypt('test', '');
  5. $this->assertNotEmpty($hash);
  6. $this->assertNotEquals('test', $hash);
  7. $this->assertEquals($hash, _pacrypt_md5crypt('test', $hash));
  8. }
  9. public function testCrypt() {
  10. // E_NOTICE if we pass in '' for the salt
  11. $hash = _pacrypt_crypt('test', 'sa');
  12. $this->assertNotEmpty($hash);
  13. $this->assertNotEquals('test', $hash);
  14. $this->assertEquals($hash, _pacrypt_crypt('test', $hash));
  15. }
  16. public function testMySQLEncrypt() {
  17. if (!db_mysql()) {
  18. $this->markTestSkipped('Not using MySQL');
  19. }
  20. $hash = _pacrypt_mysql_encrypt('test1');
  21. $hash2 = _pacrypt_mysql_encrypt('test2');
  22. $this->assertNotEquals($hash, $hash2);
  23. $this->assertNotEmpty($hash);
  24. $this->assertNotEquals('test', $hash);
  25. $this->assertNotEquals('test', $hash2);
  26. $this->assertTrue( hash_equals($hash, _pacrypt_mysql_encrypt('test1', $hash) ), "hashes should equal....");
  27. }
  28. public function testAuthlib() {
  29. global $CONF;
  30. // too many options!
  31. foreach (
  32. [
  33. 'md5raw' => '098f6bcd4621d373cade4e832627b4f6',
  34. 'md5' => 'CY9rzUYh03PK3k6DJie09g==',
  35. // crypt requires salt ...
  36. 'SHA' => 'qUqP5cyxm6YcTAhz05Hph5gvu9M='
  37. ] as $flavour => $hash
  38. ) {
  39. $CONF['authlib_default_flavour'] = $flavour;
  40. $stored = "{" . $flavour . "}$hash";
  41. $hash = _pacrypt_authlib('test', $stored);
  42. $this->assertEquals($hash, $stored, "Hash: $hash vs Stored: $stored");
  43. //var_dump("Hash: $hash from $flavour");
  44. }
  45. }
  46. public function testPacryptDovecot() {
  47. global $CONF;
  48. if (!file_exists('/usr/bin/doveadm')) {
  49. $this->markTestSkipped("No /usr/bin/doveadm");
  50. }
  51. $CONF['encrypt'] = 'dovecot:SHA1';
  52. $expected_hash = '{SHA1}qUqP5cyxm6YcTAhz05Hph5gvu9M=';
  53. $this->assertEquals($expected_hash, _pacrypt_dovecot('test', ''));
  54. $this->assertEquals($expected_hash, _pacrypt_dovecot('test', $expected_hash));
  55. // This should also work.
  56. $sha512 = '{SHA512}ClAmHr0aOQ/tK/Mm8mc8FFWCpjQtUjIElz0CGTN/gWFqgGmwElh89WNfaSXxtWw2AjDBmyc1AO4BPgMGAb8kJQ=='; // foobar
  57. $this->assertEquals($sha512, _pacrypt_dovecot('foobar', $sha512));
  58. $sha512 = '{SHA512}ClAmHr0aOQ/tK/Mm8mc8FFWCpjQtUjIElz0CGTN/gWFqgGmwElh89WNfaSXxtWw2AjDBmyc1AO4BPgMGAb8kJQ=='; // foobar
  59. $this->assertNotEquals($sha512, _pacrypt_dovecot('foobarbaz', $sha512));
  60. }
  61. public function testPhpCrypt() {
  62. $config = Config::getInstance();
  63. Config::write('encrypt', 'php_crypt:MD5');
  64. $CONF = Config::getInstance()->getAll();
  65. $expected = '$1$z2DG4z9d$jBu3Cl3BPQZrkNqnflnSO.';
  66. $enc = _pacrypt_php_crypt('foo', $expected);
  67. $this->assertEquals($enc, $expected);
  68. $fail = _pacrypt_php_crypt('bar', $expected);
  69. }
  70. public function testPhpCryptHandlesPrefixAndOrRounds() {
  71. // try with 1000 rounds
  72. Config::write('encrypt', 'php_crypt:SHA256:1000');
  73. $password = 'hello';
  74. $randomHash = '$5$VhqhhsXJtPFeBX9e$kz3/CMIEu80bKdtDAcISIrDfdwtc.ilR68Vb3hNhu/7';
  75. $randomHashWithPrefix = '{SHA256-CRYPT}' . $randomHash;
  76. $new = _pacrypt_php_crypt($password, '');
  77. $this->assertNotEquals($randomHash, $new); // salts should be different.
  78. $enc = _pacrypt_php_crypt($password, $randomHash);
  79. $this->assertEquals($enc, $randomHash);
  80. $this->assertEquals($randomHash, _pacrypt_php_crypt("hello", $randomHash));
  81. $this->assertEquals($randomHash, _pacrypt_crypt("hello", $randomHash));
  82. Config::write('encrypt', 'php_crypt:SHA256::{SHA256-CRYPT}');
  83. $enc = _pacrypt_php_crypt("hello", $randomHash);
  84. $this->assertEquals($randomHash, $enc); // we passed in something lacking the prefix, so we shouldn't have added it in.
  85. $this->assertTrue(hash_equals($randomHash, $enc));
  86. // should cope with this :
  87. $enc = _pacrypt_php_crypt($password, '');
  88. $this->assertEquals($enc, _pacrypt_php_crypt($password, $enc));
  89. $this->assertRegExp('/^\{SHA256-CRYPT\}/', $enc);
  90. $this->assertGreaterThan(20, strlen($enc));
  91. }
  92. public function testPhpCryptRandomString() {
  93. $str1 = _php_crypt_random_string('abcdefg123456789', 2);
  94. $str2 = _php_crypt_random_string('abcdefg123456789', 2);
  95. $str3 = _php_crypt_random_string('abcdefg123456789', 2);
  96. $this->assertNotEmpty($str1);
  97. $this->assertNotEmpty($str2);
  98. $this->assertNotEmpty($str3);
  99. // it should be difficult for us to get three salts of the same value back...
  100. // not impossible though.
  101. $this->assertFalse(strcmp($str1, $str2) == 0 && strcmp($str1, $str3) == 0);
  102. }
  103. public function testSha512B64() {
  104. $str1 = _pacrypt_sha512_b64('test', '');
  105. $str2 = _pacrypt_sha512_b64('test', '');
  106. $this->assertNotEmpty($str1);
  107. $this->assertNotEmpty($str2);
  108. $this->assertNotEquals($str1, $str2); // should have different salts
  109. $actualHash = '{SHA512-CRYPT.B64}JDYkM2NWcFM1WFNlUHl5MzdwSiRZWW80d0FmeWg5MXpxcS4uY3dtYUR1Y1RodTJGTDY1NHpXNUNvRU0wT3hXVFFzZkxIZ1JJSTZmT281OVpDUWJOTTF2L0JXajloME0vVjJNbENNMUdwLg==';
  110. $check = _pacrypt_sha512_b64('test', $actualHash);
  111. $this->assertTrue(hash_equals($check, $actualHash));
  112. $str3 = _pacrypt_sha512_b64('foo', '');
  113. $this->assertNotEmpty($str3);
  114. $this->assertFalse(hash_equals('test', $str3));
  115. $this->assertTrue(hash_equals(_pacrypt_sha512_b64('foo', $str3), $str3));
  116. }
  117. }