Module: Safebox

Defined in:
lib/safebox.rb,
lib/safebox/cli.rb,
lib/safebox/hash.rb,
lib/safebox/file.rb,
lib/safebox/version.rb

Defined Under Namespace

Classes: CLI, File, Hash

Constant Summary collapse

BOX_VERSION =
1
CPU_COST =
2 ** 20
MEM_COST =
2 ** 24
PACK_FORMAT =
"Q<a32a*"
VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.decrypt(password, encrypted) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/safebox.rb', line 20

def decrypt(password, encrypted)
  version, salt, ciphertext = encrypted.unpack(PACK_FORMAT)

  unless version == BOX_VERSION
    raise ArgumentError, "bad box version #{version.inspect}"
  end

  box(password, salt).decrypt(ciphertext)
end

.encrypt(password, message) ⇒ Object



13
14
15
16
17
18
# File 'lib/safebox.rb', line 13

def encrypt(password, message)
  salt = RbNaCl::Random.random_bytes(RbNaCl::SecretBox.key_bytes)
  ciphertext = box(password, salt).encrypt(message)

  [BOX_VERSION, salt, ciphertext].pack(PACK_FORMAT)
end