dvInlineScriptHashes function

List<String> dvInlineScriptHashes(
  1. String html
)

'sha256-…' for each distinct inline script in html that a browser would run, in page order. External scripts (src=) are left to the policy's own sources.

Implementation

List<String> dvInlineScriptHashes(String html) {
  final List<String> hashes = <String>[];
  for (final RegExpMatch match in _script.allMatches(html)) {
    final String attributes = match.group(1) ?? '';
    if (_src.hasMatch(' $attributes')) continue;
    final RegExpMatch? type = _type.firstMatch(' $attributes');
    if (!_runs(type == null ? null : (type.group(1) ?? type.group(2) ?? type.group(3)))) continue;
    final String digest = base64.encode(sha256.convert(utf8.encode(match.group(2) ?? '')).bytes);
    final String source = "'sha256-$digest'";
    if (!hashes.contains(source)) hashes.add(source);
  }
  return hashes;
}