annotate lib/redmine/.svn/text-base/ciphering.rb.svn-base @ 855:7294e8db2515 bug_162

Close obsolete branch bug_162
author Chris Cannam
date Thu, 14 Jul 2011 11:59:19 +0100
parents 051f544170fe
children
rev   line source
Chris@245 1 # Redmine - project management software
Chris@245 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@245 3 #
Chris@245 4 # This program is free software; you can redistribute it and/or
Chris@245 5 # modify it under the terms of the GNU General Public License
Chris@245 6 # as published by the Free Software Foundation; either version 2
Chris@245 7 # of the License, or (at your option) any later version.
Chris@245 8 #
Chris@245 9 # This program is distributed in the hope that it will be useful,
Chris@245 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@245 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@245 12 # GNU General Public License for more details.
Chris@245 13 #
Chris@245 14 # You should have received a copy of the GNU General Public License
Chris@245 15 # along with this program; if not, write to the Free Software
Chris@245 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@245 17
Chris@245 18 module Redmine
Chris@245 19 module Ciphering
Chris@245 20 def self.included(base)
Chris@245 21 base.extend ClassMethods
Chris@245 22 end
Chris@245 23
Chris@245 24 class << self
Chris@245 25 def encrypt_text(text)
Chris@245 26 if cipher_key.blank?
Chris@245 27 text
Chris@245 28 else
Chris@245 29 c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
Chris@245 30 iv = c.random_iv
Chris@245 31 c.encrypt
Chris@245 32 c.key = cipher_key
Chris@245 33 c.iv = iv
Chris@245 34 e = c.update(text.to_s)
Chris@245 35 e << c.final
Chris@245 36 "aes-256-cbc:" + [e, iv].map {|v| Base64.encode64(v).strip}.join('--')
Chris@245 37 end
Chris@245 38 end
Chris@245 39
Chris@245 40 def decrypt_text(text)
Chris@245 41 if text && match = text.match(/\Aaes-256-cbc:(.+)\Z/)
Chris@245 42 text = match[1]
Chris@245 43 c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
Chris@245 44 e, iv = text.split("--").map {|s| Base64.decode64(s)}
Chris@245 45 c.decrypt
Chris@245 46 c.key = cipher_key
Chris@245 47 c.iv = iv
Chris@245 48 d = c.update(e)
Chris@245 49 d << c.final
Chris@245 50 else
Chris@245 51 text
Chris@245 52 end
Chris@245 53 end
Chris@245 54
Chris@245 55 def cipher_key
Chris@245 56 key = Redmine::Configuration['database_cipher_key'].to_s
Chris@245 57 key.blank? ? nil : Digest::SHA256.hexdigest(key)
Chris@245 58 end
Chris@245 59 end
Chris@245 60
Chris@245 61 module ClassMethods
Chris@245 62 def encrypt_all(attribute)
Chris@245 63 transaction do
Chris@245 64 all.each do |object|
Chris@245 65 clear = object.send(attribute)
Chris@245 66 object.send "#{attribute}=", clear
Chris@245 67 raise(ActiveRecord::Rollback) unless object.save(false)
Chris@245 68 end
Chris@245 69 end ? true : false
Chris@245 70 end
Chris@245 71
Chris@245 72 def decrypt_all(attribute)
Chris@245 73 transaction do
Chris@245 74 all.each do |object|
Chris@245 75 clear = object.send(attribute)
Chris@245 76 object.write_attribute attribute, clear
Chris@245 77 raise(ActiveRecord::Rollback) unless object.save(false)
Chris@245 78 end
Chris@245 79 end
Chris@245 80 end ? true : false
Chris@245 81 end
Chris@245 82
Chris@245 83 private
Chris@245 84
Chris@245 85 # Returns the value of the given ciphered attribute
Chris@245 86 def read_ciphered_attribute(attribute)
Chris@245 87 Redmine::Ciphering.decrypt_text(read_attribute(attribute))
Chris@245 88 end
Chris@245 89
Chris@245 90 # Sets the value of the given ciphered attribute
Chris@245 91 def write_ciphered_attribute(attribute, value)
Chris@245 92 write_attribute(attribute, Redmine::Ciphering.encrypt_text(value))
Chris@245 93 end
Chris@245 94 end
Chris@245 95 end