PostfixAdmin - web based virtual user administration interface for Postfix mail servers https://postfixadmin.github.io/postfixadmin/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
5.8 KiB

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