Skip to content

Commit

Permalink
v3 - require PHP 7, use strict types
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-scott committed May 18, 2016
1 parent 347f7bf commit 78d1370
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Version 2.0.0 (not released yet)
# Version 3.0.0 (2016-05-18)

* Update `defuse/php-encryption` to `2.0.0` (not released yet either)
* Set minimum PHP version to 7.0
* Use strict_types

# Version 2.0.0 (2016-05-18)

* Update `defuse/php-encryption` to `2.0.0`
* Use `paragonie/constant_time_encoding`

# Version 1.1.0

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
]
},
"require": {
"php": ">= 7.0",
"defuse/php-encryption": "^2.0",
"paragonie/random_compat": "^1|^2",
"paragonie/constant_time_encoding": "^1|^2"
"paragonie/constant_time_encoding": "^2"
},
"require-dev": {
"defuse/php-encryption": "^2.0"
"phpunit/phpunit": "^5|^6"
}
}
17 changes: 9 additions & 8 deletions src/PasswordLock.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace ParagonIE\PasswordLock;

use \Defuse\Crypto\Crypto;
Expand All @@ -17,7 +18,7 @@ class PasswordLock
* @return string
* @throws \Exception
*/
public static function hashAndEncrypt($password, Key $aesKey)
public static function hashAndEncrypt(string $password, Key $aesKey): string
{
if (!\is_string($password)) {
throw new \InvalidArgumentException(
Expand Down Expand Up @@ -46,7 +47,7 @@ public static function hashAndEncrypt($password, Key $aesKey)
* @throws \Exception
* @throws \InvalidArgumentException
*/
public static function decryptAndVerifyLegacy($password, $ciphertext, $aesKey)
public static function decryptAndVerifyLegacy(string $password, string $ciphertext, string $aesKey): bool
{
if (!\is_string($password)) {
throw new \InvalidArgumentException(
Expand Down Expand Up @@ -79,7 +80,7 @@ public static function decryptAndVerifyLegacy($password, $ciphertext, $aesKey)
* @throws \Exception
* @throws \InvalidArgumentException
*/
public static function decryptAndVerify($password, $ciphertext, Key $aesKey)
public static function decryptAndVerify(string $password, string $ciphertext, Key $aesKey): bool
{
if (!\is_string($password)) {
throw new \InvalidArgumentException(
Expand Down Expand Up @@ -111,7 +112,7 @@ public static function decryptAndVerify($password, $ciphertext, Key $aesKey)
* @param Key $newKey
* @return string
*/
public static function rotateKey($ciphertext, Key $oldKey, Key $newKey)
public static function rotateKey(string $ciphertext, Key $oldKey, Key $newKey): string
{
$plaintext = Crypto::decrypt($ciphertext, $oldKey);
return Crypto::encrypt($plaintext, $newKey);
Expand All @@ -128,11 +129,11 @@ public static function rotateKey($ciphertext, Key $oldKey, Key $newKey)
* @throws \Exception
*/
public static function upgradeFromVersion1(
$password,
$ciphertext,
$oldKey,
string $password,
string $ciphertext,
string $oldKey,
Key $newKey
) {
): string {
if (!self::decryptAndVerifyLegacy($password, $ciphertext, $oldKey)) {
throw new \Exception(
'The correct password is necessary for legacy migration.'
Expand Down
6 changes: 4 additions & 2 deletions tests/PasswordLockTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
use \ParagonIE\PasswordLock\PasswordLock;
use \Defuse\Crypto\Key;
/**
* @backupGlobals disabled
* @backupStaticAttributes disabled
Expand All @@ -8,7 +10,7 @@ class PasswordLockTest extends PHPUnit_Framework_TestCase
{
public function testHash()
{
$key = \Defuse\Crypto\Key::createNewRandomKey();
$key = Key::createNewRandomKey();

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

Expand All @@ -26,7 +28,7 @@ public function testHash()
*/
public function testBitflip()
{
$key = \Defuse\Crypto\Key::createNewRandomKey();
$key = Key::createNewRandomKey();
$password = PasswordLock::hashAndEncrypt('YELLOW SUBMARINE', $key);
$password[0] = (\ord($password[0]) === 0 ? 255 : 0);

Expand Down

0 comments on commit 78d1370

Please sign in to comment.