Skip to content

Commit

Permalink
Merge pull request #23 from AngryBytes/feat/php-8.3
Browse files Browse the repository at this point in the history
Feat/php 8.3
  • Loading branch information
stephank authored Dec 1, 2023
2 parents ec317e3 + 6d3638a commit 73ea0a1
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-versions: ['7.4', '8.0', '8.1', '8.2']
php-versions: ['8.1', '8.2', '8.3']
name: PHP ${{ matrix.php-versions }} tests
steps:
- name: Checkout
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 5.0.0

### PHP support

- Dropped support for PHP `8.0` and lower.
- Added support for PHP `8.3`.

## 4.0.1

## PHP Support
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

[![Author](http://img.shields.io/badge/[email protected]?style=flat-square)](https://twitter.com/angrybytes)
[![Software License](https://img.shields.io/badge/license-proprietary-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Build Status](https://travis-ci.org/AngryBytes/hash.svg?branch=master)](https://travis-ci.org/AngryBytes/hash)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/AngryBytes/hash/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/AngryBytes/hash/?branch=master)
[![Build Status](https://github.com/AngryBytes/hash/actions/workflows/php-checks.yml/badge.svg?event=push)

A simple PHP library that simplifies cryptographical hashing. It provides an
object oriented interface to a variety of hashing methods.
object-oriented interface to a variety of hashing methods.

## Requirements

* PHP `7.3`, `7.4` or PHP `8.0` (recommended)
* PHP `8.1`, `8.2` or PHP `8.3` (recommended)

## Installation

Expand All @@ -32,7 +31,7 @@ Some of the main features of this component:

### Hashers

This library comes with a set of hashers to be utilised by this hash component (or
This library comes with a set of hashers to be utilized by this hash component (or
to be used on their own):

* `AngryBytes\Hash\Hasher\BlowFish`
Expand Down
8 changes: 4 additions & 4 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ This document lists important upgrade nodes and BC breaks per version.

### Update Hashing Values

If you are hashing multiple values you will need to change the way their are passed
to the hasher. Instead of passing each variable separately you will need to pass
If you are hashing multiple values, you will need to change the way they are passed
to the hasher. Instead of passing each variable separately, you will need to pass
them as an array. Like so:

```php
Expand All @@ -30,8 +30,8 @@ The same principle applies to `Hash::shortHash()`.

### Update Hash Validation

In the previous version hash validation would be done by creating a hash and comparing
it to the existing hash. Now, you can simple pass the value(s) to hash and the
In the previous version, hash validation would be done by creating a hash and comparing
it to the existing hash. Now, you can simply pass the value(s) to hash and the
existing hash to methods: `verify()` or `verfiyShortHash()`.

```php
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"scripts": {
"phpcheck": [
"./vendor/bin/phpstan analyse -c phpstan.neon -l max --memory-limit=1G src/ tests/",
"./vendor/bin/phpstan analyse -l max --memory-limit=1G src/ tests/",
"./vendor/bin/phpcs -p --standard=PSR2 --extensions=php src/ tests/"
],
"phpcbf": [
Expand All @@ -35,11 +35,11 @@
]
},
"require": {
"php": "7.4.* || 8.0.* || 8.1.* || 8.2.*"
"php": "8.1.* || 8.2.* || 8.3.*"
},
"require-dev": {
"phpstan/phpstan": "1.9.12",
"phpunit/phpunit": "9.5.28",
"squizlabs/php_codesniffer": "3.7.1"
"phpstan/phpstan": "1.10.46",
"phpunit/phpunit": "10.4.2",
"squizlabs/php_codesniffer": "3.7.2"
}
}
7 changes: 0 additions & 7 deletions phpstan.neon

This file was deleted.

21 changes: 7 additions & 14 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
<?xml version="1.0"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
bootstrap="./vendor/autoload.php"
colors="true"
>

<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" bootstrap="./vendor/autoload.php" colors="true">
<coverage/>
<testsuite name="Hasher">
<directory>./tests/AngryBytes/Hash/Test</directory>
</testsuite>

<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
8 changes: 4 additions & 4 deletions src/AngryBytes/Hash/HMAC.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ public function __construct(string $algorithm)
*/
public static function platformSupportsAlgorithm(string $algorithm): bool
{
return in_array($algorithm, hash_algos());
return in_array($algorithm, hash_algos(), true);
}

/**
* Create an HMAC
*
* This method accepts multiple variables as input, but is restricted to
* strings. All input will be concatenated before hashing.
* strings. All inputs will be concatenated before hashing.
*
* @param string[] $args
*/
public function hmac(string $sharedSecret, ...$args): string
{
// Get the data concatenated
$data = '';
foreach ($args as $index => $arg) {
foreach ($args as $arg) {
$data .= $arg;
}

Expand All @@ -55,7 +55,7 @@ public function hmac(string $sharedSecret, ...$args): string
*/
public function validHmac(string $message, string $hmac, string $sharedSecret): bool
{
// Compare HMAC with received message
// Compare HMAC with a received message
return Hash::compare(
$hmac,
// The HMAC as it should be for our shared secret
Expand Down
21 changes: 7 additions & 14 deletions src/AngryBytes/Hash/Hash.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ public function getHasher(): HasherInterface
/**
* Generate a hash
*
* @param mixed $data The data to hash. This can either be a scalar value or a serializable value.
* @param mixed[] $options Additional hasher options (the actual options depend on the registered Hasher)
*/
public function hash($data, array $options = []): string
public function hash(mixed $data, array $options = []): string
{
return $this->hasher->hash(
self::getDataString($data),
Expand All @@ -65,11 +64,9 @@ public function hash($data, array $options = []): string
/**
* Verify if the data matches the hash
*
* @param mixed $data The data to verify against the hash string.
* This can either be a scalar value or a serializable value.
* @param mixed[] $options
*/
public function verify($data, string $hash, array $options = []): bool
public function verify(mixed $data, string $hash, array $options = []): bool
{
return $this->hasher->verify(
self::getDataString($data),
Expand All @@ -82,18 +79,17 @@ public function verify($data, string $hash, array $options = []): bool
* Hash something into a short hash
*
* This is simply a shortened version of hash(), returning a 7 character hash, which should be good
* enough for identification purposes. Therefore it MUST NOT be used for cryptographic purposes or
* enough for identification purposes. Therefore, it MUST NOT be used for cryptographic purposes or
* password storage but only to create a fast, short string to compare or identify.
*
* It is RECOMMENDED to only use this method with the Hasher\MD5 hasher as hashes
* created by bcrypt/crypt() have a common beginning.
*
* @see Hash::hash()
*
* @param mixed $data
* @param mixed[] $options
*/
public function shortHash($data, array $options = []): string
public function shortHash(mixed $data, array $options = []): string
{
return substr($this->hash($data, $options), 0, 7);
}
Expand All @@ -103,10 +99,9 @@ public function shortHash($data, array $options = []): string
*
* @see Hash::shortHash()
*
* @param mixed $data
* @param mixed[] $options
*/
public function verifyShortHash($data, string $shortHash, array $options = []): bool
public function verifyShortHash(mixed $data, string $shortHash, array $options = []): bool
{
return self::compare(
$this->shortHash($data, $options),
Expand Down Expand Up @@ -150,10 +145,8 @@ protected function setSalt(?string $salt = null): self
* Get the data as a string
*
* Will serialize non-scalar values
*
* @param mixed $data
*/
private static function getDataString($data): string
private static function getDataString(mixed $data): string
{
if (is_scalar($data)) {
return (string) $data;
Expand All @@ -171,7 +164,7 @@ private static function getDataString($data): string
* @param mixed[] $options
* @return mixed[]
*/
private function parseHashOptions(array $options = [])
private function parseHashOptions(array $options = []): array
{
$defaultOptions = [];

Expand Down
4 changes: 2 additions & 2 deletions src/AngryBytes/Hash/Hasher/MD5.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* This hasher MUST NOT be used for password storage. It is RECOMMENDED
* to use the Hasher\Password for this purpose
*
* @see AngryBytes\Hasher\Password For a password hasher
* @see Password For a password hasher
*/
class MD5 implements HasherInterface
{
Expand All @@ -24,7 +24,7 @@ class MD5 implements HasherInterface
*/
public function hash(string $string, array $options = []): string
{
$salt = isset($options['salt']) ? $options['salt'] : '';
$salt = $options['salt'] ?? '';

return md5($string . '-' . $salt);
}
Expand Down
22 changes: 14 additions & 8 deletions src/AngryBytes/Hash/Hasher/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace AngryBytes\Hash\Hasher;

use AngryBytes\Hash\HasherInterface;
use InvalidArgumentException;
use RuntimeException;
use ValueError;

/**
* Password Hasher Using Native PHP Hash Methods
Expand All @@ -26,16 +29,19 @@ public function __construct(?int $cost = null)
* {@inheritDoc}
*
* Supported options in $options array:
* - 'salt': Override the salt generated by password_hash() (discouraged)
* - 'cost': Override the default cost (not advised)
*
* @throws \RuntimeException If the hashing fails
* @throws RuntimeException If the hashing fails
*/
public function hash(string $data, array $options = []): string
{
$hash = password_hash($data, PASSWORD_DEFAULT, $this->parsePasswordOptions($options));
if ($hash === false) {
throw new \RuntimeException('Failed to hash password');
try {
$hash = password_hash($data, PASSWORD_DEFAULT, $this->parsePasswordOptions($options));
} catch (ValueError $e) {
throw new RuntimeException(sprintf(
'Failed to hash password. An exception was thrown: %s',
$e->getMessage()
));
}

return $hash;
Expand All @@ -54,7 +60,7 @@ public function verify(string $string, string $hash, array $options = []): bool
*
* If true, the password should be rehashed after verification.
*
* @param mixed[] $options Password options, @see hash()
* @param mixed[] $options Password options, @see self::hash()
*/
public function needsRehash(string $hash, array $options = []): bool
{
Expand All @@ -77,7 +83,7 @@ public function getInfo(string $hash): array
* Set cost
*
* @param ?int $cost
* @throws \InvalidArgumentException if the cost is too high or low
* @throws InvalidArgumentException if the cost is too high or low
*/
public function setCost(?int $cost = null): self
{
Expand All @@ -88,7 +94,7 @@ public function setCost(?int $cost = null): self
}

if ($cost < 4 || $cost > 31) {
throw new \InvalidArgumentException(sprintf(
throw new InvalidArgumentException(sprintf(
'Cost value "%d" needs to be greater than 3 and smaller than 32',
(int) $cost
));
Expand Down

0 comments on commit 73ea0a1

Please sign in to comment.