module Rmega::Utils
Public Instance Methods
base64_mpi_to_bn(s)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/rmega/utils.rb, line 22 def string_to_bignum(string) string.bytes.inject { |a, b| (a << 8) + b } end
utf8(s)
click to toggle source
# File lib/rmega/utils.rb, line 44 def utf8(s) str = s.dup str.force_encoding("UTF-8") str.encode!("UTF-8", invalid: :replace, replace: "-") return str end