Module: UU::OS::Security::Encryption

Included in:
Session
Defined in:
uu_os_framework-0.29.16/lib/uu/os/security/encryption.rb

Overview

Provides data encryption and decryption.

Examples:

Basic usage - include allows to use Encryption methods in instance methods of a class.

require 'uu/os/security/encryption'

class Foo
  include UU::OS::Security::Encryption

  def bar(data)
    encrypted_data = encrypt(data)
  end

  def baz(data)
    decrypted_data = decrypt(data)
  end
end

Usage with class methods (singleton methods) - extend allows to use Encryption methods in class methods (but not in instance methods).

require 'uu/os/security/encryption'

class Foo
  extend UU::OS::Security::Encryption

  def self.bar(data)
    encrypted_data = encrypt(data)
  end

  def self.baz(data)
    decrypted_data = decrypt(data)
  end
end

Usage in a module - extend allows to use Encryption methods in module methods.

require 'uu/os/security/encryption'

module Foo
  extend UU::OS::Security::Encryption

  def bar(data)
    encrypted_data = encrypt(data)
  end

  def baz(data)
    decrypted_data = decrypt(data)
  end
end

Usage with both class methods (singleton methods) and instance methods.

require 'uu/os/security/encryption'

class Foo
  include UU::OS::Security::Encryption
  extend UU::OS::Security::Encryption

  def self.bar(data)
    encrypted_data = encrypt(data)
  end

  def baz(data)
    decrypted_data = decrypt(data)
  end
end

Constant Summary

Instance Method Summary (collapse)

Instance Method Details

- (String) decrypt(data, password = nil)

Decrypts the given data.

Parameters:

  • data (String)

    The data to decrypt.

  • password (String) (defaults to: nil)

    Password to be used for decryption (must be same as password for encryption, else decryption fails). In case no password is provided, default one is used.

Returns:

  • (String)

    The decrypted data.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'uu_os_framework-0.29.16/lib/uu/os/security/encryption.rb', line 98

def decrypt(data, password = nil)
  data = Base64.decode64(data)
  salt = data[8..15]
  data = data[16..-1]
  cipher = OpenSSL::Cipher.new(ALGORITHM)
  cipher.decrypt
  begin
    key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(password.nil? ? SECRET : password, salt, 1000, 128)
    cipher.key = key
    cipher.iv = key
    cipher.update(data) + cipher.final
  rescue
    cipher.reset
    cipher.pkcs5_keyivgen(SECRET, salt , 1)
    cipher.update(data) + cipher.final
  end
end

- (String) encrypt(data, password = nil)

Encrypts the given data.

Parameters:

  • data (String)

    The data to encrypt.

  • password (String) (defaults to: nil)

    Password to be used for encryption. In case no password is provided, default one is used meaning that decryption can be done by anybody using decrypt method.

Returns:

  • (String)

    The encrypted data.



79
80
81
82
83
84
85
86
87
88
89
# File 'uu_os_framework-0.29.16/lib/uu/os/security/encryption.rb', line 79

def encrypt(data, password = nil)
  salt = ''
  8.times {salt << rand(255).chr}
  cipher = OpenSSL::Cipher.new(ALGORITHM)
  cipher.encrypt
  key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(password.nil? ? SECRET : password, salt, 1000, 128)          
  cipher.key = key
  cipher.iv = key
  tmp = cipher.update(data) + cipher.final
  Base64.strict_encode64("Salted__#{salt}#{tmp}")
end