I recently began learning C#. I tried to generate an NTLM hash in this language but I could't find a function to do this for me. In python 3.x I would import hashlib
and calculate it with hashlib.new("md4", "Hello, World!".encode("utf-16le"))
.
I searched through the Object Browser in C# but didn't find anything, the closest was a windows NTLM authentication class. I also searched Microsoft's docs and found hash calculators, but only for sha1 and md5.
Is there a way to calculate an NTLM hash in C#? Can you please show me an example of how to do it, and I would prefer a short method to keep it simple.
Thanks.
Code can be found at the end of the post here. It uses BC for the MD4, as most MD4 implementations have a way to avid weak keys. NTLM does not account weak keys, so you must be able to use them if they arise.
https://markgamache.blogspot.com/2013/01/ntlm-challenge-response-is-100-broken.html
You can create an extension to the existing cryptography providers using Reflection to call CNG for MD4 (something .Net should probably either do, or make much easier):
Once you've done that, a couple of helper extensions will let you use it easily (I modified this to create a singleton so it doesn't have to do the work of reflecting/creating every time you use it):
Now you just call the extension methods on some sample data:
I think you need to use the BouncyCastle to calculate the HASH, there is a .net porting that works quite well.
and here is all the step to calculate the NTLM hash: https://asecuritysite.com/encryption/lmhash
Here's a general solution to call CNG for any valid
ALG_ID
, based on the accepted answer above. Thank you NetMage!Create an instance with
algID = 0x8002
for MD4.