Module: Redmine::Ciphering

Included in:
AuthSource, Repository
Defined in:
lib/redmine/ciphering.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.cipher_keyObject



52
53
54
55
# File 'lib/redmine/ciphering.rb', line 52

def cipher_key
  key = Redmine::Configuration['database_cipher_key'].to_s
  key.blank? ? nil : Digest::SHA256.hexdigest(key)
end

.decrypt_text(text) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/redmine/ciphering.rb', line 37

def decrypt_text(text)
  if text && match = text.match(/\Aaes-256-cbc:(.+)\Z/)
    text = match[1]
    c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
    e, iv = text.split("--").map {|s| Base64.decode64(s)}
    c.decrypt
    c.key = cipher_key
    c.iv = iv
    d = c.update(e)
    d << c.final
  else
    text
  end
end

.encrypt_text(text) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/redmine/ciphering.rb', line 22

def encrypt_text(text)
  if cipher_key.blank?
    text
  else
    c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
    iv = c.random_iv
    c.encrypt
    c.key = cipher_key
    c.iv = iv
    e = c.update(text.to_s)
    e << c.final
    "aes-256-cbc:" + [e, iv].map {|v| Base64.encode64(v).strip}.join('--')
  end
end

.included(base) ⇒ Object



17
18
19
# File 'lib/redmine/ciphering.rb', line 17

def self.included(base)
  base.extend ClassMethods
end