Skip to content

Commit

Permalink
Sync with defuse/php-encryption changes
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-scott committed Apr 23, 2016
1 parent 439ced4 commit 347f7bf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 35 deletions.
2 changes: 2 additions & 0 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@
require $file;
}
});

require_once __DIR__ . '/vendor/autoload.php';
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
},
"require": {
"defuse/php-encryption": "^2.0",
"paragonie/random_compat": "^1.2",
"paragonie/constant_time_encoding": "^0.3"
"paragonie/random_compat": "^1|^2",
"paragonie/constant_time_encoding": "^1|^2"
},
"require-dev": {
"defuse/php-encryption": "^2.0"
Expand Down
38 changes: 12 additions & 26 deletions src/PasswordLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use \Defuse\Crypto\Crypto;
use \Defuse\Crypto\Key;
use \ParagonIE\ConstantTime\Base64;
use \ParagonIE\ConstantTime\Binary;

class PasswordLock
{
Expand All @@ -14,6 +15,7 @@ class PasswordLock
* @param string $password
* @param Key $aesKey
* @return string
* @throws \Exception
*/
public static function hashAndEncrypt($password, Key $aesKey)
{
Expand All @@ -40,7 +42,9 @@ public static function hashAndEncrypt($password, Key $aesKey)
* @param string $password
* @param string $ciphertext
* @param string $aesKey - must be exactly 16 bytes
* @return boolean
* @return bool
* @throws \Exception
* @throws \InvalidArgumentException
*/
public static function decryptAndVerifyLegacy($password, $ciphertext, $aesKey)
{
Expand All @@ -49,7 +53,7 @@ public static function decryptAndVerifyLegacy($password, $ciphertext, $aesKey)
'Password must be a string.'
);
}
if (self::safeStrlen($aesKey) !== 16) {
if (Binary::safeStrlen($aesKey) !== 16) {
throw new \Exception("Encryption keys must be 16 bytes long");
}
$hash = Crypto::legacyDecrypt(
Expand All @@ -71,7 +75,9 @@ public static function decryptAndVerifyLegacy($password, $ciphertext, $aesKey)
* @param string $password
* @param string $ciphertext
* @param Key $aesKey
* @return boolean
* @return bool
* @throws \Exception
* @throws \InvalidArgumentException
*/
public static function decryptAndVerify($password, $ciphertext, Key $aesKey)
{
Expand All @@ -85,9 +91,6 @@ public static function decryptAndVerify($password, $ciphertext, Key $aesKey)
'Ciphertext must be a string.'
);
}
if (self::safeStrlen($aesKey) !== 32) {
throw new \Exception("Encryption keys must be 32 bytes long");
}
$hash = Crypto::decrypt(
$ciphertext,
$aesKey
Expand Down Expand Up @@ -119,9 +122,10 @@ public static function rotateKey($ciphertext, Key $oldKey, Key $newKey)
*
* @param string $password
* @param string $ciphertext
* @param sring $oldKey
* @param string $oldKey
* @param Key $newKey
* @return string
* @throws \Exception
*/
public static function upgradeFromVersion1(
$password,
Expand All @@ -135,24 +139,6 @@ public static function upgradeFromVersion1(
);
}
$plaintext = Crypto::legacyDecrypt($ciphertext, $oldKey);
return self::hashAndEncrypt($password, $newKey);
}

/**
* Don't count characters, count the number of bytes
*
* @param string
* @return int
*/
protected static function safeStrlen($str)
{
static $exists = null;
if ($exists === null) {
$exists = \function_exists('\\mb_strlen');
}
if ($exists) {
return \mb_strlen($str, '8bit');
}
return \strlen($str);
return self::hashAndEncrypt($plaintext, $newKey);
}
}
11 changes: 4 additions & 7 deletions tests/PasswordLockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ class PasswordLockTest extends PHPUnit_Framework_TestCase
{
public function testHash()
{
$key = \Defuse\Crypto\Key::LoadFromAsciiSafeString(
\hex2bin('0102030405060708090a0b0c0d0e0f10')
);
$key = \Defuse\Crypto\Key::createNewRandomKey();

$password = PasswordLock::hashAndEncrypt('YELLOW SUBMARINE', $key);

$this->assertTrue(
Expand All @@ -23,13 +22,11 @@ public function testHash()
}

/**
* @expectedException \Defuse\Crypto\Exception\InvalidCiphertext
* @expectedException \Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException
*/
public function testBitflip()
{
$key = \Defuse\Crypto\Key::LoadFromAsciiSafeString(
\hex2bin('0102030405060708090a0b0c0d0e0f10')
);
$key = \Defuse\Crypto\Key::createNewRandomKey();
$password = PasswordLock::hashAndEncrypt('YELLOW SUBMARINE', $key);
$password[0] = (\ord($password[0]) === 0 ? 255 : 0);

Expand Down

0 comments on commit 347f7bf

Please sign in to comment.