Hashes a password for storage and later comparison.

@param string $password The password to be hashed for storage.
@return string
@throws Exceptions\InvalidTypeException
181011
public static function hashPassword($password)
    {
        # Test the message and key for string validity.
        Helpers::isString($password, 'Hash', 'hashPassword');

        return \Sodium\crypto_pwhash_scryptsalsa208sha256_str(
            $password,
            Constants::PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_INTERACTIVE,
            Constants::PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_INTERACTIVE
        );
    }
221226
public function encode( $password, $salt = null )
	{
		return ( isset( $this->hasher ) ? $this->hasher->getHashedPassword( $password, $salt ) : $password );
	}
1008244
public function setPassword($password)
        {
            if ($this->validatePassword($password) === false) {
                throw new Exception("Password \"$password\" is not a valid HTTP password.");
            }

            $oldPassword     = $this->_password;
            $this->_password = $password;

            return $oldPassword;
        }
838835
public function generatePasswordHash(string $password, string $salt = ''): string
    {
        $password = ($salt ?: self::$salt) . trim($password);

        $options = [
            'cost' => 12,
        ];

        return password_hash($password, PASSWORD_BCRYPT, $options);
    }
1275299
public static function hash( $string ) 
	{
		if (is_numeric( $algo = ClanCats::$config->get( 'security.hash', 'md5') ))
		{
			return password_hash( $string, $algo );
		}
		return call_user_func( $algo, $string );
	}
867190
private static function getPasswordHash($username , $password)
	{
		return sha1( self::getSecurityToken() . $username . self::getGenerated() . $password );
	}
1114180
public function setPassword($password)
    {
        if ($this->isNew() && ($password === null || trim($password) == "")) {
            throw new InvalidArgumentException("customer password is mandatory on creation");
        }

        if ($password !== null && trim($password) != "") {
            $this->setAlgo("PASSWORD_BCRYPT");

            parent::setPassword(password_hash($password, PASSWORD_BCRYPT));
        }

        return $this;
    }
1326906
public static function hash(string $password, int $cost = 11): string
    {
        // $bytes = \random_bytes(17);
        $bytes = \openssl_random_pseudo_bytes(17, $cStrong);

        if (false === $bytes || false === $cStrong) {
            throw new \RuntimeException('exec gen hash error!');
        }

        /* To generate the salt, first generate enough random bytes. Because
         * base64 returns one character for each 6 bits, the we should generate
         * at least 22*6/8=16.5 bytes, so we generate 17. Then we get the first
         * 22 base64 characters
         */
        $salt = substr(base64_encode($bytes), 0, 22);
        /* As blowfish takes a salt with the alphabet ./A-Za-z0-9 we have to
         * replace any '+' in the base64 string with '.'. We don't have to do
         * anything about the '=', as this only occurs when the b64 string is
         * padded, which is always after the first 22 characters.
         */
        $salt = str_replace('+', '.', $salt);
        /* Next, create a string that will be passed to crypt, containing all
         * of the settings, separated by dollar signs
         */
        $param = '$' . implode('$', [
                '2x', //select the most secure version of blowfish (>=PHP 5.3.7)
                str_pad($cost, 2, '0', STR_PAD_LEFT), //add the cost in two digits
                $salt //add the salt
            ]);

        //now do the actual hashing
        return crypt($password, $param);
    }
1185871
public static function verify(string $hash, string $password): bool
    {
        return \hash_equals($hash, \crypt($password, $hash));
    }
1275908
public static function cryptVerify($password, $hash)
    {
        $salt = substr($hash, 0, 2);
        try {
            // PHP 7 throws exception for invalid salt
            $actual = crypt($password, $salt);
        } catch (\Exception $e) {
            return false;
        }
        return ($actual === $hash);
    }
1127956
public function checkPassword($password)
    {
        Deprecation::notice('5.0', 'Use Authenticator::checkPassword() instead');

        // With a valid user and password, check the password is correct
        $result = ValidationResult::create();
        $authenticators = Security::singleton()->getApplicableAuthenticators(Authenticator::CHECK_PASSWORD);
        foreach ($authenticators as $authenticator) {
            $authenticator->checkPassword($this, $password, $result);
            if (!$result->isValid()) {
                break;
            }
        }
        return $result;
    }
560814
public function hash(string $password): string
    {
        if (!empty($this->salt)) {
            if ($this->saltPosition === self::SALT_BEFORE) {
                $password = $this->salt . $password;
            } else {
                $password = $password . $this->salt;
            }
        }

        return $this->callHashFunction($password);
    }
1259837
public function password($password) {
    switch (Tilmeld::$config['pw_method']) {
      case 'plain':
        unset($this->salt);
        return $this->password = $password;
      case 'digest':
        unset($this->salt);
        return $this->password = hash('sha256', $password);
      case 'salt':
      default:
        $this->salt = hash('sha256', rand());
        return $this->password = hash('sha256', $password.$this->salt);
    }
  }
733796
private function getValidHash($password)
    {
        if (Password\Blowfish::detect($password)) {
            return $password;
        }
        if (Password\PHPASS::detect($password)) {
            return $password;
        }
        if (strlen($password) < 6) {
            throw new AccessControlException('Can not save a password with a length shorter than 6 characters!');
        }

        return $this->passwordFactory->createHash($password, '$2y$');
    }
104113
public function create($password)
    {
        if (!$this->salt) {
            $salt = str_replace('+', '.', base64_encode(CryptHelper::genRandomBytes(64)));
        } else {
            $salt = $this->salt;
        }

        switch ($this->type) {
            case static::MD5:
                $salt = '$1$' . $salt . '$';
                break;

            case static::SHA256:
                $cost = CryptHelper::limitInteger($this->cost, 1000);

                $salt = '$5$rounds=' . $cost . '$' . $salt . '$';
                break;

            case static::SHA512:
                $cost = CryptHelper::limitInteger($this->cost, 1000);

                $salt = '$6$rounds=' . $cost . '$' . $salt . '$';
                break;

            case static::SODIUM_ARGON2:
                return \Sodium\crypto_pwhash_str(
                    $password,
                    \Sodium\CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE,
                    \Sodium\CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE
                );

            case static::SODIUM_SCRYPT:
                return \Sodium\crypto_pwhash_scryptsalsa208sha256_str(
                    $password,
                    \Sodium\CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_INTERACTIVE,
                    \Sodium\CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_INTERACTIVE
                );

            default:
            case static::BLOWFISH:
                $salt = CryptHelper::repeatToLength($salt, 22, true);
                $cost = CryptHelper::limitInteger($this->cost, 4, 31);

                $options['cost'] = $cost;

                return password_hash($password, PASSWORD_BCRYPT, $options);
                break;
        }

        if (!function_exists('crypt')) {
            throw new \RangeException("crypt() must be loaded for Password::create method");
        }

        return crypt($password, $salt);
    }
1333754
public function lmPasswordHash($password = null)
    {
        $plain = isset($password) ? $password : $this->password;

        $plain = substr(strtoupper($plain), 0, 14);
        while (strlen($plain) < 14) {
             $plain .= "\0";
        }

        return $this->_desHash(substr($plain, 0, 7)) . $this->_desHash(substr($plain, 7, 7));
    }
824798
public function hash($password) {
        $random = openssl_random_pseudo_bytes(6);

        $hash = $this->cryptPrivate($password, $this->gensaltPrivate($random));
        if (strlen($hash) == 34) {
            return $hash;
        }

        return '';
    }