Module: HDetEc::DataManipulation

Included in:
Key, Key
Defined in:
lib/hdet-ec-key/data_manipulation.rb

Instance Method Summary collapse

Instance Method Details

#double_sha256(data) ⇒ Object Also known as: hash256



48
49
50
# File 'lib/hdet-ec-key/data_manipulation.rb', line 48

def double_sha256(data)
  Digest::SHA256.digest Digest::SHA256.digest(data)
end

#left_hash(h) ⇒ Object



30
31
32
# File 'lib/hdet-ec-key/data_manipulation.rb', line 30

def left_hash(h)
  h[0..31]
end

#right_hash(h) ⇒ Object



34
35
36
# File 'lib/hdet-ec-key/data_manipulation.rb', line 34

def right_hash(h)
  h[32..63]
end

#rmd160_sha256(data) ⇒ Object Also known as: hash160



42
43
44
# File 'lib/hdet-ec-key/data_manipulation.rb', line 42

def rmd160_sha256(data)
  Digest::RMD160.digest Digest::SHA256.digest(data)
end

#ser256(p) ⇒ Object

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

Raises:

  • (ArgumentError)


25
26
27
28
# 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) ⇒ Object

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



7
8
9
# File 'lib/hdet-ec-key/data_manipulation.rb', line 7

def ser32(i)
  [i].pack("l>")
end

#split_hash(h) ⇒ Object



38
39
40
# 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) ⇒ Object



11
12
13
14
15
16
17
18
# 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) ⇒ Object Also known as: parse256



20
21
22
# File 'lib/hdet-ec-key/data_manipulation.rb', line 20

def to_number(bin)
  bin.unpack1("H*").to_i 16
end