Class: Lolitado::Box

Inherits:
Object
  • Object
show all
Defined in:
lib/lolitado/box.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Box

initialize box

Parameters:

  • key (String)

    ENV key value used for initialize box



14
15
16
17
18
# File 'lib/lolitado/box.rb', line 14

def initialize key
  fail "There's no environment variable #{key}..." if ENV[key].nil?
  key = Base64.decode64(ENV[key])
  @box = RbNaCl::SimpleBox.from_secret_key(key)
end

Instance Attribute Details

#boxObject

Returns the value of attribute box.



7
8
9
# File 'lib/lolitado/box.rb', line 7

def box
  @box
end

Instance Method Details

#file_decrypt(file) ⇒ Object

decrypt file

Parameters:

  • file (String)

    the file need to be decrypted



37
38
39
40
41
42
# File 'lib/lolitado/box.rb', line 37

def file_decrypt file
  ciphertext = File.read(file)
  plaintext = box.decrypt(Base64.decode64(ciphertext))
  plain_file = file[0..-5]
  File.write(plain_file, plaintext)
end

#file_encrypt(file) ⇒ Object

encrypt file

Parameters:

  • file (String)

    the file need to be encrypted



25
26
27
28
29
30
# File 'lib/lolitado/box.rb', line 25

def file_encrypt file
  plaintext = File.read(file)
  ciphertext = box.encrypt(plaintext)
  enc_file = file + '.enc'
  File.write(enc_file, Base64.encode64(ciphertext))
end