title: Implementing SHA-256 in C++
body: <p>I am trying to implement SHA-256 in MSVC++. I am nearly there, except for extending the first 16 words of the message schedule array into the remaining 48. I have identified my problem to be at this point because it exactly matches the <a href="http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA256.pdf" rel="nofollow">example from nist.gov</a> until round 17 of the compression algorithm. My code for the message schedule is as follows:     </p>
  
  <pre><code>//Extend first 16 words into the remaining 48 words of the message schedule array:
  for (int k = 16; k &lt; 64; k++)
  {
      bitset&lt;32&gt; s0 = rotr(W[k - 15], 7) ^= rotr(W[k - 15], 18) ^= (W[k - 15] &gt;&gt; 3);
      bitset&lt;32&gt; s1 = rotr(W[k - 2], 17) ^= rotr(W[k - 2], 19) ^= (W[k - 2] &gt;&gt; 10);
      W[k] = add(add(W[k - 16], s0), add(W[i - 7], s1));
  }
  
  bitset&lt;32&gt; add(bitset&lt;32&gt; a, bitset&lt;32&gt; b)
  {
      unsigned long c = a.to_ulong();
      unsigned long d = b.to_ulong();
      return bitset&lt;32&gt;((c + d) % 4294967296);
  }
  
  bitset&lt;32&gt; rotr(bitset&lt;32&gt; b, int num)
  {
      int temp = (int)b.to_ulong();
      temp = _rotr(temp, num);
      return bitset&lt;32&gt; (temp);
  }
  </code></pre>
  
  <p>Where W[0..15] is a copy of the padded message (which matches the example).
  Does anyone see a problem? The full code is <a href="https://docs.google.com/document/d/1L4BpzmpmzLQsKx1dGf8DfM3RwFXq5tHqtf4ExADvPGM/pub" rel="nofollow">here.</a> It's about 170 lines.</p>