module Ciri::Utils

Constants

BLANK_SHA3
VERSION

Public Class Methods

blank?(item) click to toggle source
# File lib/ciri/utils.rb, line 63
def blank?(item)
  if item.nil?
    true
  elsif item.is_a? Integer
    item.zero?
  elsif item.is_a? String
    item.empty?
  else
    false
  end
end
blank_bytes?(item) click to toggle source
# File lib/ciri/utils.rb, line 58
def blank_bytes?(item)
  return true if item.is_a?(String) && item.each_byte.all?(&:zero?)
  blank?(item)
end
create_ec_pk(raw_pubkey: nil, raw_privkey: nil) click to toggle source
# File lib/ciri/utils.rb, line 41
def create_ec_pk(raw_pubkey: nil, raw_privkey: nil)
  public_key = raw_pubkey && begin
    group = OpenSSL::PKey::EC::Group.new('secp256k1')
    bn = OpenSSL::BN.new(raw_pubkey, 2)
    OpenSSL::PKey::EC::Point.new(group, bn)
  end

  OpenSSL::PKey::EC.new('secp256k1').tap do |key|
    key.public_key = public_key if public_key
    key.private_key = OpenSSL::BN.new(raw_privkey, 2) if raw_privkey
  end
end
hex_to_number(hex) click to toggle source
# File lib/ciri/utils.rb, line 28
def hex_to_number(hex)
  big_endian_decode to_bytes(hex)
end
keccak(*data, bits: 256) click to toggle source
# File lib/ciri/utils.rb, line 13
def keccak(*data, bits: 256)
  s = Digest::SHA3.new(bits)
  data.each {|i| s.update(i)}
  s.digest
end
number_to_hex(number) click to toggle source
# File lib/ciri/utils.rb, line 37
def number_to_hex(number)
  to_hex big_endian_encode(number)
end
present?(item) click to toggle source
# File lib/ciri/utils.rb, line 75
def present?(item)
  !blank?(item)
end
secret_compare(s1, s2) click to toggle source
# File lib/ciri/utils.rb, line 19
def secret_compare(s1, s2)
  s1.size == s2.size && s1.each_byte.each_with_index.map {|b, i| b ^ s2[i].ord}.reduce(0, :+) == 0
end
to_bytes(hex) click to toggle source
# File lib/ciri/utils.rb, line 23
def to_bytes(hex)
  hex = hex[2..-1] if hex.start_with?('0x')
  [hex].pack("H*")
end
to_hex(data) click to toggle source
# File lib/ciri/utils.rb, line 32
def to_hex(data)
  hex = data.to_s.unpack("H*").first
  '0x' + hex
end
to_underscore(str) click to toggle source
# File lib/ciri/utils.rb, line 54
def to_underscore(str)
  str.gsub(/[A-Z]/) {|a| "_" + a.downcase}
end