Skip to content

Commit

Permalink
Code tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
t0mmie committed Nov 30, 2023
1 parent 3a03c3a commit 6d3638a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 36 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

[![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

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
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 6d3638a

Please sign in to comment.