MSDN Library

CertHashStringCollection Class

Represents a collection of certificate hash strings.

Namespace: System.Net.Security
Assembly: System.Net.Primitives (in System.Net.Primitives.dll)

Syntax

public sealed class CertHashStringCollection : ICollection, IEnumerable, IList

Remarks

The CertHashStringCollection class is used to store a collection of hexadecimal strings, where each string represents the hash of a certificate. This collection is typically used in scenarios where you need to specify a set of trusted certificate hashes for secure communication.

The elements in the collection are treated as strings and are validated to ensure they represent valid hexadecimal values.

Members

Constructors

public CertHashStringCollection()

Initializes a new instance of the CertHashStringCollection class.

Properties

public int Count { get; }

Gets the number of certificate hash strings contained in the collection.

public bool IsFixedSize { get; }

Gets a value indicating whether the collection has a fixed size.

public bool IsReadOnly { get; }

Gets a value indicating whether the collection is read-only.

public bool IsSynchronized { get; }

Gets a value indicating whether access to the collection is synchronized (thread-safe).

public string this[int index] { get; set; }

Gets or sets the certificate hash string at the specified index.

public object SyncRoot { get; }

Gets an object that can be used to synchronize access to the collection.

Methods

public int Add(string value)

Adds a certificate hash string to the end of the collection.

public void AddRange(CertHashStringCollection collection)

Adds the certificate hash strings from another CertHashStringCollection to the end of the current collection.

public void Clear()

Removes all certificate hash strings from the collection.

public bool Contains(string value)

Determines whether the collection contains the specified certificate hash string.

Returns true if the certificate hash string is found; otherwise, false.

public void CopyTo(string[] array, int index)

Copies the entire collection to a compatible one-dimensional string array, starting at the specified index of the target array.

public IEnumerator GetEnumerator()

Returns an enumerator that iterates through the collection.

public int IndexOf(string value)

Searches for the specified certificate hash string and returns the zero-based index of the first occurrence within the entire collection.

Returns the zero-based index of the element to locate; -1 if the element is not found.

public void Insert(int index, string value)

Inserts a certificate hash string into the collection at the specified zero-based index.

public void Remove(string value)

Removes the first occurrence of a specific certificate hash string from the collection.

public void RemoveAt(int index)

Removes the certificate hash string at the specified zero-based index from the collection.

Example

C# Example

using System;
using System.Net.Security;

public class CertHashExample
{
    public static void Main(string[] args)
    {
        // Create a new collection of certificate hashes
        CertHashStringCollection trustedHashes = new CertHashStringCollection();

        // Add some valid certificate hash strings (example hashes)
        trustedHashes.Add("0a1b2c3d4e5f67890123456789abcdef");
        trustedHashes.Add("fedcba9876543210fedcba9876543210");

        // Check if a hash exists
        string searchHash = "0a1b2c3d4e5f67890123456789abcdef";
        if (trustedHashes.Contains(searchHash))
        {
            Console.WriteLine($"The hash '{searchHash}' is in the collection.");
        }

        // Get the number of hashes
        Console.WriteLine($"Number of trusted hashes: {trustedHashes.Count}");

        // Iterate through the collection
        Console.WriteLine("Trusted Hashes:");
        foreach (string hash in trustedHashes)
        {
            Console.WriteLine($"- {hash}");
        }

        // Remove a hash
        trustedHashes.Remove("fedcba9876543210fedcba9876543210");
        Console.WriteLine($"After removal, count is: {trustedHashes.Count}");
    }
}
Tags: System.Net.Security CertHashStringCollection Certificate Hash Security Collection .NET