Class: Classifieds::Main
- Inherits:
-
Thor
- Object
- Thor
- Classifieds::Main
- Defined in:
- lib/classifieds/main.rb
Instance Method Summary collapse
- #decrypt ⇒ Object
- #encrypt ⇒ Object
-
#initialize(*args) ⇒ Main
constructor
A new instance of Main.
- #keygen ⇒ Object
- #status ⇒ Object
Constructor Details
#initialize(*args) ⇒ Main
Returns a new instance of Main.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/classifieds/main.rb', line 15 def initialize(*args) unless classifieds? STDERR.puts "#{SOURCE_FILE} is not found in this repository".color(:red) exit 1 end source_directory = File.join(root_directory, SOURCE_DIRECTORY) FileUtils.mkdir_p(source_directory) unless Dir.exists?(source_directory) @prefix = Digest::SHA1.hexdigest('classifieds') super end |
Instance Method Details
#decrypt ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/classifieds/main.rb', line 84 def decrypt if identity_file = [:identity_file] rsa = OpenSSL::PKey::RSA.new(File.read(identity_file).chomp) @password = rsa.private_decrypt(File.read(File.join(root_directory, COMMON_KEY_PATH)).chomp) else @password = ask_password end failed_files = [] decrypted_files = classifieds.each_with_object([]) do |file_path, array| next if decrypted?(file_path) file = File.open(file_path, 'r+') file.flock(File::LOCK_EX) file.read(@prefix.size) data = file.read.chomp begin decrypted = decrypt_data(data) rescue OpenSSL::Cipher::CipherError failed_files << file_path next end file.rewind file.write decrypted file.truncate(file.tell) array << file_path end puts 'Decrypted:'.color(:green) unless decrypted_files.empty? decrypted_files.each {|decrypted_file| puts "\t" + decrypted_file } puts 'Failed to decrypt:'.color(:red) unless failed_files.empty? failed_files.each {|failed_file| puts "\t" + failed_file } end |
#encrypt ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/classifieds/main.rb', line 49 def encrypt if identity_file = [:identity_file] rsa = OpenSSL::PKey::RSA.new(File.read(identity_file).chomp) @password = rsa.private_decrypt(File.read(File.join(root_directory, COMMON_KEY_PATH)).chomp) else @password = ask_password retype_password end encrypted_files = classifieds.each_with_object([]) do |file_path, array| next if encrypted?(file_path) file = File.open(file_path, 'r+') file.flock(File::LOCK_EX) data = file.read.chomp begin encrypted = encrypt_data(data) rescue ArgumentError next end file.rewind file.write @prefix + encrypted file.truncate(file.tell) array << file_path end puts 'Encrypted:'.color(:green) unless encrypted_files.empty? encrypted_files.each {|encrypted_file| puts "\t" + encrypted_file } end |
#keygen ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/classifieds/main.rb', line 29 def keygen if ![:force] && keygenerated? STDERR.puts 'Already generated in this repository'.color(:red) exit 1 else OpenSSL::Random.seed(File.read('/dev/random', 16)) rsa = OpenSSL::PKey::RSA.new(2048) pub = rsa.public_key File.open(File.join(root_directory, PUBLIC_KEY_PATH), 'w') do |f| f.puts pub.to_pem end File.open(File.join(root_directory, COMMON_KEY_PATH), 'w') do |f| f.puts pub.public_encrypt(OpenSSL::Random.random_bytes(16)) end puts rsa end end |
#status ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/classifieds/main.rb', line 122 def status encrypted_files = [] unencrypted_files = [] classifieds.each do |file| if encrypted?(file) encrypted_files << file else unencrypted_files << file end end puts 'Encrypted:'.color(:green) unless encrypted_files.empty? encrypted_files.each {|encrypted_file| puts "\t" + encrypted_file } puts 'Unencrypted:'.color(:red) unless unencrypted_files.empty? unencrypted_files.each {|unencrypted_file| puts "\t" + unencrypted_file } end |