So I am doing something that requires me to manipulate things on the bit
level, and am having a bit of a headache with learning to use pack and
unpack to accomplish my goal. I thought I came one step closer with
this, but was disappointed to see it thinks the result is 255 bits since
I think it should be 256. What gives?
hash = OpenSSL::Digest::SHA256.new
test_hash_value = hash << "anything"
puts test_hash_value.to_s.to_i(16).to_s(2).length
=> 255
shouldn't it be 256? What is a better way to get the binary
representation of my hash string? I also note that
test_hash_value.to_s.unpack('B*')[0]
has 512 as its length, I think this must be because it is hex but being
treated as a byte string so its * 16 instead of * 8 ?
I am really new to this stuff an having somewhat of a hard time going
between all of these formats so I will appreciate any help or if anyone
can point me to resources even.
The primary problem I am having is that I can get a binary
representation of various components, and then I need to concatenate
them and encrypt them together, but I need openssl to treat them as bits
and it is treating it as bytes if I do this:
string_to_encrypt = hash_string.to_s.unpack('B*')[0] +
hash_string.to_s.unpack('B*')[0] + ("\0" * 160).unpack('B*')
I can see each of the components as a string of binary but then when I
encrypt string_to_encrypt with openSSL it seems to be treating it not as
a string of binary but as a normal string, judging by the very large
output size.
So my two goals are to get an accurate binary representation of hash
values, and to perform encryption operations on a bit string. I think I
am close to the first thing but I can only get 255 bits.
Even just some hints are greatly appreciated, I want to figure it out
myself but am starting to get frustrated with it 
···
--
Posted via http://www.ruby-forum.com/.
at least it