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

Constant Summary collapse

ENCODINGS =

Available encodings

[:none, :base64, :base64strict]

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 [String]
  The Symmetric Key to use for encryption and decryption

:iv [String]
  Optional. The Initialization Vector to use with Symmetric Key
  Highly Recommended as it is the input into the CBC algorithm

:cipher [String]
  Optional. Encryption Cipher to use
  Default: aes-256-cbc

:encoding [Symbol]
  :base64strict
    Return as a base64 encoded string that does not include additional newlines
    This is the recommended format since newlines in the values to
    SQL queries are cumbersome. Also the newline reformatting is unnecessary
    It is not the default for backward compatibility
  :base64
    Return as a base64 encoded string
  :binary
    Return as raw binary data string. Note: String can contain embedded nulls
  Default: :base64
  Recommended: :base64strict

:version [Fixnum]
  Optional. The version number of this encryption key
  Used by SymmetricEncryption to select the correct key when decrypting data


62
63
64
65
66
67
68
69
70
# File 'lib/symmetric_encryption/cipher.rb', line 62

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

  raise("Invalid Encoding: #{@encoding}") unless ENCODINGS.include?(@encoding)
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

#encodingObject

Returns the value of attribute encoding.



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

def encoding
  @encoding
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



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

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



122
123
124
# File 'lib/symmetric_encryption/cipher.rb', line 122

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

#decrypt(str) ⇒ Object



100
101
102
103
104
105
# File 'lib/symmetric_encryption/cipher.rb', line 100

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

#encrypt(str) ⇒ Object



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

def encrypt(str)
  return if str.nil?
  buf = str.to_s.encode(SymmetricEncryption::UTF8_ENCODING)
  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



117
118
119
# File 'lib/symmetric_encryption/cipher.rb', line 117

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