Module: Cinch::Plugins::EnCinch::Encryption::Base64

Defined in:
lib/cinch/plugins/encinch/encryption.rb

Constant Summary collapse

Alphabet =
"./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".freeze

Class Method Summary collapse

Class Method Details

.decode(data) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cinch/plugins/encinch/encryption.rb', line 37

def self.decode(data)
  res = String.new
  data = data.dup.force_encoding("BINARY")

  data.chars.each_slice(12) do |slice|
    slice = slice.join
    left = right = 0
    slice[0..5].each_char.with_index do |pi, i|
      right |= Alphabet.index(pi) << (i * 6)
    end

    slice[6..11].each_char.with_index do |pi, i|
      left |= Alphabet.index(pi) << (i * 6)
    end

    res << [left, right].pack('L>L>')
  end

  res
end

.encode(data) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cinch/plugins/encinch/encryption.rb', line 16

def self.encode(data)
  res = String.new
  data = data.dup.force_encoding("BINARY")

  data.chars.each_slice(8) do |slice|
    slice = slice.join
    left, right = slice.unpack('L>L>')
    6.times do
      res << Alphabet[right & 0x3f]
      right >>= 6
    end

    6.times do
      res << Alphabet[left & 0x3f]
      left >>= 6
    end
  end

  res
end