Module: SJCL::Codec::Base64
- Defined in:
- lib/sjcl/codec_base64.rb
Constant Summary collapse
- CHARS =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Class Method Summary collapse
Class Method Details
.fromBits(arr, noEquals = false, url = false) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/sjcl/codec_base64.rb', line 4 def self.fromBits(arr, noEquals=false, url=false) out = "" bits=0 c = CHARS.dup ta=0 i = 0 bl = SJCL::BitArray.bitLength(arr) if (url) c = c[0,62] + '-_'; end while (out.length * 6) < bl a = (arr[i] & 0xFFFFFFFF) || 0 out += c[(ta ^ a >> bits) >> 26,1] if (bits < 6) ta = (a << (6-bits)) & 0xFFFFFFFF bits += 26 i += 1 else ta = (ta << 6) & 0xFFFFFFFF bits -= 6 end end while ((out.length & 3 > 0) && !noEquals) out += "=" end return out end |
.toBits(str, url = false) ⇒ Object
32 33 34 35 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 62 63 |
# File 'lib/sjcl/codec_base64.rb', line 32 def self.toBits(str, url=false) i=0 bits = 0 ta = 0 c = CHARS.dup out = [] if (url) c = c[0,62] + '-_' end while (i < str.length) str = str.gsub(/\s|=/, '') x = c.index(str[i]); unless x raise "this isn't base64!" end if (bits > 26) bits -= 26; out << ((ta ^ x >> bits) & 0xFFFFFFFF) ta = x << (32-bits) ta &= 0xFFFFFFFF else bits += 6 ta ^= x << (32-bits) ta &= 0xFFFFFFFF end i += 1 end if (bits&56 > 0) out.push(SJCL::BitArray.partial(bits & 56, ta, 1)); end return out end |