I’m trying to get a test to pass and keep getting a “no method error”
I’m using the routines from Chapter 8 of “Security on Rails” and can’t
get the test to pass. I keep getting “no method error” for decrypt.
Can someone help me with the correct “address” for decrypt so I can
call it directly.
Here is the routine definition:
module Encryptor
module Routines
def decrypt(cipher_text, key, opts)
cipher = OpenSSL::Cipher::Cipher.new(algorithm(opts))
decoded_cipher_text = Base64.decode64(cipher_text)
#cipher.decrypt(key, decoded_cipher_text.slice!(0…15))
cipher.decrypt
cipher.pkcs5_keyivgen(key)
# DW 11/3/10 Above two lines per deprecation warning and
stackoverflow site
# TODO Use a real Salt instead of Thoracic1
out = cipher.update(decoded_cipher_text)
out << cipher.final
end
end
end
Here is the “after save” hook:
module Encryptor
module ActiveRecord
class Encryptor
include ::Encryptor::Routines
def after_save(model)
unless model[@field].blank?
key = model.class.encryption_key
model[@field] = decrypt(model[@field], key, @options)
end
end
end
end
end
Here is the test:
require ‘test_helper’
class UserTest < ActiveSupport::TestCase
fixtures :users
test “the_encrypter” do
@user = users(:bob)
@user.secret_answer = “Volvo”
assert @user.save
@answer = User.connection.select_all(
“select secret_answer from users where id = 1”
)
cipher_text = @answer.first[‘secret_answer’]
opts = Encryptor::ActiveRecord::Encryptor.default_options
@plain_text = decrypt(cipher_text, User.encryption_key, opts)
assert_equal @plain_text, “Volvo”
end
end