Pack()ing Bignums


(Dan Debertin) #1

I have a 128-bit Bignum that I need to pack into a binary string in
order to pass it to MD5.digest(). But there doesn’t appear to be a
format for Array#pack that would accommodate such a large number; the
biggest appears to be “L”, which is only a quarter the size I need.

What’s the best way to do this?

Dan

···


airboss@nodewarrior.org
www.nodewarrior.org
ignorami: n:
The art of folding problem users into representational shapes.


(Yukihiro Matsumoto) #2

Hi,

I have a 128-bit Bignum that I need to pack into a binary string in
order to pass it to MD5.digest(). But there doesn’t appear to be a
format for Array#pack that would accommodate such a large number; the
biggest appears to be “L”, which is only a quarter the size I need.

What’s the best way to do this?

Perhaps not as efficient as you expect:

a)

b = 128-bit bignum value
data = []
4.times{|i|
data.unshift (b>>i*32)&0xffffffff
}
data.pack(“NNNN”)

b)

[sprintf("%0128b", b)].pack(“B*”)

						matz.
···

In message “pack()ing Bignums” on 02/06/12, Dan Debertin airboss@nodewarrior.org writes:


(Dan Debertin) #3

Yukihiro Matsumoto writes:

Perhaps not as efficient as you expect:

a)

b = 128-bit bignum value
data = []
4.times{|i|
data.unshift (b>>i*32)&0xffffffff
}
data.pack(“NNNN”)

This works:
data.unshift((b>>i*32)&0xffffffff)

Without the enclosing parens it tries to & the array, it appears.

b)

[sprintf("%0128b", b)].pack(“B*”)

Also works. Many thanks.

Dan

···


airboss@nodewarrior.org
www.nodewarrior.org
ignorami: n:
The art of folding problem users into representational shapes.


(ts) #4

data.unshift (b>>i*32)&0xffffffff

[...]

       data.unshift((b>>i*32)&0xffffffff)

Without the enclosing parens it tries to & the array, it appears.

This is a difference 1.6, 1.7

Guy Decoux


(Yukihiro Matsumoto) #5

Hi,

···

In message “Re: pack()ing Bignums” on 02/06/12, ts decoux@moulon.inra.fr writes:

data.unshift (b>>i32)&0xffffffff
[…]
data.unshift((b>>i
32)&0xffffffff)

Without the enclosing parens it tries to & the array, it appears.

This is a difference 1.6, 1.7

Oops, 1.7 is too smart. I should have checked on 1.6 as well.

						matz.