lib/encryptor.rb in encryptor-1.1.2 vs lib/encryptor.rb in encryptor-1.1.3
- old
+ new
@@ -25,12 +25,12 @@
# Example
#
# encrypted_value = Encryptor.encrypt(:value => 'some string to encrypt', :key => 'some secret key')
# # or
# encrypted_value = Encryptor.encrypt('some string to encrypt', :key => 'some secret key')
- def encrypt(*args)
- crypt :encrypt, *args
+ def encrypt(*args, &block)
+ crypt :encrypt, *args, &block
end
# Decrypts a <tt>:value</tt> with a specified <tt>:key</tt>
#
# Optionally accepts <tt>:iv</tt> and <tt>:algorithm</tt> options
@@ -38,25 +38,27 @@
# Example
#
# decrypted_value = Encryptor.decrypt(:value => 'some encrypted string', :key => 'some secret key')
# # or
# decrypted_value = Encryptor.decrypt('some encrypted string', :key => 'some secret key')
- def decrypt(*args)
- crypt :decrypt, *args
+ def decrypt(*args, &block)
+ crypt :decrypt, *args, &block
end
protected
def crypt(cipher_method, *args) #:nodoc:
options = default_options.merge(:value => args.first).merge(args.last.is_a?(Hash) ? args.last : {})
+ raise ArgumentError.new('must specify a :key') if options[:key].to_s.empty?
cipher = OpenSSL::Cipher::Cipher.new(options[:algorithm])
cipher.send(cipher_method)
if options[:iv]
cipher.key = options[:key]
cipher.iv = options[:iv]
else
cipher.pkcs5_keyivgen(options[:key])
end
+ yield cipher, options if block_given?
result = cipher.update(options[:value])
result << cipher.final
end
end
\ No newline at end of file