Zangief Ief wrote:
> So if I have well understood, is that correct if I use unpack('N*') like 
> this?
> 
>>> message = "A message"
> => "A message"
>>> message.unpack('N*').join.to_i.to_s(2)
> => "1001011110100010100001010001010100101100001110000101010101100111"

No. The message itself isn't treated as a 64-bit integer, only the 
*length* of the message is a 64-bit integer, which is *appended* to the 
message. In this case the length is 9*8 = 72 bits, so you need 
\x00\x00\x00\x00\x00\x00\x00\x48

Anyway, I don't know why you are going to binary. You just want a String 
of bytes. Don't worry about the order of bits-within-bytes; it will be 
correct, trust me :-)

Of course, if you are trying to write an SHA1 implementation which 
properly handles input streams which are not a multiple of 8 bits long 
(as many don't), then you have a little bit more work to do. But not 
very much, since the padding operating makes it into whole bytes anyway.

e.g. if your input is
10101010101

this becomes

10101010 10110000 00000000 00000000 ...
            ^^^^^ ^^^^^^^^ ^^^^^^^^
                  padding

and hence your string just needs to be \xAA\xB0\x00\x00 ..... padded to 
the correct length. And the length is \x00\x00\x00\x00\x00\x00\x00\x0b, 
i.e. 11 bits.

However if your SHA1 input is just a stream of bytes, as is normally the 
case, then the padding is simply \x80\x00\x00\x00\x00 ... etc

Anyway, this is no longer a Ruby question, this is about reading the 
SHA1 pseudocode correctly. But you could always submit it as a Ruby Quiz 
idea :-)
-- 
Posted via http://www.ruby-forum.com/.