Ashrith said:
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.
"Integer-Hex" doesn't really mean anything.
The specification says it works on 32-bit unsigned integer values, and
that each 64-byte block of source data is treated as 16 x 32-bit words.
You can get this via
# data is a 64-byte string
w = data.unpack("N16")
# now w is an array of 16 Integers
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.
String#hex will give you an Integer directly; the to_i is superfluous.
But in any case, the conversion into hex-ascii in the first place is
superfluous. Unpack directly to Integers, as shown above.
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.
Sorry, but I am unable to make any sense of that sentence at all. The
input data to SHA1 is an arbitary-sized string of bytes (*); the padding
algorithm requires you to add more bytes (*) to the end, to achieve
alignment into 64-byte blocks. So adding padding bytes is exactly what
is required.
(*) actually an arbitary-sized string of *bits*, but most
implementations assume that it's a whole number of bytes, i.e. n*8 bits.
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.
I think you may have lost the distinction between a number, and its
external representation.
Doing a bitwise operation "in hex" or "in decimal" doesn't make any
sense. The number is stored internally in binary - this is a digital
computer, after all - and the bitwise operations are done on those bits.
It is only converted into a hex or decimal representation at the point
where you input or output the number.
a = 20
a.to_s # converts to string "20"
a.to_s(16) # converts to string "14"
Anyway, maybe you would like to submit this as a ruby quiz, as you'd
probably get some good implementations to look at.
Brian.