From: Ashrith Barthur Date: 2008-10-22T22:36:50+09:00 Subject: Re: Big endian convention in Ruby Brian Candler wrote: > Just to make this clearer: the padding operation just pads the message > up to a multiple of 64 bytes (512 bits), where the last block consists > of 56 bytes (448 bits) followed by 8 bytes of message length. > > So assuming your message consists only of whole bytes, as your example > implied, then I believe the padding operation is simply this: > > message = "A message" > bits = message.size * 8 > message << "\x80" > message << "\x00" while (message.size & 63) != 56 > message << [("%016X" % bits)].pack("H*") > > Now your message is exactly n * 64 bytes long, and you can proceed. Firstly, forgive me for continuing the topic of SHA-1 here.. but I found that this is the relevant to what I am doing right now and hence wanted to post here. Brian Candler, I would like to say that my implementation of SHA-1 is almost on the same lines as you have explained. I have also coded it so that I handed in hex. Let me list down the requirements of SHA-1 implementations followed by where we might have an issue while using ruby. 1. There is a bit wise operation that is required between 2 Hex values and this will only occur if both of them are Integer-Hex or Integer anything. When you use unpack in ruby to get a string into its hex values, Ruby still thinks that it is a string but only in its hex values. For this to undergo bitwise operation. You will have to explicitly convert this to an Integer. For example lets say the string is 'abc' a gives 61 when unpacked in hex. so now. if the array holding this is messageHex and the position of a is [0] then we will have to explicitly say messageHex[0].hex.to_i this will ensure that it is a integer in hex. Next thing.. appending strings '0x80' or '0x00' is felt not to be appropriate by me because.. if you were to use 0x80 or 0x00 then ruby thinks that its an integer already and you dont need to do any explicit type casting. Also ruby does not explicitly give you a value in hex if you do any mathematical or bitwise operation in hex, it always defaults to dec. These were some of the issues I faced while implementing SHA-1. Please do let me know if there are any workarounds or easier way to implement or typecast the hex values. Also is there an default value for ruby to understand that any unpacking of a string to hex will tell ruby to take them as a integer value directly. Thanks Ashrith -- Posted via http://www.ruby-forum.com/.