Module: Rmega::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/rmega/utils.rb

Instance Method Summary collapse

Instance Method Details

#base64_mpi_to_bn(s) ⇒ Object



26
27
28
29
30
# File 'lib/rmega/utils.rb', line 26

def base64_mpi_to_bn(s)
  data = ::Rmega::Utils.base64urldecode(s)
  len = ((data[0].ord * 256 + data[1].ord + 7) / 8) + 2
  data[2,len+2].unpack('H*').first.to_i(16)
end

#base64urldecode(data) ⇒ Object



11
12
13
14
# File 'lib/rmega/utils.rb', line 11

def base64urldecode(data)
  fix_for_decoding = '=='[((2-data.length*3)&3)..-1]
  return Base64.urlsafe_decode64("#{data}#{fix_for_decoding}")
end

#base64urlencode(string) ⇒ Object



5
6
7
8
9
# File 'lib/rmega/utils.rb', line 5

def base64urlencode(string)
  r = string.size % 3
  encoded = Base64.urlsafe_encode64(string)
  return (r != 0) ? encoded[0..r - 4] : encoded
end

#compact_to_8_bytes(string) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rmega/utils.rb', line 32

def compact_to_8_bytes(string)
  raise("Invalid data length") if string.size != 16

  bytes = string.bytes.to_a

  return 8.times.inject([]) do |ary, i|
    n = i < 4 ? 0 : 4
    ary[i] = bytes[i+n] ^ bytes[i+n+4]
    ary
  end.map(&:chr).join
end

#hexstr_to_bstr(h) ⇒ Object



16
17
18
19
20
# File 'lib/rmega/utils.rb', line 16

def hexstr_to_bstr(h)
  bstr = ''
  (0..h.length-1).step(2) {|n| bstr << h[n,2].to_i(16).chr }
  bstr
end

#string_to_bignum(string) ⇒ Object



22
23
24
# File 'lib/rmega/utils.rb', line 22

def string_to_bignum(string)
  string.bytes.inject { |a, b| (a << 8) + b }
end