Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
2 / 3
CRAP
87.50% covered (warning)
87.50%
7 / 8
Hash
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
2 / 3
3.02
87.50% covered (warning)
87.50%
7 / 8
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
6 / 6
 valid
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 generate
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
<?php
declare(strict_types=1);
namespace Eywa\Security\Hashing
{
    use Eywa\Exception\Kedavra;
    /**
     * Class Hash
     *
     * @author  Willy Micieli
     *
     * @package Imperium\Security\Hashing
     *
     * @license GPL
     *
     * @version 10
     *
     */
    class Hash
    {
        /**
         *
         * The valid hash
         *
         */
        private string $valid;
        /**
         *
         * The secret key
         *
         */
        private string $secret;
        /**
         *
         * The hash algorithm
         *
         */
        private string $algorithm;
        /**
         *
         */
        private string $data;
        /**
         *
         * Hash constructor.
         *
         * @param string $data
         *
         * @throws Kedavra
         */
        public function __construct(string $data)
        {
            $this->algorithm = config('hash', 'algorithm');
            $this->secret = config('hash', 'secret');
            $this->data = $data;
            not_in(hash_algos(), $this->algorithm, true, "The current algorithm is not supported");
            $this->valid = hash_hmac($this->algorithm, $this->data, $this->secret);
        }
        /**
         *
         * Check if the hash is valid
         *
         * @param  string  $value
         *
         * @return bool
         *
         */
        public function valid(string $value): bool
        {
            return hash_equals($this->valid, $value);
        }
        /**
         *
         * Generate the hash
         *
         * @return string
         *
         */
        public function generate(): string
        {
            return $this->valid;
        }
    }
}