Skip to content

Instantly share code, notes, and snippets.

@icostan
Created November 14, 2018 08:09
Show Gist options
  • Save icostan/608aa367f83a6ff1c7f8fee3327ce999 to your computer and use it in GitHub Desktop.
Save icostan/608aa367f83a6ff1c7f8fee3327ce999 to your computer and use it in GitHub Desktop.
def bytes_to_bignum(bytes_string)
bytes_string.bytes.reduce { |n, b| (n << 8) + b }
end
def bignum_to_bytes(n, length=nil)
a = []
while n > 0
a << (n & 0xFF)
n >>= 8
end
a.fill 0x00, a.length, length - a.length if length
a.reverse.pack('C*')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment