@@ -17,10 +17,10 | |||||
17 |
|
17 | |||
18 | module Redmine |
|
18 | module Redmine | |
19 | module Ciphering |
|
19 | module Ciphering | |
20 |
def self.included(base) |
|
20 | def self.included(base) | |
21 | base.extend ClassMethods |
|
21 | base.extend ClassMethods | |
22 | end |
|
22 | end | |
23 |
|
23 | |||
24 | class << self |
|
24 | class << self | |
25 | def encrypt_text(text) |
|
25 | def encrypt_text(text) | |
26 | if cipher_key.blank? |
|
26 | if cipher_key.blank? | |
@@ -36,7 +36,7 module Redmine | |||||
36 | "aes-256-cbc:" + [e, iv].map {|v| Base64.encode64(v).strip}.join('--') |
|
36 | "aes-256-cbc:" + [e, iv].map {|v| Base64.encode64(v).strip}.join('--') | |
37 | end |
|
37 | end | |
38 | end |
|
38 | end | |
39 |
|
39 | |||
40 | def decrypt_text(text) |
|
40 | def decrypt_text(text) | |
41 | if text && match = text.match(/\Aaes-256-cbc:(.+)\Z/) |
|
41 | if text && match = text.match(/\Aaes-256-cbc:(.+)\Z/) | |
42 | text = match[1] |
|
42 | text = match[1] | |
@@ -51,13 +51,13 module Redmine | |||||
51 | text |
|
51 | text | |
52 | end |
|
52 | end | |
53 | end |
|
53 | end | |
54 |
|
54 | |||
55 | def cipher_key |
|
55 | def cipher_key | |
56 | key = Redmine::Configuration['database_cipher_key'].to_s |
|
56 | key = Redmine::Configuration['database_cipher_key'].to_s | |
57 | key.blank? ? nil : Digest::SHA256.hexdigest(key) |
|
57 | key.blank? ? nil : Digest::SHA256.hexdigest(key) | |
58 | end |
|
58 | end | |
59 | end |
|
59 | end | |
60 |
|
60 | |||
61 | module ClassMethods |
|
61 | module ClassMethods | |
62 | def encrypt_all(attribute) |
|
62 | def encrypt_all(attribute) | |
63 | transaction do |
|
63 | transaction do | |
@@ -68,7 +68,7 module Redmine | |||||
68 | end |
|
68 | end | |
69 | end ? true : false |
|
69 | end ? true : false | |
70 | end |
|
70 | end | |
71 |
|
71 | |||
72 | def decrypt_all(attribute) |
|
72 | def decrypt_all(attribute) | |
73 | transaction do |
|
73 | transaction do | |
74 | all.each do |object| |
|
74 | all.each do |object| | |
@@ -79,14 +79,14 module Redmine | |||||
79 | end |
|
79 | end | |
80 | end ? true : false |
|
80 | end ? true : false | |
81 | end |
|
81 | end | |
82 |
|
82 | |||
83 | private |
|
83 | private | |
84 |
|
84 | |||
85 | # Returns the value of the given ciphered attribute |
|
85 | # Returns the value of the given ciphered attribute | |
86 | def read_ciphered_attribute(attribute) |
|
86 | def read_ciphered_attribute(attribute) | |
87 | Redmine::Ciphering.decrypt_text(read_attribute(attribute)) |
|
87 | Redmine::Ciphering.decrypt_text(read_attribute(attribute)) | |
88 | end |
|
88 | end | |
89 |
|
89 | |||
90 | # Sets the value of the given ciphered attribute |
|
90 | # Sets the value of the given ciphered attribute | |
91 | def write_ciphered_attribute(attribute, value) |
|
91 | def write_ciphered_attribute(attribute, value) | |
92 | write_attribute(attribute, Redmine::Ciphering.encrypt_text(value)) |
|
92 | write_attribute(attribute, Redmine::Ciphering.encrypt_text(value)) |
General Comments 0
You need to be logged in to leave comments.
Login now