Module: Raptcha::Encryptor

Extended by:
Encryptor
Included in:
Encryptor
Defined in:
lib/raptcha.rb

Instance Method Summary collapse

Instance Method Details

#algObject



190
191
192
# File 'lib/raptcha.rb', line 190

def alg
  @alg ||= 'AES-256-CBC'
end

#cycle(plaintext, options = {}) ⇒ Object



172
173
174
# File 'lib/raptcha.rb', line 172

def cycle(plaintext, options = {})
  decrypt(encrypt(plaintext, options), options)
end

#decrypt(ciphertext, options = {}) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/raptcha.rb', line 160

def decrypt(ciphertext, options = {})
  ciphertext = Encoder.decode(ciphertext.to_s)
  key = options[:key] || options['key'] || Encryptor.key
  alg = options[:alg] || options['alg'] || Encryptor.alg
  salt = options[:salt] || options['salt'] || Encryptor.salt
  dec = OpenSSL::Cipher::Cipher.new(alg)
  dec.decrypt
  dec.pkcs5_keyivgen(key, salt)
  plaintext =  dec.update(ciphertext)
  plaintext << dec.final
end

#default_keyObject



182
183
184
# File 'lib/raptcha.rb', line 182

def default_key
  Rails.application.config.secret_token
end

#encrypt(plaintext, options = {}) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/raptcha.rb', line 147

def encrypt(plaintext, options = {})
  plaintext = plaintext.to_s
  key = options[:key] || options['key'] || Encryptor.key
  alg = options[:alg] || options['alg'] || Encryptor.alg
  salt = options[:salt] || options['salt'] || Encryptor.salt
  enc = OpenSSL::Cipher::Cipher.new(alg)
  enc.encrypt
  enc.pkcs5_keyivgen(key, salt)
  ciphertext =  enc.update(plaintext)
  ciphertext << enc.final
  Encoder.encode(ciphertext)
end

#key(*key) ⇒ Object



176
177
178
179
180
# File 'lib/raptcha.rb', line 176

def key(*key)
  self.key = key.first.to_s unless key.empty?
  self.key = default_key unless defined?(@key)
  @key
end

#key=(key) ⇒ Object



186
187
188
# File 'lib/raptcha.rb', line 186

def key=(key)
  @key = key.to_s[0, 56]
end

#saltObject



194
195
196
# File 'lib/raptcha.rb', line 194

def salt
  @salt ||= nil
end

#salt=(salt) ⇒ Object



198
199
200
# File 'lib/raptcha.rb', line 198

def salt=(salt)
  @salt = salt
end