Sha256: d5b497f4c0f5a46af579fd1dd71351a348daa9607aa497e3069335d9b5e8b45f

Contents?: true

Size: 1.19 KB

Versions: 9

Compression:

Stored size: 1.19 KB

Contents

# coding: utf-8

require 'openssl'
require 'digest/sha1'

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

    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
      if encrypted.respond_to?(:codepoints)
        encrypted = encrypted.codepoints.to_a
      else
        encrypted = (0..encrypted.length-1).collect do |x|
          encrypted[x]
        end
      end
      encrypted.join('_')
    end

    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
      decrypted.gsub(/[^a-z]/, '')
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rails_captcha-0.0.10 lib/rails_captcha/captcha/cipher.rb
rails_captcha-0.0.8 lib/rails_captcha/captcha/cipher.rb
rails_captcha-0.0.7 lib/rails_captcha/captcha/cipher.rb
rails_captcha-0.0.6 lib/rails_captcha/captcha/cipher.rb
rails_captcha-0.0.5 lib/rails_captcha/captcha/cipher.rb
rails_captcha-0.0.4 lib/rails_captcha/captcha/cipher.rb
rails_captcha-0.0.3 lib/rails_captcha/captcha/cipher.rb
rails_captcha-0.0.2 lib/rails_captcha/captcha/cipher.rb
rails_captcha-0.0.1 lib/rails_captcha/captcha/cipher.rb