Sha256: 7dfdf7aea38f376d2fe5107475b46c80e0519e5948b9474cedf8672d500b786b

Contents?: true

Size: 1.05 KB

Versions: 6

Compression:

Stored size: 1.05 KB

Contents

module Rmega
  module Utils
    extend self

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

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

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

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

    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

    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
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rmega-0.2.6 lib/rmega/utils.rb
rmega-0.2.5 lib/rmega/utils.rb
rmega-0.2.4 lib/rmega/utils.rb
rmega-0.2.2 lib/rmega/utils.rb
rmega-0.2.1 lib/rmega/utils.rb
rmega-0.2.0 lib/rmega/utils.rb