Laravel по-русски

Русское сообщество разработки на PHP-фреймворке Laravel.

Ты не вошёл. Вход тут.

#1 Re: Laravel 5.x » Перенос сайта с MODX Revo на Laravel 5 » 20.12.2016 07:24:10

Привет, по поводу шифрования PBKDF2, выдернул из самого Modx Revo классы хэширования ниже приведу код, может кому будет интересно и поможет:

<?php

namespace HashPassword;

abstract class Hash {

    public $host= null;

    public $options= array();

    function __construct(Hashing &$host, $options= array()) {
        $this->host =& $host;
        if (is_array($options)) {
            $this->options = $options;
        }
    }

    public function getOption($key, $options = null, $default = null) {
        if (is_array($options) && array_key_exists($key, $options)) {
            $option = $options[$key];
        } else {
            $option = $this->host->getOption($key, $this->options, $default);
        }
        return $option;
    }

    public abstract function hash($string, array $options = array());
}

<?php

namespace HashPassword;

class Hashing {

    public $options= array();

    protected $_hashes= array();


    function __construct($options= array()) {
        if (is_array($options)) {
            $this->options = $options;
        }
    }


    public function getOption($key, $options = null, $default = null) {
        if (is_array($options) && array_key_exists($key, $options)) {
            $option = $options[$key];
        } elseif (array_key_exists($key, $this->options)) {
            $option = $this->options[$key];
        } else {
            $option = $default;
        }
        return $option;
    }

    public function getHash($key, $class, $options = array())
    {
            if (empty($key)) {
                abort(400, 'Key not be empty');
            }

            if (!array_key_exists($key, $this->_hashes)) {
                $hash = new Pbkdf2($this, $options);
                if ($hash instanceof Pbkdf2) {
                    $this->_hashes[$key] = $hash;
                    $this->$key =& $this->_hashes[$key];
                }
            }
            if (array_key_exists($key, $this->_hashes)) {
                return $this->_hashes[$key];
            }
    }
}

<?php

namespace HashPassword;

class Pbkdf2 extends Hash {

    public function hash($string, array $options = array()) {
        $derivedKey = false;
        $salt = $this->getOption('salt', $options, false);
        if (is_string($salt) && strlen($salt) > 0) {
            $iterations = (integer) $this->getOption('iterations', $options, 1000);
            $derivedKeyLength = (integer) $this->getOption('derived_key_length', $options, 32);
            $algorithm = $this->getOption('algorithm', $options, 'sha256');

            $hashLength = strlen(hash($algorithm, null, true));
            $keyBlocks = ceil($derivedKeyLength / $hashLength);
            $derivedKey = '';
            for ($block = 1; $block <= $keyBlocks; $block++) {
                $hashBlock = $hb = hash_hmac($algorithm, $salt . pack('N', $block), $string, true);
                for ($blockIteration = 1; $blockIteration < $iterations; $blockIteration++) {
                    $hashBlock ^= ($hb = hash_hmac($algorithm, $hb, $string, true));
                }
                $derivedKey .= $hashBlock;
            }
            $derivedKey = substr($derivedKey, 0, $derivedKeyLength);
            if (!$this->getOption('raw_output', $options, false)) {
                $derivedKey = base64_encode($derivedKey);
            }
        } else {
            abort(400, 'PBKDF2 requires a valid salt string');
        }
        return $derivedKey;
    }
}

<?php

namespace HashPassword;

class Password
{
    protected static function getPbkdf2Instance ()
    {
        $hashing = new Hashing();
        $pbkdf2 = new Pbkdf2($hashing);

        return $pbkdf2;
    }
    
    public static function validate ($password, $safePassword, $salt)
    {
        $pbkdf2 = self::getPbkdf2Instance();

        $hash = $pbkdf2->hash($password, ['salt' => $salt]);

        if ($hash !== $safePassword) {
            return false;
        }

        return true;
    }
}

Подвал раздела