Class: Cinch::Plugins::EnCinch::Encryption

Inherits:
Object
  • Object
show all
Defined in:
lib/cinch/plugins/encinch/encryption.rb

Defined Under Namespace

Modules: Base64

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Encryption

Returns a new instance of Encryption.



60
61
62
# File 'lib/cinch/plugins/encinch/encryption.rb', line 60

def initialize(key)
  @blowfish = Crypt::Blowfish.new(key)
end

Instance Method Details

#decrypt(data) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cinch/plugins/encinch/encryption.rb', line 78

def decrypt(data)
  data.sub!(/^\+OK\s+/, '')

  return nil if not data.length % 12 == 0

  result = String.new

  num_block = (data.length / 12).to_i
  num_block.times do |n|
    block = Base64.decode( data[n*12..(n + 1)*12-1] )
    result += @blowfish.decrypt_block(block)
  end

  result.gsub(/\0*$/, '')
end

#encrypt(data) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cinch/plugins/encinch/encryption.rb', line 64

def encrypt(data)
  data = pad(data, 8)
  result = String.new

  num_block = data.length / 8
  num_block.times do |n|
    block = data[n * 8..(n + 1) * 8 - 1]
    enc = @blowfish.encrypt_block(block)
    result += Base64.encode(enc)
  end

  "+OK " << result
end