diff lib/redmine/ciphering.rb @ 909:cbb26bc654de redmine-1.3

Update to Redmine 1.3-stable branch (Redmine SVN rev 8964)
author Chris Cannam
date Fri, 24 Feb 2012 19:09:32 +0000
parents 051f544170fe
children 433d4f72a19b
line wrap: on
line diff
--- a/lib/redmine/ciphering.rb	Fri Feb 24 18:36:29 2012 +0000
+++ b/lib/redmine/ciphering.rb	Fri Feb 24 19:09:32 2012 +0000
@@ -17,13 +17,13 @@
 
 module Redmine
   module Ciphering
-    def self.included(base) 
+    def self.included(base)
       base.extend ClassMethods
     end
-    
+
     class << self
       def encrypt_text(text)
-        if cipher_key.blank?
+        if cipher_key.blank? || text.blank?
           text
         else
           c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
@@ -36,9 +36,13 @@
           "aes-256-cbc:" + [e, iv].map {|v| Base64.encode64(v).strip}.join('--')
         end
       end
-      
+
       def decrypt_text(text)
         if text && match = text.match(/\Aaes-256-cbc:(.+)\Z/)
+          if cipher_key.blank?
+            logger.error "Attempt to decrypt a ciphered text with no cipher key configured in config/configuration.yml" if logger
+            return text
+          end
           text = match[1]
           c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
           e, iv = text.split("--").map {|s| Base64.decode64(s)}
@@ -51,13 +55,17 @@
           text
         end
       end
-      
+
       def cipher_key
         key = Redmine::Configuration['database_cipher_key'].to_s
         key.blank? ? nil : Digest::SHA256.hexdigest(key)
       end
+      
+      def logger
+        Rails.logger
+      end
     end
-  
+
     module ClassMethods
       def encrypt_all(attribute)
         transaction do
@@ -68,7 +76,7 @@
           end
         end ? true : false
       end
-      
+
       def decrypt_all(attribute)
         transaction do
           all.each do |object|
@@ -79,14 +87,14 @@
         end
       end ? true : false
     end
-    
+
     private
-    
+
     # Returns the value of the given ciphered attribute
     def read_ciphered_attribute(attribute)
       Redmine::Ciphering.decrypt_text(read_attribute(attribute))
     end
-    
+
     # Sets the value of the given ciphered attribute
     def write_ciphered_attribute(attribute, value)
       write_attribute(attribute, Redmine::Ciphering.encrypt_text(value))