module HDetEc::DataManipulation

Public Instance Methods

double_sha256(data) click to toggle source
# File lib/hdet-ec-key/data_manipulation.rb, line 48
def double_sha256(data)
  Digest::SHA256.digest Digest::SHA256.digest(data)
end
Also aliased as: hash256
hash160(data)
Alias for: rmd160_sha256
hash256(data)
Alias for: double_sha256
left_hash(h) click to toggle source
# File lib/hdet-ec-key/data_manipulation.rb, line 30
def left_hash(h)
  h[0..31]
end
parse256(bin)

Interprets a 32-byte sequence as a 256-bit number, most significant byte first.

Alias for: to_number
right_hash(h) click to toggle source
# File lib/hdet-ec-key/data_manipulation.rb, line 34
def right_hash(h)
  h[32..63]
end
rmd160_sha256(data) click to toggle source
# File lib/hdet-ec-key/data_manipulation.rb, line 42
def rmd160_sha256(data)
  Digest::RMD160.digest Digest::SHA256.digest(data)
end
Also aliased as: hash160
ser256(p) click to toggle source

Serializes the integer p as a 32-byte sequence, most significant byte first.

# File lib/hdet-ec-key/data_manipulation.rb, line 25
def ser256(p)
  raise ArgumentError, "overflow" if p.bit_length > 256
  to_binary(p, 256/8)
end
ser32(i) click to toggle source

Serialize a 32-bit unsigned integer i as a 4-byte sequence, most significant byte first.

# File lib/hdet-ec-key/data_manipulation.rb, line 7
def ser32(i)
  [i].pack("l>")
end
split_hash(h) click to toggle source
# File lib/hdet-ec-key/data_manipulation.rb, line 38
def split_hash(h)
  [left_hash(h), right_hash(h)]
end
to_binary(num, byte_length=1) click to toggle source
# File lib/hdet-ec-key/data_manipulation.rb, line 11
def to_binary(num, byte_length=1)
  num = num.to_i if num.kind_of? OpenSSL::BN

  hex_num = num.to_s(16).rjust(byte_length*2, "0")
  hex_num = "0" + hex_num if hex_num.size.odd?

  bin = [hex_num].pack("H*")
end
to_number(bin) click to toggle source
# File lib/hdet-ec-key/data_manipulation.rb, line 20
def to_number(bin)
  bin.unpack1("H*").to_i 16
end
Also aliased as: parse256