Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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