Add a new (pre-calculated) base64-encoded hash to the existing CSP

@param string $directive
@param string $hash
@param string $algorithm
@return self
973906
public function preHash(
        string $directive = 'script-src',
        string $hash = '',
        string $algorithm = 'sha384'
    ): self {
        $ruleKeys = \array_keys($this->policies);
        if (\in_array($directive, $ruleKeys)) {
            $this->policies[$directive]['hashes'] []= [
                $algorithm => $hash
            ];
        }
        return $this;
    }
531815
public function setSignature($algorithm)
    {
        /*
         * If we don't support passed algprithm, leave old one.
         */
        switch ($algorithm) {
            case 'md5':
                $this->signatureAlgorithm = Phar::MD5;
                break;
            case 'sha1':
                $this->signatureAlgorithm = Phar::SHA1;
                break;
            case 'sha256':
                $this->signatureAlgorithm = Phar::SHA256;
                break;
            case 'sha512':
                $this->signatureAlgorithm = Phar::SHA512;
                break;
            case 'openssl':
                $this->signatureAlgorithm = Phar::OPENSSL;
                break;
            default:
                break;
        }
    }
997245
public static function Ds(int $keyTag, int $algorithm, string $digest, int $digestType = DS::DIGEST_SHA1)
    {
        $rdata = new DS();
        $rdata->setKeyTag($keyTag);
        $rdata->setAlgorithm($algorithm);
        $rdata->setDigest($digest);
        $rdata->setDigestType($digestType);

        return $rdata;
    }
442309
private function canKeyBeUsedWithAlgorithm($algorithm, JWKInterface $key)
    {
        if (null === $algorithm) {
            return 0;
        }
        if ($key->has('alg')) {
            return $algorithm === $key->get('alg') ? 1 : false;
        }

        return 0;
    }
68752
public static String createHash(final InputStream inputStream, final String algorithm) {
        checkNotNull("inputStream", inputStream);
        checkNotNull("algorithm", algorithm);
        try {
            final MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
            try (final BufferedInputStream in = new BufferedInputStream(inputStream)) {
                final byte[] buf = new byte[1024];
                int count = 0;
                while ((count = in.read(buf)) > -1) {
                    messageDigest.update(buf, 0, count);
                }
            }
            return encodeHex(messageDigest.digest());
        } catch (final NoSuchAlgorithmException | IOException ex) {
            throw new RuntimeException(ex);
        }
    }
532743
public function setHash(string $hash): self
    {
        $this->hash = (string) hex2bin($hash);
        return $this;
    }
300657
public static function encrypt(RSAKey $key, $plaintext, $hash_algorithm)
    {
        /*
         * @var Hash
         */
        $hash = Hash::$hash_algorithm();
        $length = $key->getModulusLength() - 2 * $hash->getLength() - 2;
        Assertion::greaterThan($length, 0);
        $plaintext = str_split($plaintext, $length);
        $ciphertext = '';
        foreach ($plaintext as $m) {
            $ciphertext .= self::encryptRSAESOAEP($key, $m, $hash);
        }

        return $ciphertext;
    }
636351
public function addPGPSignature($body, $algorithm = null)
    {
        $this->signature = $body;
        $this->micalg    = $algorithm;

        // Reset Content-Type to be overwritten with valid boundary
        unset($this->headers['Content-Type']);
        unset($this->headers['Content-Transfer-Encoding']);
    }
758538
function hash(css, algorithm, trim) {
    return crypto
        .createHash(algorithm)
        .update(css)
        .digest("hex")
        .substr(0, trim);
}
584875
private function sign($data, $algorithm)
    {
        if ($algorithm === 'HmacSHA1') {
            $hash = 'sha1';
        } else if ($algorithm === 'HmacSHA256') {
            $hash = 'sha256';
        } else {
            throw new \Exception("Non-supported signing method specified");
        }

        return base64_encode(hash_hmac($hash, $data, $this->config['secret_key'], true));
    }
594666
public static String digest(byte[] self, String algorithm) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance(algorithm);
        md.update(ByteBuffer.wrap(self));

        return encodeHex(md.digest()).toString();
    }
1246671
public static function hash($password, $algorithm = PasswordFile::ALG_MD5, array $options = null)
    {
        if ($options === null) {
            $options = [];
        }
        switch ($algorithm) {
            case PasswordFile::ALG_MD5:
                return APR1::hash($password);
            case PasswordFile::ALG_BCRYPT:
                $cost = isset($options['cost']) ? $options['cost'] : null;
                return BCrypt::hash($password, $cost);
            case PasswordFile::ALG_SHA1:
                return self::sha1($password);
            case PasswordFile::ALG_CRYPT:
                return self::cryptHash($password);
            case PasswordFile::ALG_PLAIN:
                return $password;
        }
        return null;
    }
845252
public function getHmac(string $key, string $algorithm = 'sha256', bool $raw = false): string
	{
		return hash_hmac_file($algorithm, $this->getPathname(), $key, $raw);
	}
27028
public function addDirective($directive)
    {
        if (isset($this->directives[$i = $directive->id->toString()])) {
            throw new HTMLPurifier_ConfigSchema_Exception("Cannot redefine directive '$i'");
        }
        $this->directives[$i] = $directive;
    }
737579
public function setHashAlgorithm($hash)
    {
        switch ($hash) {
            case 'rsa-sha1':
                $this->hashAlgorithm = 'rsa-sha1';
                break;
            case 'rsa-sha256':
                $this->hashAlgorithm = 'rsa-sha256';
                if (!defined('OPENSSL_ALGO_SHA256')) {
                    throw new Swift_SwiftException('Unable to set sha256 as it is not supported by OpenSSL.');
                }
                break;
            default:
                throw new Swift_SwiftException('Unable to set the hash algorithm, must be one of rsa-sha1 or rsa-sha256 (%s given).', $hash);
        }

        return $this;
    }
228192
private function cspDoHash(
        $string,
        $algo = null,
        $isFile = null
    ) {
        Types::assert(['string' => [$string], '?string' => [$algo]]);

        if ( ! isset($algo))
        {
            $algo = 'sha256';
        }

        if ( ! isset($isFile))
        {
            $isFile = false;
        }

        if ( ! $isFile)
        {
            $hash = hash($algo, $string, true);
        }
        else
        {
            if (file_exists($string))
            {
                $hash = hash_file($algo, $string, true);
            }
            else
            {
                $this->addError(
                    __FUNCTION__.': The specified file '
                    . "<strong>'$string'</strong>, does not exist"
                );

                return '';
            }
        }

        return base64_encode($hash);
    }
121104
public static function hash($value, string $algorithm = 'fnv1a32'): string
    {
        $type = gettype($value);

        switch ($type) {
            case 'object':
                if (Validate::isEquatable($value)) {
                    $string = sprintf('e_%s', $value->hashValue());
                } else {
                    $string = sprintf('o_%s', spl_object_hash($value));
                }
                break;
            case 'string':
                $string = sprintf('s_%s', $value);
                break;
            case 'integer':
                $string = sprintf('i_%d', $value);
                break;
            case 'double':
                $string = sprintf('f_%.14F', $value);
                break;
            case 'boolean':
                $string = sprintf('b_%d', (int) $value);
                break;
            case 'resource':
                $string = sprintf('r_%d', (int) $value);
                break;
            case 'array':
                $string = sprintf('a_%s', serialize($value));
                break;
            default:
                $string = '0';
                break;
        }

        return hash($algorithm, $string);
    }