Class: Bcome::Encryptor
- Inherits:
-
Object
- Object
- Bcome::Encryptor
- Includes:
- Singleton
- Defined in:
- lib/objects/encryptor.rb
Constant Summary collapse
- UNENC_SIGNIFIER =
"".freeze
- ENC_SIGNIFIER =
"enc".freeze
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
- #has_encrypted_files? ⇒ Boolean
- #has_files_to_encrypt? ⇒ Boolean
- #metadata_path ⇒ Object
- #pack ⇒ Object
- #path_to_metadata ⇒ Object
- #prompt_for_key ⇒ 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.
9 10 11 |
# File 'lib/objects/encryptor.rb', line 9 def key @key end |
Instance Method Details
#all_encrypted_filenames ⇒ Object
90 91 92 |
# File 'lib/objects/encryptor.rb', line 90 def all_encrypted_filenames Dir["#{}/*.enc"] end |
#all_unencrypted_filenames ⇒ Object
86 87 88 |
# File 'lib/objects/encryptor.rb', line 86 def all_unencrypted_filenames Dir["#{}/*"].reject {|f| f =~ /\.enc/} end |
#has_encrypted_files? ⇒ Boolean
31 32 33 |
# File 'lib/objects/encryptor.rb', line 31 def has_encrypted_files? all_encrypted_filenames.any? end |
#has_files_to_encrypt? ⇒ Boolean
35 36 37 |
# File 'lib/objects/encryptor.rb', line 35 def has_files_to_encrypt? all_unencrypted_filenames.any? end |
#metadata_path ⇒ Object
94 95 96 |
# File 'lib/objects/encryptor.rb', line 94 def "bcome/metadata" end |
#pack ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/objects/encryptor.rb', line 11 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 return end |
#path_to_metadata ⇒ Object
77 78 79 |
# File 'lib/objects/encryptor.rb', line 77 def "bcome/metadata" end |
#prompt_for_key ⇒ Object
25 26 27 28 29 |
# File 'lib/objects/encryptor.rb', line 25 def prompt_for_key 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 |
#toggle_packed_files(filenames, packer_method) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/objects/encryptor.rb', line 53 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 =~ /#{}\/(.+)\.enc/ opposing_filename = $1 action = "Unpacking" else filename =~ /#{}\/(.*)/ opposing_filename = "#{$1}.enc" action = "Packing" end # Write encrypted/decryption action enc_decrypt_result = raw_contents.send(packer_method, @key) puts "#{action}\s".informational + filename + "\sto\s".informational + "#{}/" + opposing_filename write_file(opposing_filename, enc_decrypt_result) end puts "\ndone".informational end |
#unpack ⇒ Object
47 48 49 50 51 |
# File 'lib/objects/encryptor.rb', line 47 def unpack prompt_for_key toggle_packed_files(all_encrypted_filenames,:decrypt) return end |
#verify_presented_key ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/objects/encryptor.rb', line 39 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
81 82 83 84 |
# File 'lib/objects/encryptor.rb', line 81 def write_file(filename, contents) filepath = "#{}/#{filename}" File.open("#{filepath}", 'w') { |f| f.write(contents) } end |