Find a Certificate by Certificate Hash or Thumbprint - C#



private X509Certificate2 GetCertificateByHash(byte[] certHash)
{
    X509Certificate2 result = null;

    using (var certStore = new X509Store(StoreLocation.LocalMachine))
    {
        certStore.Open(OpenFlags.ReadOnly);

        var thumbprint = string.Join("", certHash.Select(a => string.Format("{0:X}", a).PadLeft(2, '0')));

        var certs = certStore.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);

        if (certs.Count > 0)
        {
            result = certs[0];
        }

        certStore.Close();
    }

    return result;
}