From: Brian Candler Date: 2008-10-16T22:25:39+09:00 Subject: Re: Big endian convention in Ruby 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*") BTW, I presume you're doing this as an academic exercise. After all, there's already: require 'digest/sha1' puts Digest::SHA1.hexdigest("hello world") HTH, Brian. -- Posted via http://www.ruby-forum.com/.