Don’t be helpless: You can find information, too, if you try


Here's a question that floated past my view:

Anybody know if there exists a library for computing MD5 hashes from unmanaged code? MSDN has information about .NET classes, but nothing about the unmanaged side.

Hm, let's see.

C:\Windows SDK\Include> grep MD5 *.h
wincrypt.h:#define ALG_SID_MD5                     3
wincrypt.h:#define ALG_SID_SSL3SHAMD5              8
wincrypt.h:#define CALG_MD5                (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_MD5)
wincrypt.h:#define CALG_HUGHES_MD5         (ALG_CLASS_KEY_EXCHANGE|ALG_TYPE_ANY|ALG_SID_MD5)
wincrypt.h:#define CALG_SSL3_SHAMD5        (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_SSL3SHAMD5)
wincrypt.h:#define KP_PRECOMP_MD5          24
wincrypt.h:#define szOID_RSA_MD5RSA        "1.2.840.113549.1.1.4"
wincrypt.h:#define szOID_RSA_MD5           "1.2.840.113549.2.5"

Wow, those hits sure look promising. Perhaps a search on Windows Live or Google¹ will turn up something. Oh hey, how about that, sample code.

Exercise: Use this exact same technique to answer this commenter's question on how the C# ++ operator works. Hint: Since this is a question about the C# language, the C# language specification would be a good starting point.

Exercise: Use this technique to answer this commenter's question on how to connect to a process as a debugger.

Footnotes

¹ OH MY GOD I LINKED TO GOOGLE.

Comments (29)
  1. John Topley says:

    Shouldn’t that be:

    findstr /c:"MD5" /s *.h

    –This is Windows after all! ;-)

  2. Steve Thresher says:

    Have you tried using MSDN to find Win32 API help lately? You have to wade through page after page of C# info before you get to what you really want. I know MSDN is not your area but the original poster had at least tried to find information and it seems unfair to slate them for not knowing the exact place to look.

    How about a small article on the resources used by ‘Guru Raymond’.

  3. CGomez says:

    The Win32 API has been renamed the Windows API (which I just discovered today via a search).  I searched for that and found this:

    http://msdn.microsoft.com/en-us/library/cc433218(VS.85).aspx

    After that you have the tree on the left that can take you to the reference.  Seems better organized than ever.

  4. James Schend says:

    To be fair, it’s really hard to search for things like "C# ++" in any web search engine, since they don’t typically recognize "++" as a word, and might trim off the trailing pound sign, and only actually search for "C". This sometimes applies even when you’re searching the language specification directly.

    I had a similar problem recently when searching for what "$" did in some Javascript code I was maintaining. According to the ECMAScript specification (the first place I looked), $ is reserved for "mechanically-generated code" whatever that means. Turns out, in reality, it’s commonly used in libraries like mootools.js, prototype.js to mean "getElementById." Sometimes. Some libraries overload $ to do something entirely different. jquery.js uses it to do something entirely different, thus making it impossible to use jquery.js and prototype.js on the same site.

    All of these libraries are probably in violation of the ECMAScript language spec, which makes me laugh since they’re usually maintained by the most hardcore "web standards are God! Any violation from the standards deserves DEATH!"-type of people.

  5. Steve Thresher says:

    Okay,

    Windows API  

    ..Windows API Reference

    ….Functions in Alphabetical Order

    ……C Functions

    CryptAcquireContext() is not there. How is that better organized?

  6. James, at one point that was true, but The Google now does just fine with both C++ and C#.

    Raymond, I love your preemptive angry footnotes.  don’t ever change.

  7. Warren says:

    Steve, have -you- tried MSDN lately?  The search received a major upgrade a couple of weeks ago.

    For this MD5 hash example, if you go to http://msdn.microsoft.com/ , then type "create md5 hash" into the search box then filter by "Win32 & COM development", and then filter further by "Windows Security", it becomes pretty easy to find the answer you’re looking for.  

  8. James Schend says:

    David, thanks for the tip, but it mostly annoys me when I’m already at the language spec and their search engine won’t find things like "++" or "%" or what-not. Adobe’s Actionscript documentation is an example of this, and pretty much a good example of crappy documentation everywhere. I particularly enjoy the AS2 samples that don’t work when copied from the website and pasted into Flash CS3.

    But this is the web world, where everything sucks.

  9. Raymond, is it ok if I read your posts simply for the humor?  :)

    Thanks once again for the laugh!

  10. James: if you read the JQuery documentation, it provides the capability to  interoperate with other libraries that use "$" via thejQuery.noConflict() method. Locating said documentation is left as an exercise for the reader ;-)

  11. mikeb says:

    For all the quirks that search engines may have, it’s pretty amazing that people often don’t even seem to try.  We’re in an age that was literally science fiction a few years ago. I can go to any computer and type in any question (more or less) and generally get good answers.  I’m still amazed by this every time I stop to think about it.

  12. John says:

    It’s not so much that people don’t TRY to search (although sometimes this is the case); it’s more that people don’t know HOW to search.

    If you don’t know jack about CryptoAPI, you are going to be hard-pressed to find any useful information about calculating MD5 hashes in unmanaged code.  Go to MSDN and search for MD5-related things; every result is about the .NET MD5 class.  Buried deep on the second page are a few hits that will point you in the right direction.  The problem with searching documentation is that if you don’t have the function name it is going to be hard to find what you are looking for.  Even Google struggles in this area.  Inevitably you end up at a code example website that does what you are trying to do; now you have the function name and can get to the documentation with ease.

  13. Alexandre Grigoriev says:

    MSDN search is so messed up. For some reason they want to think that an underscore is word separator. Good luck trying to search for an underscore-separated #define’d macro. That said, local search in WDK doc viewer is so incredibly slow, slower than online.

  14. Radu Ilie says:

    There is also another library available on Windows: cryptdll.dll. The Visual Studio 2005 documentation says that is part of Windows since Windows 2000. It contains all you need for MD5 hashes: MD5Init, MD5Update and MD5Final.

  15. Alexandre Grigoriev says:

    MSDN has changed for a little better. Underscore now means the words go immediately one after another. It still thinks I’m searching for separate Device Object words, when I want DEVICE_OBJECT, but at least Device and Object has to come together in the test. And it now prioritizes search result more or less reasonably.

  16. One thing I’d love to know a searchable name for is the . and \? path name prefixes that show up in various places on MSDN. Google refuses to search for such weird strings, and MSDN search gives millions upon millions of questionable "hits". I suspect it considers every page it knows to "match" . as a search string.

    The documentation for CreateFile tells of various things that one can put after ., but makes no claim of being comprehensive. For example, .mailslotfoo is not mentioned, presumably because mailslots cannot or should not be manipulated using the file API. But clearly there is some general structured namespace involved here – raising such questions as what else it is used for and what are its rules? (E.g., if I’m writing a function to check for "moral equivalence" of pathnames, in which parts of the namespace is . and .. compression valid?)

    Now, if only I knew of useful search terms for . and \? …

  17. hexatron says:

    I had to modify some VB in excel (is that VBA?) today. It was all done with google: VB string concatenation — whaddya know, it’s an ampersand!

    google: VB if else — found Then and End If

    (I did remember For…Next)

    In only a few hours, I had done fifteen minutes of work. Yay google.

  18. Ulric says:

    Yeah, most people on Windows, even — or especially — experienced developers, don’t have the reflex of grepping in the system header files.  

    Probably because help view searcj is just there, and easier to search, so we’re not connected to the header files.  Also, historically the header files have been in a folder very deep, with spaces in them.  Just plain annoying to get at.

    The only reason to grep is to find the value of an ID, like a windows message.

  19. Worf says:

    Funny enough, I’m sure if some google’d for MD5 calculations, they’d run into the RFC about it, which includes a nice C implementation of MD5. A similar one exists for SHA as well.

    And sometimes, it helps to use Wikipedia too.

    (I had to learn how to implement s crypto service provider, and learn CryptoAPI to test my CSP)

  20. Koro says:

    Ah, my favorite game, "grep the SDK headers to find the value of a constant".

    A game which I used to play a lot when having to do P/Invoke from C# at work.

    Note that *some* pages in the PSDK docs will give you the constant value next to it (i.e. the SystemParametersInfo one, although in VS2008 it does not do it anymore), and some won’t.

  21. RoaldFalcon says:

    "’grep’ is not recognized as an internal or external command,

    operable program or batch file."

    It seems I really am helpless.

  22. It seems I really am helpless.

    Apparently so.

    However, a non-helpless person would probably be bold enough to try using other file searching tools at their disposals (even that built-in search feature with the annoying doggy animation can search for contents in text files for you, IIRC), or at least attempt to investigate how to acquire this wonderful utility of which Raymond speaks. Google for "grep for Windows" and several useful hits appear on the first page, along with a helpful suggestion to refine the search to "unix utilities for Windows".

  23. dog sux says:

    The search dog is the most useless search feature EVER constructed. It doesn’t find anything useful! I had to create a search program myself. And have turned off indexing which make windows slower.

  24. Simon says:

    @dog_sux:

    There have been a number of upgrades to the Windows search facility since XP came out.  The latest version is Windows Search 4.0; you can get it <a href="http://support.microsoft.com/kb/940157/en-us">here</a&gt;.  It is a big improvement.

    Incidentally, you do realise that you can turn the dog off?  Two or three clicks IIRC…

  25. Yuhong Bao says:

    "The documentation for CreateFile tells of various things that one can put after ., but makes no claim of being comprehensive. For example, .mailslotfoo is not mentioned, presumably because mailslots cannot or should not be manipulated using the file API. But clearly there is some general structured namespace involved here – raising such questions as what else it is used for and what are its rules?"

    Clue: Look at the native APIs and the paths and namespace it uses. For example, if you, for example, call CreateFile with .ddd, it calls NtCreateFile with ??ddd. There is also Device, for example.

  26. Thanks, Yuhong Bao. I can now describe my ignorance in more searchable terms.

  27. Yuhong Bao says:

    "If you don’t know jack about CryptoAPI, you are going to be hard-pressed to find any useful information about calculating MD5 hashes in unmanaged code."

    Similarly, you aren’t going to answer Henning Makholm’s question if you don’t know jack about the Native API.

    BTW, WinObj lets you browse the native namespace.

  28. s/grep/findstr/ if you’re in a hurry.

  29. Jay Lauffer says:

    Searching the Headers is a good start since it will likely turn up an enumeration, does anyone remember before internet when you had to read the compiled help file or a book?  If you were lucky you could fidonet a sample or something back when they had bulletin boards.

    As far as Jack of the CryptoAPI I remember having one of the first betas mailed to me along with the nifty non-export warning.  Good times then, my son was still an infant.

    I’m with you Raymond, people stop and spend the extra fifteen minutes to try to learn something.  It really helps in the office environment when I’m not being interrupted by a question that might have required 30 minutes of research/discovery.  Instead of a Senior Software Engineer I’m becoming a Programming Instructor with much of my work becoming me explaining how I’ve learned to navigate documentation to get answers for things I don’t specifically know but that I understand exist because I fathom the capabilities of the hardware, the division of the modules (physically, logically), often times I discover things this is some of the joy of learning.  (Anyhow sorry for the long comment, it’s just those questions that I can answer in 30 seconds that someone could discover in about ten minutes that distract me and drive me nuts throughout my day).

Comments are closed.

Skip to main content