module Neb::Utils

Public Instance Methods

aes_decrypt(ciphertext, bin_key, bin_iv) click to toggle source
# File lib/neb/utils.rb, line 38
def aes_decrypt(ciphertext, bin_key, bin_iv)
  cipher = OpenSSL::Cipher::AES128.new(:ctr)
  cipher.decrypt
  cipher.key = bin_key
  cipher.iv = bin_iv

  result = cipher.update(ciphertext)
  result += cipher.final
  result
end
aes_encrypt(raw, bin_key, bin_iv) click to toggle source
# File lib/neb/utils.rb, line 27
def aes_encrypt(raw, bin_key, bin_iv)
  cipher = OpenSSL::Cipher::AES128.new(:ctr)
  cipher.encrypt
  cipher.key = bin_key
  cipher.iv = bin_iv

  result = cipher.update(raw)
  result += cipher.final
  result
end
base58(x) click to toggle source
# File lib/neb/utils.rb, line 97
def base58(x)
  Base58.binary_to_base58(x, :bitcoin)
end
Also aliased as: binary_to_base58
base58_to_binary(b) click to toggle source
# File lib/neb/utils.rb, line 102
def base58_to_binary(b)
  Base58.base58_to_binary(b, :bitcoin)
end
big_endian_to_int(s) click to toggle source
# File lib/neb/utils.rb, line 57
def big_endian_to_int(s)
  RLP::Sedes.big_endian_int.deserialize(s.sub(/\A(\x00)+/, ''))
end
bin_to_hex(bytes) click to toggle source
# File lib/neb/utils.rb, line 65
def bin_to_hex(bytes)
  BaseConvert.convert(bytes, 256, 16, bytes.size * 2).force_encoding('utf-8')
end
binary_to_base58(x)
Alias for: base58
encode64(str) click to toggle source
# File lib/neb/utils.rb, line 53
def encode64(str)
  Base64.strict_encode64(str)
end
from_json(s) click to toggle source
# File lib/neb/utils.rb, line 14
def from_json(s)
  JSON.parse(s, symbolize_names: true)
end
hash160(x) click to toggle source
# File lib/neb/utils.rb, line 93
def hash160(x)
  ripemd160(keccak256(x))
end
hex_to_bin(hex) click to toggle source
# File lib/neb/utils.rb, line 69
def hex_to_bin(hex)
  BaseConvert.convert(hex, 16, 256, hex.size / 2).force_encoding('ascii-8bit')
end
int_to_big_endian(n) click to toggle source
# File lib/neb/utils.rb, line 61
def int_to_big_endian(n)
  RLP::Sedes.big_endian_int.serialize(n)
end
keccak256(x) click to toggle source
# File lib/neb/utils.rb, line 77
def keccak256(x)
  SHA3::Digest::SHA256.digest(x)
end
keccak512(x) click to toggle source
# File lib/neb/utils.rb, line 81
def keccak512(x)
  SHA3::Digest::SHA512.digest(x)
end
lpad(x, symbol, l) click to toggle source
# File lib/neb/utils.rb, line 106
def lpad(x, symbol, l)
  return x if x.size >= l
  symbol * (l - x.size) + x
end
mod_exp(x, y, n) click to toggle source
# File lib/neb/utils.rb, line 120
def mod_exp(x, y, n)
  x.to_bn.mod_exp(y, n).to_i
end
mod_mul(x, y, n) click to toggle source
# File lib/neb/utils.rb, line 124
def mod_mul(x, y, n)
  x.to_bn.mod_mul(y, n).to_i
end
random_bytes(size = 32) click to toggle source
# File lib/neb/utils.rb, line 73
def random_bytes(size = 32)
  SecureRandom.random_bytes(size)
end
ripemd160(x) click to toggle source
# File lib/neb/utils.rb, line 89
def ripemd160(x)
  Digest::RMD160.digest(x)
end
rpad(x, symbol, l) click to toggle source
# File lib/neb/utils.rb, line 111
def rpad(x, symbol, l)
  return x if x.size >= l
  x + symbol * (l - x.size)
end
scrypt(secret, salt, *args) click to toggle source

args: n, r, p, key_len

# File lib/neb/utils.rb, line 23
def scrypt(secret, salt, *args)
  SCrypt::Engine.scrypt(secret, salt, *args)
end
secure_compare(a, b) click to toggle source
# File lib/neb/utils.rb, line 18
def secure_compare(a, b)
  ActiveSupport::SecurityUtils.secure_compare(a, b)
end
sha256(x) click to toggle source
# File lib/neb/utils.rb, line 85
def sha256(x)
  Digest::SHA256.digest(x)
end
to_json(h) click to toggle source
# File lib/neb/utils.rb, line 10
def to_json(h)
  JSON.generate(h)
end
uuid() click to toggle source
# File lib/neb/utils.rb, line 49
def uuid
  SecureRandom.uuid
end
zpad(x, l) click to toggle source
# File lib/neb/utils.rb, line 116
def zpad(x, l)
  lpad(x, BYTE_ZERO, l)
end