Module: StringEncryption

Defined in:
lib/string_encryption/encryption.rb,
lib/string_encryption/secure_random.rb

Defined Under Namespace

Modules: SecureRandom

Class Method Summary collapse

Class Method Details

.decrypt(encrypted_data, decryption_key = ENV['LIB_STRING_ENCRYPTION_KEY']) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/string_encryption/encryption.rb', line 5

def self.decrypt(encrypted_data, decryption_key=ENV['LIB_STRING_ENCRYPTION_KEY'])
  des = OpenSSL::Cipher::Cipher.new("des-ede3-cbc")
  des.decrypt
  des.key = decryption_key
  encrypted_data = URI.unescape(encrypted_data)
  encrypted_data = Base64.decode64(encrypted_data)
  
  des.iv =  encrypted_data.slice!(0,8)
  
  des.update(encrypted_data) + des.final  
end

.encrypt(string, encryption_key = ENV['LIB_STRING_ENCRYPTION_KEY']) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/string_encryption/encryption.rb', line 17

def self.encrypt(string, encryption_key=ENV['LIB_STRING_ENCRYPTION_KEY'])
  des = OpenSSL::Cipher::Cipher.new("des-ede3-cbc")
  des.encrypt
  des.key = encryption_key
  
  des.iv = iv = SecureRandom.hex(4)
  
  data = des.update(string) + des.final  
  data = iv + data
  data = Base64.encode64(data)
  data = URI.escape(data, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
end