Recently in a blog post, we discussed, how to detect whether an assembly is digitally signed using the C# programming language and System.Management.Automation.dll from Microsoft. That was one way to get the status of the digital signing.

 

Today we will learn another method to detect the same without using any additional dll references in our project. I hope that, it will much more interesting to learn.

 

How to detect whether binaries are digitally signed (www.kunal-chowdhury.com)

 

In last post we learnt how to use the System.Management.Automation.dll to add PowerShell script to get the digital signature certificate details. The said dll is of approx. 2MB in size, which will unnecessary increase the overall size of your application just to grab the digital signing status.

 

So, what to do to overcome this? If you just need to check whether the file is digitally signed or not, you can use the API 'X509Certificate.CreateFromSignedFile' from the 'System.Security.Cryptography.X509Certificates' namespace. If you are able to create the certificate for the specified file from code, the file contains a digital certificate. In other case, it will throw exception. Handle that exception and return to user confirming 'No Certificate' available for that file.

 

 

Here's the code for you to use:

 

private bool IsDigitallySigned(string filePath)
{
    try
    {
        var digitalCertificate = X509Certificate.CreateFromSignedFile(filePath);
        return true;
    }
    catch (CryptographicException)
    {
        return false;
    }
}

 

Please note that the above process is simple enough to check whether the assembly is signed or not, but to grab more details from the certificate, you will need to use the System.Management.Automation.dll assembly reference.

 

If you are looking to know the way to grab more details out of the certificate using C# code, checkout my next post. Do let me know, if you have any queries. Stay tuned for upcoming articles in this blog space. Have a great day!

 

 

Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.