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?
"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/)
text = match[1]
text
end
end
-
+
def cipher_key
key = Redmine::Configuration['database_cipher_key'].to_s
key.blank? ? nil : Digest::SHA256.hexdigest(key)
end
end
-
+
module ClassMethods
def encrypt_all(attribute)
transaction do
end
end ? true : false
end
-
+
def decrypt_all(attribute)
transaction do
all.each do |object|
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))