Пример #1
0
        public StyleCspDirective AddHash(CspHashAlgorithm algorithm, string base64Nonce)
        {
            if (base64Nonce == null)
            {
                throw new ArgumentNullException(nameof(base64Nonce));
            }

            return(this.AddSource($"{algorithm.ToString().ToLower()}-{base64Nonce}"));
        }
Пример #2
0
    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)));
    }
Пример #3
0
    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}'"));
    }
Пример #4
0
        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}'.");
        }
    }
Пример #6
0
    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)));
    }
Пример #7
0
 public StyleCspDirective AddHash(CspHashAlgorithm algorithm, byte[] hash)
 {
     return(this.AddHash(algorithm, Convert.ToBase64String(hash)));
 }
Пример #8
0
 public ScriptCspDirective AddHashOf(string content, CspHashAlgorithm algorithm = CspHashAlgorithm.Sha256) => this.AddHashOf(content, Encoding.UTF8, algorithm);