Class: EJSON::Encryption

Inherits:
Object
  • Object
show all
Defined in:
lib/ejson/encryption.rb

Constant Summary collapse

PrivateKeyMissing =
Class.new(StandardError)
ExpectedEncryptedString =
Class.new(StandardError)
ENCRYPTED =
/\AENC\[(.*)\]\n*\z/m

Instance Method Summary collapse

Constructor Details

#initialize(public_key_file, private_key_file) ⇒ Encryption

Returns a new instance of Encryption.



10
11
12
13
14
15
# File 'lib/ejson/encryption.rb', line 10

def initialize(public_key_file, private_key_file)
  @public_key_x509 = load_public_key(public_key_file)
  if private_key_file
    @private_key_rsa = load_private_key(private_key_file)
  end
end

Instance Method Details

#dump(str) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/ejson/encryption.rb', line 27

def dump(str)
  if str =~ ENCRYPTED
    str
  else
    "ENC[#{encrypt_string(str)}]"
  end
end

#load(str) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/ejson/encryption.rb', line 19

def load(str)
  if str =~ ENCRYPTED
    decrypt_string($1)
  else
    raise ExpectedEncryptedString
  end
end