On 16.10.2008 15:25, Brian Candler wrote:
> Zangief Ief wrote:
>> Thanks you for you answer.
>> Actually I would like to rewrite the SHA1 algorithm 
>> (http://en.wikipedia.org/wiki/SHA_hash_functions#SHA-1_pseudocode) in a 
>> pure ruby implementation. And in this way, I would need the ability to 
>> accomplish the "Pre-processing" step by converting the input as a 64-bit 
>> big-endian integer. I believe that's could be more simple to do in Ruby 
>> then in an other language such as in C. But I am not really sure about 
>> the way to do so.
> 
> ri String#unpack
> 
> Unfortunately, the q/Q conversion character seems to use native ordering 
> and I don't think there's a network-order equivalent:
> 
> irb(main):002:0> "\000\000\000\000\000\000\000\001".unpack("Q")
> => [72057594037927936]
> 
> If all you're concerned about is this step:
> 
> "append length of message (before pre-processing), in bits, as 64-bit 
> big-endian integer"
> 
> then you could do it by converting to hex first:
> 
>   buff << [("%016X" % len)].pack("H*")

Or use "N" and combine, e.g.

irb(main):007:0> s = "\000\000\000\000\000\000\000\001"
=> "\000\000\000\000\000\000\000\001"
irb(main):008:0> r=[];s.unpack("N*").each_slice(2) {|hi,lo| r << (hi << 
32 | lo)}; r
=> [1]

Kind regards

	robert