Module: Neb::Utils

Extended by:
Utils
Includes:
Constant
Included in:
Utils
Defined in:
lib/neb/utils.rb

Constant Summary

Constants included from Constant

Constant::ADDRESS_LENGTH, Constant::ADDRESS_PREFIX, Constant::BYTE_EMPTY, Constant::BYTE_ONE, Constant::BYTE_ZERO, Constant::CONTRACT_CODE_SIZE_LIMIT, Constant::CONTRACT_TYPE, Constant::HASH_ZERO, Constant::INT_MAX, Constant::INT_MIN, Constant::NORMAL_TYPE, Constant::PRIVKEY_ZERO, Constant::PRIVKEY_ZERO_HEX, Constant::PUBKEY_ZERO, Constant::TT160, Constant::TT256, Constant::TT32, Constant::TT40, Constant::TT64M1, Constant::UINT_MAX, Constant::UINT_MIN

Instance Method Summary collapse

Instance Method Details

#aes_decrypt(ciphertext, bin_key, bin_iv) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# 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) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object Also known as: binary_to_base58



97
98
99
# File 'lib/neb/utils.rb', line 97

def base58(x)
  Base58.binary_to_base58(x, :bitcoin)
end

#base58_to_binary(b) ⇒ Object



102
103
104
# File 'lib/neb/utils.rb', line 102

def base58_to_binary(b)
  Base58.base58_to_binary(b, :bitcoin)
end

#big_endian_to_int(s) ⇒ Object



57
58
59
# 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) ⇒ Object



65
66
67
# 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

#encode64(str) ⇒ Object



53
54
55
# File 'lib/neb/utils.rb', line 53

def encode64(str)
  Base64.strict_encode64(str)
end

#from_json(s) ⇒ Object



14
15
16
# File 'lib/neb/utils.rb', line 14

def from_json(s)
  JSON.parse(s, symbolize_names: true)
end

#hash160(x) ⇒ Object



93
94
95
# File 'lib/neb/utils.rb', line 93

def hash160(x)
  ripemd160(keccak256(x))
end

#hex_to_bin(hex) ⇒ Object



69
70
71
# 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) ⇒ Object



61
62
63
# File 'lib/neb/utils.rb', line 61

def int_to_big_endian(n)
  RLP::Sedes.big_endian_int.serialize(n)
end

#keccak256(x) ⇒ Object



77
78
79
# File 'lib/neb/utils.rb', line 77

def keccak256(x)
  SHA3::Digest::SHA256.digest(x)
end

#keccak512(x) ⇒ Object



81
82
83
# File 'lib/neb/utils.rb', line 81

def keccak512(x)
  SHA3::Digest::SHA512.digest(x)
end

#lpad(x, symbol, l) ⇒ Object



106
107
108
109
# 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) ⇒ Object



120
121
122
# 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) ⇒ Object



124
125
126
# 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) ⇒ Object



73
74
75
# File 'lib/neb/utils.rb', line 73

def random_bytes(size = 32)
  SecureRandom.random_bytes(size)
end

#ripemd160(x) ⇒ Object



89
90
91
# File 'lib/neb/utils.rb', line 89

def ripemd160(x)
  Digest::RMD160.digest(x)
end

#rpad(x, symbol, l) ⇒ Object



111
112
113
114
# 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) ⇒ Object

args: n, r, p, key_len



23
24
25
# File 'lib/neb/utils.rb', line 23

def scrypt(secret, salt, *args)
  SCrypt::Engine.scrypt(secret, salt, *args)
end

#secure_compare(a, b) ⇒ Object



18
19
20
# File 'lib/neb/utils.rb', line 18

def secure_compare(a, b)
  ActiveSupport::SecurityUtils.secure_compare(a, b)
end

#sha256(x) ⇒ Object



85
86
87
# File 'lib/neb/utils.rb', line 85

def sha256(x)
  Digest::SHA256.digest(x)
end

#to_json(h) ⇒ Object



10
11
12
# File 'lib/neb/utils.rb', line 10

def to_json(h)
  JSON.generate(h)
end

#uuidObject



49
50
51
# File 'lib/neb/utils.rb', line 49

def uuid
  SecureRandom.uuid
end

#zpad(x, l) ⇒ Object



116
117
118
# File 'lib/neb/utils.rb', line 116

def zpad(x, l)
  lpad(x, BYTE_ZERO, l)
end