Class: LightGptProxy::Guard

Inherits:
Object
  • Object
show all
Defined in:
lib/light_gpt_proxy/guard.rb

Defined Under Namespace

Classes: DecryptionError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Guard

Returns a new instance of Guard.



13
14
15
16
# File 'lib/light_gpt_proxy/guard.rb', line 13

def initialize(key)
  @key = Digest::SHA256.hexdigest(key)
  @lockbox = Lockbox.new(key: @key)
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



11
12
13
# File 'lib/light_gpt_proxy/guard.rb', line 11

def key
  @key
end

Instance Method Details

#decode(filepath) ⇒ Object



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

def decode(filepath)
  encrypted_content = File.read(filepath)
  begin
    decrypted_content = @lockbox.decrypt(encrypted_content)
    YAML.safe_load(decrypted_content)
  rescue Lockbox::DecryptionError
    raise DecryptionError, 'Failed to decrypt file. Incorrect password or corrupted file.'
  end
end

#encode(data, filepath) ⇒ Object



18
19
20
21
22
# File 'lib/light_gpt_proxy/guard.rb', line 18

def encode(data, filepath)
  yaml_content = data.to_yaml
  encrypted_content = @lockbox.encrypt(yaml_content)
  File.write(filepath, encrypted_content)
end