Class: SymmetricEncryption::Cipher

Inherits:
Object
  • Object
show all
Defined in:
lib/symmetric_encryption/cipher.rb

Overview

Hold all information related to encryption keys as well as encrypt and decrypt data using those keys

Cipher is thread safe so that the same instance can be called by multiple threads at the same time without needing an instance of Cipher per thread

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parms = {}) ⇒ Cipher

Create a Symmetric::Key for encryption and decryption purposes

Parameters:

:key
  The Symmetric Key to use for encryption and decryption
:iv
  Optional. The Initialization Vector to use with Symmetric Key
:cipher
  Optional. Encryption Cipher to use
  Default: aes-256-cbc


41
42
43
44
45
46
# File 'lib/symmetric_encryption/cipher.rb', line 41

def initialize(parms={})
  raise "Missing mandatory parameter :key" unless @key = parms[:key]
  @iv = parms[:iv]
  @cipher = parms[:cipher] || 'aes-256-cbc'
  @version = parms[:version]
end

Instance Attribute Details

#cipherObject (readonly)

Cipher to use for encryption and decryption



10
11
12
# File 'lib/symmetric_encryption/cipher.rb', line 10

def cipher
  @cipher
end

#versionObject (readonly)

Cipher to use for encryption and decryption



10
11
12
# File 'lib/symmetric_encryption/cipher.rb', line 10

def version
  @version
end

Class Method Details

.random_key_pair(cipher = 'aes-256-cbc', generate_iv = true) ⇒ Object

Generate a new Symmetric Key pair

Returns a hash containing a new random symmetric_key pair consisting of a :key and :iv. The cipher is also included for compatibility with the Cipher initializer



20
21
22
23
24
25
26
27
28
29
# File 'lib/symmetric_encryption/cipher.rb', line 20

def self.random_key_pair(cipher = 'aes-256-cbc', generate_iv = true)
  openssl_cipher = OpenSSL::Cipher.new(cipher)
  openssl_cipher.encrypt

  {
    :key    => openssl_cipher.random_key,
    :iv     => generate_iv ? openssl_cipher.random_iv : nil,
    :cipher => cipher
  }
end

Instance Method Details

#block_sizeObject

Returns the block size for the configured cipher



86
87
88
# File 'lib/symmetric_encryption/cipher.rb', line 86

def block_size
  ::OpenSSL::Cipher::Cipher.new(@cipher).block_size
end

#decrypt(str) ⇒ Object

AES Symmetric Decryption of supplied string

Returns decrypted string
Returns nil if the supplied str is nil
Returns "" if it is a string and it is empty


72
73
74
75
76
77
# File 'lib/symmetric_encryption/cipher.rb', line 72

def decrypt(str)
  return if str.nil?
  buf = str.to_s
  return str if buf.empty?
  crypt(:decrypt, buf)
end

#encrypt(str) ⇒ Object

AES Symmetric Encryption of supplied string

Returns result as a Base64 encoded string
Returns nil if the supplied str is nil
Returns "" if it is a string and it is empty

options:
  :encoding
     :base64 Return as a base64 encoded string
     :binary Return as raw binary data string. Note: String can contain embedded nulls
    Default: :base64
  :compress
    [true|false] Whether or not to compress the data _before_ encrypting
    Default: false


61
62
63
64
65
66
# File 'lib/symmetric_encryption/cipher.rb', line 61

def encrypt(str)
  return if str.nil?
  buf = str.to_s
  return str if buf.empty?
  crypt(:encrypt, buf)
end

#random_keyObject

Return a new random key using the configured cipher Useful for generating new symmetric keys



81
82
83
# File 'lib/symmetric_encryption/cipher.rb', line 81

def random_key
  ::OpenSSL::Cipher::Cipher.new(@cipher).random_key
end