Class: Captcha::Cipher

Inherits:
Object
  • Object
show all
Defined in:
lib/captcha/cipher.rb

Constant Summary collapse

@@key =
Digest::SHA1.hexdigest(Config.options[:password])
@@iv =
'captchas'*2

Class Method Summary collapse

Class Method Details

.decrypt(text) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/captcha/cipher.rb', line 25

def self.decrypt(text)
  # Decode chr coded string
  encrypted = text.split('_').collect do |x|
    x.to_i.chr
  end
  encrypted = encrypted.join('')
  # Decrypt
  cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
  cipher.decrypt
  cipher.key = @@key
  cipher.iv = @@iv
  decrypted = cipher.update(encrypted)
  decrypted << cipher.final
end

.encrypt(text) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/captcha/cipher.rb', line 9

def self.encrypt(text)
  # Encrypt
  cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
  cipher.encrypt
  cipher.key = @@key
  cipher.iv = @@iv
  encrypted = cipher.update(text)
  encrypted << cipher.final
  # Turn into chr codes separated by underscores
  # 135_14_163_53_43_135_172_31_1_23_169_81_49_110_49_230
  encrypted = (0..encrypted.length-1).collect do |x|
    encrypted[x]
  end
  encrypted.join('_')
end