I need some help with hash value calculation. I am a bit confused by this statement that I found on w3 website on calculation of a DigestValue for xmldsig ( http://www.w3.org/TR/2002/REC-xmlenc-core-20021210/Overview.html#sha256 ):

A SHA-256 digest is a 256-bit string. The content of the DigestValue element shall be the base64 encoding of this bit string viewed as a 32-octet octet stream.

Does that mean that the sha256 hash value has to be converted to a 32-bit string before encoding it to base64? If yes, could someone please tell me how this can be done? I'm a kind of a n00b, but I haven't found info on how to do that on the internet.

  • 1
    $\begingroup$ I think this is just a weird formulation for "SHA256 outputs a 32 bytes/256 bits which you should then encode with BASE64". $\endgroup$ – CodesInChaos Jan 16 '15 at 14:22

No, you don't have to convert to a 32-bit string.

A "32-octet octet stream" means an octet stream with a length of 32 octets. An octet stream is also known as a byte array. In older times bytes could have different lengths (7 or 9 byte lengths have existed). An octet is always 8 bits; nowadays octets and bytes should be considered synonyms.

So basically they are saying: treat the bit string as a stream of octets of the same length (as $32*8=256$), then base-64 encode it. Now most crypto libraries already treats the output of SHA-256 as byte array (octet stream). So you just are left with encoding the output to base 64.

One small warning: some crypto libraries already contain encoding internally. You will need to explicitly choose raw byte output for those libraries, unless you can specify base64, of course.

| improve this answer | |
  • $\begingroup$ Note that the base64 encoding of 32 bytes will be $\lfloor\frac{32}{3}\rfloor\cdot 4 + 4 = 44$ characters, of which the last is an = sign (as we miss one byte when grouping into groups of 3 bytes). $\endgroup$ – Henno Brandsma Jan 16 '15 at 18:49
  • $\begingroup$ I guess that should be $\lceil\frac{32}{3}\rceil\cdot 4 = 44$ as, for 3 bytes, base64 only uses 4 characters. But that doesn't matter for this result. Thanks for the additional info @HennoBrandsma $\endgroup$ – Maarten Bodewes Jan 16 '15 at 19:05
  • $\begingroup$ If this solves the question then don't forget to accept! $\endgroup$ – Maarten Bodewes Jan 27 '15 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.