Class: Rtprov::Encryption

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

Constant Summary collapse

KEY_FILE =
"encryption_key".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Encryption

Returns a new instance of Encryption.



26
27
28
# File 'lib/rtprov/encryption.rb', line 26

def initialize(key)
  @key = key.dup.freeze
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



8
9
10
# File 'lib/rtprov/encryption.rb', line 8

def key
  @key
end

Class Method Details

.decrypt(encrypted, key = load_key) ⇒ Object



18
19
20
# File 'lib/rtprov/encryption.rb', line 18

def self.decrypt(encrypted, key = load_key)
  new(key).decrypt(encrypted)
end

.encrypt(plain, key = load_key) ⇒ Object



14
15
16
# File 'lib/rtprov/encryption.rb', line 14

def self.encrypt(plain, key = load_key)
  new(key).encrypt(plain)
end

.generate_keyObject



22
23
24
# File 'lib/rtprov/encryption.rb', line 22

def self.generate_key
  SecureRandom.base64(512)
end

.load_keyObject



10
11
12
# File 'lib/rtprov/encryption.rb', line 10

def self.load_key
  ENV["ENCRYPTION_KEY"] || (File.exist?(KEY_FILE) && File.read(KEY_FILE).strip) || raise("ENCRYPTION_KEY env or encryption_key file not found")
end

Instance Method Details

#decrypt(encrypted) ⇒ Object



34
35
36
# File 'lib/rtprov/encryption.rb', line 34

def decrypt(encrypted)
  ReversibleCryptography::Message.decrypt(encrypted, key)
end

#encrypt(plain) ⇒ Object



30
31
32
# File 'lib/rtprov/encryption.rb', line 30

def encrypt(plain)
  ReversibleCryptography::Message.encrypt(plain, key)
end