Class: Encrypth::CLI
- Inherits:
-
Object
- Object
- Encrypth::CLI
- Defined in:
- lib/encrypth/cli.rb
Class Method Summary collapse
- .decrypt_to_directory(archive, destination) ⇒ Object
- .encrypt_files(files, archive) ⇒ Object
- .load_salt(archive) ⇒ Object
- .read_password(prompt) ⇒ Object
- .run(args) ⇒ Object
- .salt_path(archive) ⇒ Object
- .save_salt(archive, salt) ⇒ Object
Class Method Details
.decrypt_to_directory(archive, destination) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/encrypth/cli.rb', line 49 def self.decrypt_to_directory(archive, destination) password = read_password("Введите пароль: ") salt = load_salt(archive) cipher_data = Encrypth::Cipher.from_password(password, salt) cipher = Encrypth::Cipher.new(cipher_data[:key]) archiver = Encrypth::Archiver.new(cipher) archiver.decrypt_to_directory(archive, destination) end |
.encrypt_files(files, archive) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/encrypth/cli.rb', line 40 def self.encrypt_files(files, archive) password = read_password("Задайте пароль: ") cipher_data = Encrypth::Cipher.from_password(password) save_salt(archive, cipher_data[:salt]) cipher = Encrypth::Cipher.new(cipher_data[:key]) archiver = Encrypth::Archiver.new(cipher) archiver.encrypt_files(files, archive) end |
.load_salt(archive) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/encrypth/cli.rb', line 68 def self.load_salt(archive) salt_file = salt_path(archive) unless File.exist?(salt_file) raise "Файл соли не найден. Невозможно восстановить ключ для архива #{archive}." end Base64.decode64(File.read(salt_file)) end |
.read_password(prompt) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/encrypth/cli.rb', line 77 def self.read_password(prompt) print prompt $stdout.flush password = STDIN.noecho { |io| io.gets }&.chomp || "" puts password end |
.run(args) ⇒ Object
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 |
# File 'lib/encrypth/cli.rb', line 6 def self.run(args) if args.length < 1 puts "Использование: encrypth.rb <archive> <flag (-e/-d)> <...files/destination?>" exit(1) end archive = args[0] flag = args[1] if flag == "-e" if File.exist?(archive) puts "Архив #{archive} уже существует. Пожалуйста, выберите другое имя или удалите существующий архив." exit(1) end files = args[2..-1] encrypt_files(files, archive) elsif flag == "-d" if !File.exist?(archive) puts "Архив #{archive} не существует." exit(1) end destination = args[2] if destination && File.exist?(destination) && !File.directory?(destination) puts "Указанный путь #{destination} не является директорией." exit(1) end destination = "." if destination.nil? || destination.empty? decrypt_to_directory(archive, destination) else puts "Неверный флаг. Используйте -e для шифрования или -d для дешифрования." exit(1) end end |
.salt_path(archive) ⇒ Object
60 61 62 |
# File 'lib/encrypth/cli.rb', line 60 def self.salt_path(archive) "#{archive}.salt" end |
.save_salt(archive, salt) ⇒ Object
64 65 66 |
# File 'lib/encrypth/cli.rb', line 64 def self.save_salt(archive, salt) File.binwrite(salt_path(archive), Base64.strict_encode64(salt)) end |