Class: Bcome::Encryptor
- Inherits:
-
Object
- Object
- Bcome::Encryptor
- Includes:
- Singleton
- Defined in:
- lib/objects/encryptor.rb
Constant Summary collapse
- UNENC_SIGNIFIER =
''
- ENC_SIGNIFIER =
'enc'
- AFFIRMATIVE =
'yes'
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Instance Method Summary collapse
- #all_encrypted_filenames ⇒ Object
- #all_unencrypted_filenames ⇒ Object
- #decrypt_file_data(filename) ⇒ Object
- #diff ⇒ Object
- #enc_file_diff(filename) ⇒ Object
- #get_diffs(file_one, file_two) ⇒ Object
- #has_encrypted_files? ⇒ Boolean
- #has_files_to_encrypt? ⇒ Boolean
- #metadata_path ⇒ Object
- #opposing_file_for_filename(filename) ⇒ Object
- #pack ⇒ Object
- #path_to_metadata ⇒ Object
- #prompt_for_key ⇒ Object
- #prompt_to_overwrite ⇒ Object
- #toggle_packed_files(filenames, packer_method) ⇒ Object
- #unpack ⇒ Object
- #verify_presented_key ⇒ Object
- #write_file(filename, contents) ⇒ Object
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
13 14 15 |
# File 'lib/objects/encryptor.rb', line 13 def key @key end |
Instance Method Details
#all_encrypted_filenames ⇒ Object
176 177 178 |
# File 'lib/objects/encryptor.rb', line 176 def all_encrypted_filenames Dir["#{}/*.enc"] end |
#all_unencrypted_filenames ⇒ Object
172 173 174 |
# File 'lib/objects/encryptor.rb', line 172 def all_unencrypted_filenames Dir["#{}/*"].reject { |f| f =~ /\.enc/ } end |
#decrypt_file_data(filename) ⇒ Object
67 68 69 70 |
# File 'lib/objects/encryptor.rb', line 67 def decrypt_file_data(filename) raw_contents = File.read(filename) raw_contents.send(:decrypt, @key) end |
#diff ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/objects/encryptor.rb', line 106 def diff prompt_for_key puts "\n" all_encrypted_filenames.each do |filename| opposing_file = opposing_file_for_filename(filename) if File.exist?(opposing_file) if diffs = enc_file_diff(filename) puts "\n[+/-]\s".warning + filename + "\sis different to your local unpacked version\n\n" puts diffs + "\n\n" else puts filename.to_s.informational + "\s- no differences".bc_green end else puts filename.to_s.informational + "\s- new file".warning end end puts "\n" end |
#enc_file_diff(filename) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/objects/encryptor.rb', line 72 def enc_file_diff(filename) # Get decrypted file data decrypted_data_for_filename = decrypt_file_data(filename) # Get unpacked file data opposing_filename = opposing_file_for_filename(filename) return nil unless File.exist?(opposing_filename) unpacked_file_data = File.read(opposing_filename) # there are no differences return nil if decrypted_data_for_filename.eql?(unpacked_file_data) get_diffs(unpacked_file_data, decrypted_data_for_filename) end |
#get_diffs(file_one, file_two) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/objects/encryptor.rb', line 93 def get_diffs(file_one, file_two) diffy = ::Diffy::SplitDiff.new(file_one, file_two) left_diffs = diffy.left.split("\n").each_with_index.collect { |l, index| "#{index + 1}:\s#{l}" } right_diffs = diffy.right.split("\n").each_with_index.collect { |l, index| "#{index + 1}:\s#{l}" } diffed_lines = (left_diffs + right_diffs).select { |line| line =~ /^[0-9]+:\s[+-](.+)$/ } return nil if diffed_lines.empty? diffed_lines.collect do |line| line =~ /^[0-9]+:\s\+(.+)$/ ? line.bc_green : line.bc_red end.join("\n") end |
#has_encrypted_files? ⇒ Boolean
45 46 47 |
# File 'lib/objects/encryptor.rb', line 45 def has_encrypted_files? all_encrypted_filenames.any? end |
#has_files_to_encrypt? ⇒ Boolean
49 50 51 |
# File 'lib/objects/encryptor.rb', line 49 def has_files_to_encrypt? all_unencrypted_filenames.any? end |
#metadata_path ⇒ Object
180 181 182 |
# File 'lib/objects/encryptor.rb', line 180 def 'bcome/metadata' end |
#opposing_file_for_filename(filename) ⇒ Object
88 89 90 91 |
# File 'lib/objects/encryptor.rb', line 88 def opposing_file_for_filename(filename) filename =~ %r{#{}/(.+)\.enc} "#{}/#{Regexp.last_match(1)}" end |
#pack ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/objects/encryptor.rb', line 15 def pack # Bcome currently works with a single encryption key - the same one - for all files # When we attempt an encrypt we'll check first to see if any encrypted files already exists, and # we'll try our key on it. If the fails to unpack the file, we abort the encryption attempt. prompt_for_key if has_files_to_encrypt? verify_presented_key if has_encrypted_files? toggle_packed_files(all_unencrypted_filenames, :encrypt) else puts "\nNo unencrypted files to encrypt.\n".warning end nil end |
#path_to_metadata ⇒ Object
163 164 165 |
# File 'lib/objects/encryptor.rb', line 163 def 'bcome/metadata' end |
#prompt_for_key ⇒ Object
29 30 31 32 33 34 |
# File 'lib/objects/encryptor.rb', line 29 def prompt_for_key puts "\n" print 'Please enter an encryption key (and if your data is already encrypted, you must provide the same key): '.informational @key = STDIN.noecho(&:gets).chomp # puts "\n" end |
#prompt_to_overwrite ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/objects/encryptor.rb', line 36 def prompt_to_overwrite valid_answers = [AFFIRMATIVE, 'no'] puts "\n" print "Do you want to continue with unpacking this file? Your local changes would be overwritten [#{valid_answers.join(',')}]\s" answer = STDIN.gets.chomp prompt_to_overwrite unless valid_answers.include?(answer) answer end |
#toggle_packed_files(filenames, packer_method) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/objects/encryptor.rb', line 125 def toggle_packed_files(filenames, packer_method) raise 'Missing encryption key. Please set an encryption key' unless @key filenames.each do |filename| # Get raw raw_contents = File.read(filename) if packer_method == :decrypt filename =~ %r{#{}/(.+)\.enc} opposing_filename = Regexp.last_match(1) action = 'Unpacking' # Skip unpacking a file if there are local modifications that the user does not want to lose. if diffs = enc_file_diff(filename) puts "\n[+/-]\s".warning + filename + "\sis different to your local unpacked version\n\n" puts diffs if prompt_to_overwrite != AFFIRMATIVE puts "\n\nskipping\s".warning + filename + "\n" next end puts "\n" end else filename =~ %r{#{}/(.*)} opposing_filename = "#{Regexp.last_match(1)}.enc" action = 'Packing' end # Write encrypted/decryption action enc_decrypt_result = raw_contents.send(packer_method, @key) print "\n\n" puts "#{action}\s".informational + filename + "\sto\s".informational + "#{}/" + opposing_filename write_file(opposing_filename, enc_decrypt_result) end puts "\ndone".informational end |
#unpack ⇒ Object
61 62 63 64 65 |
# File 'lib/objects/encryptor.rb', line 61 def unpack prompt_for_key toggle_packed_files(all_encrypted_filenames, :decrypt) nil end |
#verify_presented_key ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/objects/encryptor.rb', line 53 def verify_presented_key # We attempt a decrypt of any encrypted file in order to verify that a newly presented key # matches the key used to previously encrypt. Bcome operates on a one-key-per-implementation basis. test_file = all_encrypted_filenames.first file_contents = File.read(test_file) file_contents.decrypt(@key) end |
#write_file(filename, contents) ⇒ Object
167 168 169 170 |
# File 'lib/objects/encryptor.rb', line 167 def write_file(filename, contents) filepath = "#{}/#{filename}" File.open(filepath.to_s, 'w') { |f| f.write(contents) } end |