Generate a salt for storing the encrypted password

Taken from http://code.activestate.com/recipes/576894-generate-a-salt/

@param int $max Length of salt
@return string
558223
private function createSalt($max = 15)
    {
        $characterList = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $i = 0;
        $salt = "";
        do {
            $salt .= $characterList{mt_rand(0,strlen($characterList)-1)};
            $i++;
        } while ($i < $max);

        return $salt;
    }
221226
public function encode( $password, $salt = null )
	{
		return ( isset( $this->hasher ) ? $this->hasher->getHashedPassword( $password, $salt ) : $password );
	}
1173727
public static function generateRandomInt($min, $max)
    {
        $range = $max - $min;
        $bits = $bytes = $mask = $val = 0;
        while ($range > 0) {
            if ($bits % 8 === 0) {
               ++$bytes;
            }
            ++$bits;
            $range >>= 1;
            $mask = $mask << 1 | 1;
        }
        $valueShift = $min;

        do {
            if ($attempts > 128) {
                throw new \Exception('Could not generate valid random integer');
            }
            $randomByteString = self::generateCryptoKey($bytes);

            $val = 0;
            for ($i = 0; $i < $bytes; ++$i) {
                $val |= ord($randomByteString[$i]) << ($i * 8);
            }

            $val &= $mask;
            $val += $valueShift;

            ++$attempts;

        } while (!is_int($val) || $val > $max || $val < $min);

        return (int) $val;
    }
1097620
public function generatePassword(int $length = 8)
    {
        if ($length <= 0)
            throw new DomainException("Cannot generate zero-length passwords");

        if (empty($this->characters))
            throw new UnderflowException("First add characters used to generate the password");

        $chars = implode('', array_keys($this->characters));
        $max_val = mb_strlen($chars) - 1;
        $pwd = "";
        for ($i = 0; $i < $length; ++$i)
        {
            $pos = random_int(0, $max_val);
            $pwd .= mb_substr($chars, $pos, 1);
        }
        return $pwd;
    }
626914
protected function protect($password, $salt = null)
    {
        $salt = isset($salt)
            ? $salt
            : $this->salt;

        return $password . $salt;
    }
896525
public static function getSalt(): string
    {
        $result = '';

        try {
            $result = static::readSaltFromFile();
        } catch (InvalidArgumentException $e) {
            Log::warning("Failed to read salt from file");
        }

        if ($result) {
            return $result;
        }

        try {
            $salt = static::generateSalt();
            static::writeSaltToFile($salt);
            $result = static::readSaltFromFile();
        } catch (InvalidArgumentException $e) {
            throw new InvalidArgumentException("Failed to regenerate and save new salt: " . $e->getMessage(), 0, $e);
        }
        Log::warning("New salt is generated and stored.  Users might need to logout and clean their cookies.");

        return $result;
    }
448019
private function getSalt() {
		$salt = $this->getIdentifier();

		$bytes = $this->getRandomBytes($this->getSaltLength());

		$salt .= $this->encodeBytes($bytes);

		return $salt;
	}
1226170
public static function random_password($length = 8)
	{
		$genpass = '';
		$salt = "abchefghjkmnpqrstuvwxyz0123456789";

		srand((double)microtime()*1000000);

		$i = 0;

		while ($i < $length)
		{
			$num = rand() % 33;
			$tmp = substr($salt, $num, 1);
			$genpass = $genpass . $tmp;
			$i++;
		}

		return $genpass;
	}
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);
    }
686838
private static function generateSalt (int $length) : string
	{
		// Not 100% unique, not 100% random, but good enough for a salt
		// MD5 returns 32 characters
		$uniqueRandomString = md5(uniqid(mt_rand(),true));

		// Valid Characters for a salt are [a-AZ-Z0-9./]
		$base64String = base64_encode($uniqueRandomString);

		// But no '+' which is valid in base64 encoding
		$modifiedBase64String = str_replace('+', '.', $base64String);

		// Truncate String to the correct length
		return substr($modifiedBase64String, 0, $length);
	}
1227653
public function encodePassword($raw, $salt)
    {
        if ($this->isPasswordTooLong($raw)) {
            throw new BadCredentialsException('Invalid password.');
        }

        if (!\in_array($this->algorithm, hash_algos(), true)) {
            throw new \LogicException(sprintf('The algorithm "%s" is not supported.', $this->algorithm));
        }

        $digest = hash_pbkdf2($this->algorithm, $raw, $salt, $this->iterations, $this->length, true);

        return $this->encodeHashAsBase64 ? base64_encode($digest) : bin2hex($digest);
    }
968155
private function getRandomKey($length = self::DEFAULT_RANDOM_BYTE_LENGTH)
    {
        $key = '';
        $salt = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $saltLength = strlen($salt);

        // Build the random key.
        for ($i = 0; $i < $length; $i++) {
            $key .= $salt[mt_rand(0, $saltLength - 1)];
        }

        return $key;
    }
346826
public function generateRandomInt($min, $max)
    {
        try {
            return $this->driverInstance->generateRandomInt($min, $max);
        } catch (\Exception $e) {
            throw new CryptException($e->getMessage());
        }
    }
634449
public function salt(int $length = 32): string {
    try {
      return random_bytes($length);
    } catch(Exception $e) {
      throw new RuntimeException('Could not generate crytographically secure salt', 0, $e);
    }
  }
1123524
public function hash($password, $salt = null)
    {
        return password_hash($this->protect($password, $salt), $this->algorithm, $this->options);
    }
134865
public static function salts($length)
    {
        $chars = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'));
        $stringlength = count($chars); //Used Count because its array now
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $chars[rand(0, $stringlength - 1)];
        }

        return $randomString;
    }
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$');
    }