-
Notifications
You must be signed in to change notification settings - Fork 45
/
encryption.rb
52 lines (42 loc) · 1.46 KB
/
encryption.rb
1
2
3
4
5
6
7
8
9
10
11
12
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
45
46
47
48
49
50
51
52
require 'openssl'
# Encrypts all files of a folder
#
# @param folder Folder of the certificate and provisioning profile
# @param key Encryption key for the certificate and provisioning profile
def encrypt(folder, key)
Dir.each_child(folder) do | child |
unless File.extname(child) != ".enc"
next
end
cipher = OpenSSL::Cipher::AES256.new :CBC
cipher.encrypt
cipher.key = Digest::SHA256.digest key
file = File.absolute_path(child, folder)
content = File.open(file, "rb").read
encrypted_content = cipher.update(content) + cipher.final
output_file = File.absolute_path(child + ".enc", folder)
File.open(output_file, "w").write(encrypted_content)
end
end
# Decrypts all files of a folder
#
# @param folder Folder of the certificate and provisioning profile
# @param key Decryption key for the certificate and provisioning profile
def decrypt(folder, key)
Dir.each_child(folder) do | child |
unless File.extname(child) == ".enc"
next
end
puts child
decipher = OpenSSL::Cipher::AES256.new :CBC
decipher.decrypt
decipher.key = Digest::SHA256.digest key
file = File.absolute_path(child, folder)
content = File.open(file, "rb").read
decrypted_content = decipher.update(content) + decipher.final
output_file_path = File.absolute_path(File.basename(child, ".enc"), folder)
output_file = File.open(output_file_path, "w")
output_file.sync = true
output_file.write(decrypted_content)
end
end