Represents a collection of certificate hash strings.
Namespace: System.Net.Security
Assembly: System.Net.Primitives (in System.Net.Primitives.dll)
public sealed class CertHashStringCollection : ICollection, IEnumerable, IList
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.
Initializes a new instance of the CertHashStringCollection class.
Gets the number of certificate hash strings contained in the collection.
Gets a value indicating whether the collection has a fixed size.
Gets a value indicating whether the collection is read-only.
Gets a value indicating whether access to the collection is synchronized (thread-safe).
Gets or sets the certificate hash string at the specified index.
Gets an object that can be used to synchronize access to the collection.
Adds a certificate hash string to the end of the collection.
Adds the certificate hash strings from another CertHashStringCollection to the end of the current collection.
Removes all certificate hash strings from the collection.
Determines whether the collection contains the specified certificate hash string.
Returns true if the certificate hash string is found; otherwise, false.
Copies the entire collection to a compatible one-dimensional string array, starting at the specified index of the target array.
array at which the copying begins.
Returns an enumerator that iterates through the collection.
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.
Inserts a certificate hash string into the collection at the specified zero-based index.
value should be inserted.
Removes the first occurrence of a specific certificate hash string from the collection.
Removes the certificate hash string at the specified zero-based index from the collection.
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}");
}
}