hashScripts function

List<CspHash> hashScripts({
  1. required File htmlFile,
  2. Hash hashType = sha256,
  3. HashMode hashMode = HashMode.script,
})

Hashes all the scripts in the html file Returns a list of CspHash If there are no scripts, it returns an empty list hashType is the type of hash to use (sha256, sha384, sha512), defaults to sha256 hashMode is the mode to use (script, style), defaults to script htmlFile is the html file to hash Ignores scripts with a nonce attribute

Example:

<script src="main.dart" nonce="abc123"></script> // will be ignored

Example:

<script src="main.dart"></script> // will be hashed
final htmlFile = File('index.html');
final hashes = hashScripts(hashType: sha256, htmlFile: htmlFile);

Implementation

List<CspHash> hashScripts({
  required File htmlFile,
  Hash hashType = sha256,
  HashMode hashMode = HashMode.script,
}) {
  assert(hashType == sha256 || hashType == sha384 || hashType == sha512);
  // getting all the scripts from the html file
  final scripts = getScripts(htmlFile, hashMode);
  if (scripts.isEmpty) {
    return [];
  }

  // hashing the scripts
  return hasher(scripts, hashType, hashMode);
}