Class: SFax::Encryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/sfax/encryptor.rb

Constant Summary collapse

DEFAULT_IV =
'x49e*wJVXr8BrALE'.freeze
OPEN_SSL_CIPHER =
'aes-256-cbc'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, iv: DEFAULT_IV) ⇒ Encryptor

Returns a new instance of Encryptor.



9
10
11
12
# File 'lib/sfax/encryptor.rb', line 9

def initialize(key:, iv: DEFAULT_IV)
  @key = key.dup
  @iv = iv
end

Instance Attribute Details

#ivObject (readonly)

Returns the value of attribute iv.



14
15
16
# File 'lib/sfax/encryptor.rb', line 14

def iv
  @iv
end

#keyObject (readonly)

Returns the value of attribute key.



14
15
16
# File 'lib/sfax/encryptor.rb', line 14

def key
  @key
end

Instance Method Details

#encrypt(plain) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/sfax/encryptor.rb', line 16

def encrypt(plain)
  cipher = new_encrypt_cipher

  encrypted_data = cipher.update plain
  encrypted_data << cipher.final

  Base64.encode64 encrypted_data
end

#new_encrypt_cipherObject



25
26
27
28
29
30
31
32
# File 'lib/sfax/encryptor.rb', line 25

def new_encrypt_cipher
  OpenSSL::Cipher::Cipher.new(OPEN_SSL_CIPHER).tap do |c|
    c.encrypt

    c.key = key
    c.iv = iv
  end
end