Module: Rmega::Utils
Instance Method Summary collapse
- #a32_to_base64(a32) ⇒ Object
- #a32_to_str(a32, len = nil) ⇒ Object
- #b2s(b) ⇒ Object
- #b64a ⇒ Object
- #base64_to_a32(base64) ⇒ Object
- #base64urldecode(data) ⇒ Object
- #base64urlencode(string) ⇒ Object
- #chunks(size) ⇒ Object
- #mpi2b(s) ⇒ Object
- #str_to_a32(string) ⇒ Object
Instance Method Details
#a32_to_base64(a32) ⇒ Object
28 29 30 |
# File 'lib/rmega/utils.rb', line 28 def a32_to_base64(a32) base64urlencode a32_to_str(a32) end |
#a32_to_str(a32, len = nil) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/rmega/utils.rb', line 11 def a32_to_str(a32, len = nil) if len b = [] len.times do |i| # TODO: should be ((a32[i>>2] >>> (24-(i & 3)*8)) & 255) b << (((a32[i>>2] || 0) >> (24-(i & 3)*8)) & 255) end b.pack 'C*' else a32.pack 'l>*' end end |
#b2s(b) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/rmega/utils.rb', line 141 def b2s(b) bs = 28 bm = 268435455 bn = 1; bc = 0; r = [0]; rb = 1; rn = 0 bits = b.length * bs rr = '' bits.times do |n| if (b[bc] & bn) and (b[bc] & bn) != 0 r[rn] = r[rn] ? (r[rn] | rb) : rb end rb = rb << 1 if rb > 255 rb = 1 r[rn += 1] = 0 end bn = bn << 1 if bn > bm bn = 1 bc += 1 end end while rn >= 0 && r[rn] == 0 rn -= 1 end (rn + 1).times do |n| rr = r[n].chr + rr end rr end |
#b64a ⇒ Object
24 25 26 |
# File 'lib/rmega/utils.rb', line 24 def b64a @b64a ||= ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a + ["-", "_", "="] end |
#base64_to_a32(base64) ⇒ Object
32 33 34 |
# File 'lib/rmega/utils.rb', line 32 def base64_to_a32(base64) str_to_a32 base64urldecode(base64) end |
#base64urldecode(data) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/rmega/utils.rb', line 63 def base64urldecode(data) data += '=='[((2-data.length*3)&3)..-1] i = 0 ac = 0 dec = "" tmp_arr = [] return data unless data while i < data.size h1 = b64a.index(data[i]) || -1 i += 1 h2 = b64a.index(data[i]) || -1 i += 1 h3 = b64a.index(data[i]) || -1 i += 1 h4 = b64a.index(data[i]) || -1 i += 1 bits = (h1 << 18) | (h2 << 12) | (h3 << 6) | h4 o1 = bits >> 16 & 0xff o2 = bits >> 8 & 0xff o3 = bits & 0xff if h3 == 64 tmp_arr[ac] = o1.chr elsif h4 == 64 tmp_arr[ac] = o1.chr + o2.chr else tmp_arr[ac] = o1.chr + o2.chr + o3.chr end ac += 1 end tmp_arr.join '' end |
#base64urlencode(string) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rmega/utils.rb', line 36 def base64urlencode(string) i = 0 tmp_arr = [] while i < string.size + 1 o1 = string[i].ord rescue 0 i += 1 o2 = string[i].ord rescue 0 i += 1 o3 = string[i].ord rescue 0 i += 1 bits = o1 << 16 | o2 << 8 | o3 h1 = bits >> 18 & 0x3f h2 = bits >> 12 & 0x3f h3 = bits >> 6 & 0x3f h4 = bits & 0x3f tmp_arr.push b64a[h1] + b64a[h2] + b64a[h3] + b64a[h4] end enc = tmp_arr.join '' r = string.size % 3 (r != 0) ? enc[0..r - 4] : enc end |
#chunks(size) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/rmega/utils.rb', line 179 def chunks(size) list = {} p = 0 pp = 0 i = 1 while i <= 8 and p < size - (i * 0x20000) list[p] = i * 0x20000 pp = p p += list[p] i += 1 end while p < size list[p] = 0x100000 pp = p p += list[p] end if size - pp > 0 list[pp] = size - pp end list end |
#mpi2b(s) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/rmega/utils.rb', line 103 def mpi2b(s) bn = 1 r = [0] rn = 0 sb = 256 sn = s.size bm = 268435455 c = nil return 0 if sn < 2 len = (sn - 2) * 8 bits = s[0].ord * 256 + s[1].ord return 0 if bits > len or bits < len - 8 len.times do |n| sb = sb << 1 if sb > 255 sb = 1 c = s[sn -= 1].ord end if bn > bm bn = 1 r[rn += 1] = 0 end if (c & sb) and (c & sb != 0) r[rn] = r[rn] ? (r[rn] | bn) : bn end bn = bn << 1 end r end |
#str_to_a32(string) ⇒ Object
5 6 7 8 9 |
# File 'lib/rmega/utils.rb', line 5 def str_to_a32(string) size = (string.bytesize + 3) >> 2 string = string.ljust (string.bytesize + 3), "\x00" string.unpack "l>#{size}" end |