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