Module: SlackSmartBot::Utils::Encryption
- Defined in:
- lib/slack/smart-bot/utils/encryption/decrypt.rb,
lib/slack/smart-bot/utils/encryption/encrypt.rb,
lib/slack/smart-bot/utils/encryption/encryption_get_key_iv.rb
Class Method Summary collapse
- .decrypt(data, config) ⇒ Object
- .encrypt(data, config) ⇒ Object
- .encryption_get_key_iv(config) ⇒ Object
Class Method Details
.decrypt(data, config) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/slack/smart-bot/utils/encryption/decrypt.rb', line 4 def self.decrypt(data, config) if config.encrypt or (config.key?(:recover_encrypted) and config[:recover_encrypted]) require "openssl" require "base64" if data == '' plain = '' else begin key, iv = Utils::Encryption.encryption_get_key_iv(config) encrypted = Base64.decode64(data) cipher = OpenSSL::Cipher.new("AES-256-CBC") cipher.decrypt cipher.key = key cipher.iv = iv plain = cipher.update(encrypted) + cipher.final rescue Exception => stack return data end end return plain else return data end end |
.encrypt(data, config) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/slack/smart-bot/utils/encryption/encrypt.rb', line 4 def self.encrypt(data, config) if config.encrypt require "openssl" require "base64" if data == '' encrypted = '' else key, iv = Utils::Encryption.encryption_get_key_iv(config) cipher = OpenSSL::Cipher::Cipher.new "AES-256-CBC" cipher.encrypt cipher.key = key cipher.iv = iv encrypted = cipher.update(data) + cipher.final encrypted = Base64.encode64(encrypted) if defined?(Thread.current) Thread.current[:encrypted] ||= [] Thread.current[:encrypted] << data end end return encrypted else return data end end |
.encryption_get_key_iv(config) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/slack/smart-bot/utils/encryption/encryption_get_key_iv.rb', line 4 def self.encryption_get_key_iv(config) if defined?(@encryption_key_built) key = @encryption_key_built iv = @encryption_iv_built else if config.key?(:encryption) and config.encryption.key?(:key) and config.encryption.key?(:iv) key = config[:encryption][:key] iv = config[:encryption][:iv] else key = (Socket.gethostname + config.token.reverse)[0..49] iv = config.token[0..15] end #Convert from hex to raw bytes: key = [key].pack("H*") #Pad with zero bytes to correct length: key << ("\x00" * (32 - key.length)) #Convert from hex to raw bytes: iv = [iv].pack("H*") #Pad with zero bytes to correct length: iv << ("\x00" * (16 - iv.length)) @encryption_key_built = key @encryption_iv_built = iv end return key, iv end |