Skip to content

Commit

Permalink
Use an options hash to specify digest/cipher algorithm and a serializ…
Browse files Browse the repository at this point in the history
…er for MessageVerifier and MessageEncryptor.
  • Loading branch information
wvanbergen committed Sep 15, 2011
1 parent 2d30d4c commit 41fea03
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
17 changes: 10 additions & 7 deletions activesupport/lib/active_support/message_encryptor.rb
Expand Up @@ -13,12 +13,15 @@ class MessageEncryptor
class InvalidMessage < StandardError; end
OpenSSLCipherError = OpenSSL::Cipher.const_defined?(:CipherError) ? OpenSSL::Cipher::CipherError : OpenSSL::CipherError

attr_accessor :serializer

def initialize(secret, cipher = 'aes-256-cbc', serializer = Marshal)
def initialize(secret, options = {})
unless options.is_a?(Hash)
ActiveSupport::Deprecation.warn "The second parameter should be an options hash. Use :cipher => 'algorithm' to sepcify the cipher algorithm."
options = { :cipher => options }
end

@secret = secret
@cipher = cipher
@serializer = serializer
@cipher = options[:cipher] || 'aes-256-cbc'
@serializer = options[:serializer] || Marshal
end

def encrypt(value)
Expand All @@ -30,7 +33,7 @@ def encrypt(value)
cipher.key = @secret
cipher.iv = iv

encrypted_data = cipher.update(serializer.dump(value))
encrypted_data = cipher.update(@serializer.dump(value))
encrypted_data << cipher.final

[encrypted_data, iv].map {|v| ActiveSupport::Base64.encode64s(v)}.join("--")
Expand All @@ -47,7 +50,7 @@ def decrypt(encrypted_message)
decrypted_data = cipher.update(encrypted_data)
decrypted_data << cipher.final

serializer.load(decrypted_data)
@serializer.load(decrypted_data)
rescue OpenSSLCipherError, TypeError
raise InvalidMessage
end
Expand Down
17 changes: 10 additions & 7 deletions activesupport/lib/active_support/message_verifier.rb
Expand Up @@ -26,27 +26,30 @@ module ActiveSupport
class MessageVerifier
class InvalidSignature < StandardError; end

attr_accessor :serializer

def initialize(secret, digest = 'SHA1', serializer = Marshal)
def initialize(secret, options = {})
unless options.is_a?(Hash)
ActiveSupport::Deprecation.warn "The second parameter should be an options hash. Use :digest => 'algorithm' to sepcify the digest algorithm."
options = { :digest => options }
end

@secret = secret
@digest = digest
@serializer = serializer
@digest = options[:digest] || 'SHA1'
@serializer = options[:serializer] || Marshal
end

def verify(signed_message)
raise InvalidSignature if signed_message.blank?

data, digest = signed_message.split("--")
if data.present? && digest.present? && secure_compare(digest, generate_digest(data))
serializer.load(ActiveSupport::Base64.decode64(data))
@serializer.load(ActiveSupport::Base64.decode64(data))
else
raise InvalidSignature
end
end

def generate(value)
data = ActiveSupport::Base64.encode64s(serializer.dump(value))
data = ActiveSupport::Base64.encode64s(@serializer.dump(value))
"#{data}--#{generate_digest(data)}"
end

Expand Down
6 changes: 3 additions & 3 deletions activesupport/test/message_encryptor_test.rb
Expand Up @@ -52,9 +52,9 @@ def test_signed_round_tripping
end

def test_alternative_serialization_method
@encryptor.serializer = JSONSerializer.new
message = @encryptor.encrypt_and_sign({ :foo => 123, 'bar' => Time.utc(2010) })
assert_equal @encryptor.decrypt_and_verify(message), { "foo" => 123, "bar" => "2010-01-01T00:00:00Z" }
encryptor = ActiveSupport::MessageEncryptor.new(SecureRandom.hex(64), :serializer => JSONSerializer.new)
message = encryptor.encrypt_and_sign({ :foo => 123, 'bar' => Time.utc(2010) })
assert_equal encryptor.decrypt_and_verify(message), { "foo" => 123, "bar" => "2010-01-01T00:00:00Z" }
end

private
Expand Down
6 changes: 3 additions & 3 deletions activesupport/test/message_verifier_test.rb
Expand Up @@ -45,9 +45,9 @@ def test_tampered_data_raises
end

def test_alternative_serialization_method
@verifier.serializer = JSONSerializer.new
message = @verifier.generate({ :foo => 123, 'bar' => Time.utc(2010) })
assert_equal @verifier.verify(message), { "foo" => 123, "bar" => "2010-01-01T00:00:00Z" }
verifier = ActiveSupport::MessageVerifier.new("Hey, I'm a secret!", :serializer => JSONSerializer.new)
message = verifier.generate({ :foo => 123, 'bar' => Time.utc(2010) })
assert_equal verifier.verify(message), { "foo" => 123, "bar" => "2010-01-01T00:00:00Z" }
end

def assert_not_verified(message)
Expand Down

0 comments on commit 41fea03

Please sign in to comment.