如何计算 PHP 中 AMP 的 JavaScript 哈希值?

How to Calculating the JavaScript Hash for AMP in PHP?

Calculating the script hash 我们可以看到如何计算 AMP 情况下的 JavaScript 哈希:

const crypto = require('crypto');
const hash = crypto.createHash('sha384');

function generateCSPHash(script) {
  const data = hash.update(script, 'utf-8');
  return (
    'sha384-' +
    data
      .digest('base64')
      .replace(/=/g, '')
      .replace(/\+/g, '-')
      .replace(/\//g, '_')
  );
}

如何在 PHP 中执行相同的操作?以下似乎不起作用:

<?php
$hash = base64_encode( hash( 'SHA384', 'Give me my hash!', true ) );

代码引用自WordPress AMP插件

/**
 * Function to generate AMP scrip hash.
 *
 * @param string $script the script as a string to generate the hash.
 *
 * @return string hash generated from the script.
 */
function amp_generate_script_hash( $script ) {
    $sha384 = hash( 'sha384', $script, true );
    if ( false === $sha384 ) {
        return null;
    }
    $hash = str_replace(
        [ '+', '/', '=' ],
        [ '-', '_', '.' ],
        base64_encode( $sha384 )
    );
    return 'sha384-' . $hash;
}