13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/ruar/serialize.rb', line 13
def self.aead(srcdir, dstfile)
time = Time.now.strftime('%Y%m%dT%H%M')
tmpdir = File.expand_path("ruar_#{time}", Dir.tmpdir)
FileUtils.copy_entry(srcdir, tmpdir)
encryption_info = nil
index = Ruar::Index.new(tmpdir) do |file|
data = File.read(file)
encryption_info = Ruar.cipher.encrypt(data)
File.write(file, encryption_info[:encrypted])
end
encrypted_index = Ruar.cipher.encrypt(index.json_index)[:encrypted]
Ruar::Serialize::Native.(dstfile, encrypted_index)
index.source_info.each do |src|
Ruar::Serialize::Native.append_file(dstfile, src['realpath'])
end
setup_file = <<~SETUP
Ruar.cipher.setup(
iv: '#{Base64.encode64(encryption_info[:iv]).chomp}',
key: '#{Base64.encode64(encryption_info[:key]).chomp}',
auth_data: '#{Base64.encode64(encryption_info[:auth_data]).chomp}',
tag: '#{Base64.encode64(encryption_info[:tag]).chomp}')
Ruar.cipher.enable
SETUP
File.write("#{dstfile}.setup", setup_file)
FileUtils.rm_rf(tmpdir)
end
|