public StyleCspDirective AddHash(CspHashAlgorithm algorithm, string base64Nonce) { if (base64Nonce == null) { throw new ArgumentNullException(nameof(base64Nonce)); } return(this.AddSource($"{algorithm.ToString().ToLower()}-{base64Nonce}")); }
public ScriptCspDirective AddHashOf(byte[] content, CspHashAlgorithm algorithm = CspHashAlgorithm.Sha256) { if (content == null) { throw new ArgumentNullException(nameof(content)); } using (var hashAlgorithm = algorithm.CreateHashAlgorithm()) return(this.AddHash(algorithm, hashAlgorithm.ComputeHash(content))); }
public ScriptCspDirective AddHash(CspHashAlgorithm algorithm, string base64hash) { if (base64hash == null) { throw new ArgumentNullException(nameof(base64hash)); } string algorithmName = algorithm.ToString().ToLowerInvariant(); return(this.AddSource($"'{algorithmName}-{base64hash}'")); }
public CspDirective AddHash(CspHashAlgorithm algorithm, byte[] hash) { if (hash == null) { throw new ArgumentNullException(nameof(hash)); } string algorithmName = algorithm.ToString().ToLowerInvariant(); string base64 = Convert.ToBase64String(hash); return(this.AddSource($"'{algorithmName}-{base64}'")); }
public static HashAlgorithm CreateHashAlgorithm(this CspHashAlgorithm algorithm) { switch (algorithm) { case CspHashAlgorithm.Sha256: return(SHA256.Create()); case CspHashAlgorithm.Sha384: return(SHA384.Create()); case CspHashAlgorithm.Sha512: return(SHA512.Create()); default: throw new NotSupportedException($"Could not create HashAlgorithm from '{algorithm}'."); } }
public ScriptCspDirective AddHashOf(string content, Encoding encoding, CspHashAlgorithm algorithm = CspHashAlgorithm.Sha256) { if (content == null) { throw new ArgumentNullException(nameof(content)); } if (encoding == null) { throw new ArgumentNullException(nameof(encoding)); } byte[] rawContent = encoding.GetBytes(content); using (var hashAlgorithm = algorithm.CreateHashAlgorithm()) return(this.AddHash(algorithm, hashAlgorithm.ComputeHash(rawContent))); }
public StyleCspDirective AddHash(CspHashAlgorithm algorithm, byte[] hash) { return(this.AddHash(algorithm, Convert.ToBase64String(hash))); }
public ScriptCspDirective AddHashOf(string content, CspHashAlgorithm algorithm = CspHashAlgorithm.Sha256) => this.AddHashOf(content, Encoding.UTF8, algorithm);