Module: Cryptos::Utils::Bytes
Instance Method Summary collapse
- #bignum_to_bytes(n, length = nil, stringify = true) ⇒ Object
- #bytes_to_bignum(bytes_string) ⇒ Object
Instance Method Details
#bignum_to_bytes(n, length = nil, stringify = true) ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/cryptos/utils/bytes.rb', line 8 def bignum_to_bytes(n, length=nil, stringify=true) a = [] while n > 0 a << (n & 0xFF) n >>= 8 end a.fill 0x00, a.length, length - a.length if length bytes = a.reverse stringify ? bytes.pack('C*') : bytes end |
#bytes_to_bignum(bytes_string) ⇒ Object
4 5 6 |
# File 'lib/cryptos/utils/bytes.rb', line 4 def bytes_to_bignum(bytes_string) bytes_string.bytes.reduce { |n, b| (n << 8) + b } end |