{"id":92941,"date":"2016-01-28T07:00:00","date_gmt":"2016-01-28T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=92941"},"modified":"2019-03-13T10:29:24","modified_gmt":"2019-03-13T17:29:24","slug":"20160128-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20160128-00\/?p=92941\/","title":{"rendered":"Why is getting the HP_HASHSIZE so weird?"},"content":{"rendered":"<p>A comment on <a HREF=\"https:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa379947(v=vs.85).aspx\">the documentation page for <code>Crypt&shy;Get&shy;Hash&shy;Param<\/code><\/a> notes that the &#8220;obvious&#8221; way to get the <code>HP_HASH&shy;SIZE<\/code> is incorrect. <\/p>\n<pre>\n<i>\/\/ Version 1: wrong.\nDWORD size = 0;\nif (CryptGetHashParam(hash, HP_HASHSIZE, nullptr, &amp;size, 0))<\/i> ...\n\n\/\/ Version 2: right.\nDWORD size;\nDWORD bufferSize = sizeof(size);\nif (CryptGetHashParam(hash, HP_HASHSIZE, &amp;size, &amp;bufferSize, 0))<\/i> ...\n<\/pre>\n<p>What&#8217;s going on here? I mean, the documentation says that if you want to get the size of a parameter, you pass <code>nullptr<\/code> for the buffer, and the <code>DWORD*<\/code> parameter gets the size of the buffer. So if I want to get the hash size, I should pass <code>nullptr<\/code> for the buffer, and the <code>DWORD*<\/code> parameter gets the size of the hash. But it doesn&#8217;t. It always returns 4. What&#8217;s going on? <\/p>\n<p>What&#8217;s going on is that you are working at the wrong level of indirection. The code in version 1 is not asking for the size of the hash. It&#8217;s asking for the size of the <code>HP_HASH&shy;SIZE<\/code>. In other words, you&#8217;re asking for the size of the <i>size<\/i>. Since <code>HP_HASH&shy;SIZE<\/code> is a <code>DWORD<\/code>, its size is 4. You then need to follow up with the code in version 2, which allocates a buffer of size 4 and asks for it to be filled in with the <code>HP_HASH&shy;SIZE<\/code>. <\/p>\n<p>A third way to get the size of the hash is to ignore <code>HP_HASH&shy;SIZE<\/code> completely and go straight for the <code>HP_HASH&shy;VAL<\/code>: <\/p>\n<pre>\n\/\/ Version 3: righter\nDWORD hashSize = 0;\nif (CryptGetHashParam(hash, HP_HASHVAL, nullptr, &amp;hashSize, 0))<\/i> ...\n<\/pre>\n<p>I don&#8217;t know why the crypto folks bothered to have a <code>HP_HASH&shy;SIZE<\/code> parameter. Adding it only created confusion. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Second-order logic.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-92941","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Second-order logic.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/92941","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=92941"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/92941\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=92941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=92941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=92941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}