Module: CiBlockIo::Helper

Defined in:
lib/ci_block_io.rb

Class Method Summary collapse

Class Method Details

.base58_to_int(base58_val) ⇒ Object



400
401
402
403
404
405
406
407
408
# File 'lib/ci_block_io.rb', line 400

def self.base58_to_int(base58_val)
  alpha = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
  int_val, base = 0, alpha.size
  base58_val.reverse.each_char.with_index do |char,index|
    raise ArgumentError, 'Value not a valid Base58 String.' unless char_index = alpha.index(char)
    int_val += char_index*(base**index)
  end
  int_val
end

.decode_base58(base58_val) ⇒ Object



415
416
417
418
419
420
421
# File 'lib/ci_block_io.rb', line 415

def self.decode_base58(base58_val)
  s = Helper.base58_to_int(base58_val).to_s(16); s = (s.bytesize.odd? ? '0'+s : s)
  s = '' if s == '00'
  leading_zero_bytes = (base58_val.match(/^([1]+)/) ? $1 : '').size
  s = ("00"*leading_zero_bytes) + s  if leading_zero_bytes > 0
  s
end

.decrypt(encrypted_data, b64_enc_key, iv = nil, cipher_type = 'AES-256-ECB') ⇒ Object

Decrypts a block of data (encrypted_data) given an encryption key



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/ci_block_io.rb', line 361

def self.decrypt(encrypted_data, b64_enc_key, iv = nil, cipher_type = 'AES-256-ECB')

  response = nil

  begin
    aes = OpenSSL::Cipher.new(cipher_type)
    aes.decrypt
    aes.key = Base64.strict_decode64(b64_enc_key)
    aes.iv = iv if iv != nil
    response = aes.update(Base64.strict_decode64(encrypted_data)) + aes.final
  rescue Exception => e
    # decryption failed, must be an invalid Secret PIN
    raise Exception.new('Invalid Secret PIN provided.')
  end

  return response
end

.encode_base58(hex) ⇒ Object



410
411
412
413
# File 'lib/ci_block_io.rb', line 410

def self.encode_base58(hex)
  leading_zero_bytes  = (hex.match(/^([0]+)/) ? $1 : '').size / 2
  ("1"*leading_zero_bytes) + Helper.int_to_base58( hex.to_i(16) )
end

.encrypt(data, b64_enc_key, iv = nil, cipher_type = 'AES-256-ECB') ⇒ Object

Encrypts a block of data given an encryption key



380
381
382
383
384
385
386
# File 'lib/ci_block_io.rb', line 380

def self.encrypt(data, b64_enc_key, iv = nil, cipher_type = 'AES-256-ECB')
  aes = OpenSSL::Cipher.new(cipher_type)
  aes.encrypt
  aes.key = Base64.strict_decode64(b64_enc_key)
  aes.iv = iv if iv != nil
  Base64.strict_encode64(aes.update(data) + aes.final)
end

.extractKey(encrypted_data, b64_enc_key) ⇒ Object



331
332
333
334
335
336
337
338
339
# File 'lib/ci_block_io.rb', line 331

def self.extractKey(encrypted_data, b64_enc_key)
  # passphrase is in plain text
  # encrypted_data is in base64, as it was stored on Block.io
  # returns the private key extracted from the given encrypted data

  decrypted = self.decrypt(encrypted_data, b64_enc_key)

  return Key.from_passphrase(decrypted)
end

.int_to_base58(int_val, leading_zero_bytes = 0) ⇒ Object

courtesy bitcoin-ruby



390
391
392
393
394
395
396
397
398
# File 'lib/ci_block_io.rb', line 390

def self.int_to_base58(int_val, leading_zero_bytes=0)
  alpha = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
  base58_val, base = '', alpha.size
  while int_val > 0
    int_val, remainder = int_val.divmod(base)
    base58_val = alpha[remainder] + base58_val
  end
  base58_val
end

.pinToAesKey(secret_pin, iterations = 2048) ⇒ Object



348
349
350
351
352
353
354
355
356
357
358
# File 'lib/ci_block_io.rb', line 348

def self.pinToAesKey(secret_pin, iterations = 2048)
  # converts the pincode string to PBKDF2
  # returns a base64 version of PBKDF2 pincode
  salt = ""

  # pbkdf2-ruby gem uses SHA256 as the default hash function
  aes_key_bin = PBKDF2.new(:password => secret_pin, :salt => salt, :iterations => iterations/2, :key_length => 128/8).value
  aes_key_bin = PBKDF2.new(:password => aes_key_bin.unpack("H*")[0], :salt => salt, :iterations => iterations/2, :key_length => 256/8).value

  return Base64.strict_encode64(aes_key_bin) # the base64 encryption key
end

.sha256(value) ⇒ Object



341
342
343
344
345
346
# File 'lib/ci_block_io.rb', line 341

def self.sha256(value)
  # returns the hex of the hash of the given value
  hash = Digest::SHA2.new(256)
  hash << value
  hash.hexdigest # return hex
end

.signData(inputs, keys) ⇒ Object

Raises:

  • (Exception)


298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/ci_block_io.rb', line 298

def self.signData(inputs, keys)
  # sign the given data with the given keys
  # TODO loop is O(n^3), make it better

  raise Exception.new('Keys object must be an array of keys, without at least one key inside it.') unless keys.is_a?(Array) and keys.size >= 1

  i = 0
  while i < inputs.size do
    # iterate over all signers
    input = inputs[i]

    j = 0
    while j < input['signers'].size do
      # if our public key matches this signer's public key, sign the data
      signer = inputs[i]['signers'][j]

      k = 0
      while k < keys.size do
        # sign for each key provided, if we can
        key = keys[k]
        signer['signed_data'] = key.sign(input['data_to_sign']) if signer['signer_public_key'] == key.public_key
        k = k + 1
      end

      j = j + 1
    end

    i = i + 1
  end

  inputs
end