Class: Serket::FieldDecrypter

Inherits:
Object
  • Object
show all
Defined in:
lib/serket/field_decrypter.rb

Overview

Used to decrypt a field given a private key, field delimiter, symmetric algorithm, encoding, and format (:json or :delimited)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FieldDecrypter

Returns a new instance of FieldDecrypter.



10
11
12
13
14
15
16
17
18
# File 'lib/serket/field_decrypter.rb', line 10

def initialize(options = {})
  options ||= {}

  @private_key_filepath   = Serket.configuration.private_key_path
  @field_delimiter        = options[:field_delimiter]     || Serket.configuration.delimiter 
  @symmetric_algorithm    = options[:symmetric_algorithm] || Serket.configuration.symmetric_algorithm
  @format                 = options[:format]              || Serket.configuration.format
  @encoding               = options[:encoding]            || Serket.configuration.encoding
end

Instance Attribute Details

#encodingObject

Returns the value of attribute encoding.



8
9
10
# File 'lib/serket/field_decrypter.rb', line 8

def encoding
  @encoding
end

#field_delimiterObject

Returns the value of attribute field_delimiter.



8
9
10
# File 'lib/serket/field_decrypter.rb', line 8

def field_delimiter
  @field_delimiter
end

#private_key_filepathObject

Returns the value of attribute private_key_filepath.



8
9
10
# File 'lib/serket/field_decrypter.rb', line 8

def private_key_filepath
  @private_key_filepath
end

#symmetric_algorithmObject

Returns the value of attribute symmetric_algorithm.



8
9
10
# File 'lib/serket/field_decrypter.rb', line 8

def symmetric_algorithm
  @symmetric_algorithm
end

Instance Method Details

#decrypt(field) ⇒ Object

Decrypt the provided cipher text, and return the plaintext Return nil if whitespace



22
23
24
25
26
27
28
29
# File 'lib/serket/field_decrypter.rb', line 22

def decrypt(field)
  return if field !~ /\S/
  iv, encrypted_aes_key, encrypted_text = parse(field)
  private_key = OpenSSL::PKey::RSA.new(File.read(private_key_filepath))
  decrypted_aes_key = private_key.private_decrypt(Base64.decode64(encrypted_aes_key))
  decrypted_field = decrypt_data(iv, decrypted_aes_key, encrypted_text)
  decrypted_field.force_encoding(encoding)
end