Module: InlineEncryption::Base

Included in:
InlineEncryption
Defined in:
lib/inline_encryption/base.rb

Instance Method Summary collapse

Instance Method Details

#configInlineEncryption::Config

Returns the configuration instance.

Returns:



67
68
69
# File 'lib/inline_encryption/base.rb', line 67

def config
  @config ||= Config.new
end

#decrypt(data, fail_text = nil) ⇒ String

Returns decrypted target.

Parameters:

  • decryption (String)

    target

  • text (String)

    to be returned in the case of a decryption failure

Returns:

  • (String)

    decrypted target



55
56
57
58
59
60
61
62
63
# File 'lib/inline_encryption/base.rb', line 55

def decrypt(data, fail_text=nil)
  config.check_required_variables

  begin
    decrypt!(data)
  rescue DecryptionFailureError => e
    return fail_text.nil? ? data : fail_text.to_s
  end
end

#decrypt!(data) ⇒ String

Returns decrypted target.

Parameters:

  • data (String)

    decryption target

Returns:

  • (String)

    decrypted target

Raises:



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/inline_encryption/base.rb', line 38

def decrypt!(data)
  config.check_required_variables

  begin
    converted = Base64.decode64(data)
    this_key = config.real_key.private? ? config.real_key.public_key : config.real_key
    decrypted = this_key.public_decrypt(converted)
  rescue => e
    err = DecryptionFailureError.exception "Encrypted: #{data}"
    raise err
  end
end

#encrypt(data, fail_text = nil) ⇒ String

Returns encrypted target, or fail_text on error (default data).

Parameters:

  • encryption (String)

    target

Returns:

  • (String)

    encrypted target, or fail_text on error (default data)



24
25
26
27
28
29
30
31
32
# File 'lib/inline_encryption/base.rb', line 24

def encrypt(data, fail_text=nil)
  config.check_required_variables

  begin
    encrypt!(data)
  rescue EncryptionFailureError => e
    return fail_text.nil? ? data : fail_text.to_s
  end
end

#encrypt!(data) ⇒ String

Returns encrypted target.

Parameters:

  • data (String)

    encryption target

Returns:

  • (String)

    encrypted target

Raises:



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/inline_encryption/base.rb', line 9

def encrypt!(data)
  config.check_required_variables

  begin
    encrypted = config.real_key.private_encrypt(data)
    converted = Base64.encode64(encrypted)
  rescue => e
    err = EncryptionFailureError.exception "Target: #{data}"
    raise err
  end
end