Top Level Namespace
Defined Under Namespace
Modules: MySecret
Constant Summary collapse
- CONFIG_DIRECTORY =
"#{Dir.home}/.config"- MYSECRET_DIRECTORY =
"#{CONFIG_DIRECTORY}/MySecret"- NOTES_DIRECTORY =
"#{MYSECRET_DIRECTORY}/notes"- EDITOR_PATH =
"#{MYSECRET_DIRECTORY}/editor.config"- IV_ALGORITHM =
"MySecret"
Instance Method Summary collapse
- #change_secret(file_path, old_secret_key, new_secret_key) ⇒ Object
- #decode(data) ⇒ Object
- #decrypt(data, secret_key) ⇒ Object
- #decrypt_note(file_path, secret_key) ⇒ Object
- #delete_note(file_path, secret_key) ⇒ Object
- #display_menu ⇒ Object
- #display_notes ⇒ Object
- #display_text_editor ⇒ Object
- #edit_note(file_path, secret_key) ⇒ Object
- #encode(data) ⇒ Object
- #encrypt(data, secret_key) ⇒ Object
- #encrypt_note(file_path, secret_key) ⇒ Object
- #execute(command, pause = false) ⇒ Object
- #get_file ⇒ Object
- #get_secret_key(message = "secret key: ") ⇒ Object
- #get_text_editor ⇒ Object
- #handler(option) ⇒ Object
- #hasher(secret, limit_chars) ⇒ Object
- #options_menu ⇒ Object
- #read_file(file_path) ⇒ Object
- #read_note(file_path, secret_key) ⇒ Object
- #validate_options(option) ⇒ Object
- #write_file(file_path, data) ⇒ Object
- #write_note(file_path, secret_key) ⇒ Object
Instance Method Details
#change_secret(file_path, old_secret_key, new_secret_key) ⇒ Object
70 71 72 73 |
# File 'lib/MySecret/note.rb', line 70 def change_secret(file_path, old_secret_key, new_secret_key) decrypt_note(file_path, old_secret_key) encrypt_note(file_path, new_secret_key) end |
#decode(data) ⇒ Object
8 9 10 11 |
# File 'lib/MySecret/base64.rb', line 8 def decode(data) decoded_data = Base64.decode64(data) return decoded_data end |
#decrypt(data, secret_key) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/MySecret/cryptography.rb', line 15 def decrypt(data, secret_key) aes = OpenSSL::Cipher.new("aes-256-cbc") aes.decrypt aes.iv = hasher(IV_ALGORITHM, 16) aes.key = secret_key return aes.update(data) + aes.final end |
#decrypt_note(file_path, secret_key) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/MySecret/note.rb', line 62 def decrypt_note(file_path, secret_key) data = read_file(file_path) decoded_data = decode(data) decrypted_data = decrypt(decoded_data, secret_key) write_file(file_path, decrypted_data) end |
#delete_note(file_path, secret_key) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/MySecret/note.rb', line 47 def delete_note(file_path, secret_key) decrypt_note(file_path, secret_key) encrypt_note(file_path, secret_key) execute("rm #{file_path}") end |
#display_menu ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/MySecret/display.rb', line 11 def () puts "\n========== MySecret ==========" puts "[1] Create new note" puts "[2] Read a note" puts "[3] Edit a note" puts "[4] Delete a note" puts "[5] Encrypt an existing note" puts "[6] Decrypt an existing note" puts "[7] Change secret key" puts "[8] Change text editor" puts "[0] Exit" print "> " end |
#display_notes ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/MySecret/display.rb', line 3 def display_notes() notes = Dir.glob("#{NOTES_DIRECTORY}/*") print "notes: " notes.each do |note| print "#{note.gsub! "#{NOTES_DIRECTORY}/", ""}; " end end |
#display_text_editor ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/MySecret/display.rb', line 25 def display_text_editor() puts "=================================" puts "Select your favorite text editor:" puts "[0] Nano" puts "[1] VI" puts "[2] VIM" puts "[3] NeoVim" puts "[4] Emacs" puts "[5] Notepad" print "> " end |
#edit_note(file_path, secret_key) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/MySecret/note.rb', line 30 def edit_note(file_path, secret_key) data = read_file(file_path) decoded_data = decode(data) decrypted_data = decrypt(decoded_data, secret_key) temp_file = "#{file_path}.temp" write_file(temp_file, decrypted_data) editor = get_text_editor() execute("#{editor} #{temp_file}") encrypt_note(temp_file, secret_key) execute("rm #{file_path}") execute("mv #{temp_file} #{file_path}") end |
#encode(data) ⇒ Object
3 4 5 6 |
# File 'lib/MySecret/base64.rb', line 3 def encode(data) encoded_data = Base64.encode64(data) return encoded_data end |
#encrypt(data, secret_key) ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'lib/MySecret/cryptography.rb', line 6 def encrypt(data, secret_key) aes = OpenSSL::Cipher.new("aes-256-cbc") aes.encrypt aes.iv = hasher(IV_ALGORITHM, 16) aes.key = secret_key return aes.update(data) + aes.final end |
#encrypt_note(file_path, secret_key) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/MySecret/note.rb', line 54 def encrypt_note(file_path, secret_key) data = read_file(file_path) encrypted_data = encrypt(data, secret_key) encoded_data = encode(encrypted_data) write_file(file_path, encoded_data) end |
#execute(command, pause = false) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/MySecret/system.rb', line 21 def execute(command, pause=false) system(command) if pause print "\nPress enter to proceed" gets end end |
#get_file ⇒ Object
4 5 6 7 8 9 |
# File 'lib/MySecret/file.rb', line 4 def get_file() print "file name: " file_name = gets.chomp file_path = "#{NOTES_DIRECTORY}/#{file_name}" return file_path end |
#get_secret_key(message = "secret key: ") ⇒ Object
11 12 13 14 15 |
# File 'lib/MySecret/file.rb', line 11 def get_secret_key( = "secret key: ") print secret_key = hasher(gets.chomp, 32) return secret_key end |
#get_text_editor ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/MySecret/system.rb', line 5 def get_text_editor() text_editors = ["nano", "vi", "vim", "nvim", "emacs", "notepad"] if File.file?(EDITOR_PATH) editor = read_file(EDITOR_PATH).chomp if text_editors.include?(editor) return editor end end display_text_editor() text_editor = gets.chomp.to_i write_file(EDITOR_PATH, text_editors[text_editor]) return text_editors[text_editor] end |
#handler(option) ⇒ Object
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 53 54 55 56 |
# File 'lib/MySecret/main.rb', line 7 def handler(option) if option == 1 # Get file name & secret key file_path = get_file() secret_key = get_secret_key() write_note(file_path, secret_key) elsif option == 2 # Get file name & secret key file_path = get_file() secret_key = get_secret_key() read_note(file_path, secret_key) elsif option == 3 # Get file name & secret key file_path = get_file() secret_key = get_secret_key() edit_note(file_path, secret_key) elsif option == 4 # Get file name file_path = get_file() secret_key = get_secret_key("deleting confirmation secret_key: ") delete_note(file_path, secret_key) elsif option == 5 # Get file name & secret key file_path = get_file() secret_key = get_secret_key() encrypt_note(file_path, secret_key) elsif option == 6 # Get file name & secret key file_path = get_file() secret_key = get_secret_key() decrypt_note(file_path, secret_key) elsif option == 7 # Get file name & secret key file_path = get_file() secret_key = get_secret_key() new_secret_key = get_secret_key("new secret key: ") change_secret(file_path, secret_key, new_secret_key) elsif option == 8 execute("rm -f #{EDITOR_PATH}") get_text_editor() end end |
#hasher(secret, limit_chars) ⇒ Object
24 25 26 27 28 |
# File 'lib/MySecret/cryptography.rb', line 24 def hasher(secret, limit_chars) secret = Digest::SHA256.hexdigest secret secret = secret[0, limit_chars] return secret end |
#options_menu ⇒ Object
42 43 44 45 |
# File 'lib/MySecret/system.rb', line 42 def () () option = gets.chomp.to_i end |
#read_file(file_path) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/MySecret/file.rb', line 17 def read_file(file_path) file = File.open(file_path) if file data = file.read return data.chomp else puts "error: not able to access the file" return false end end |
#read_note(file_path, secret_key) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/MySecret/note.rb', line 17 def read_note(file_path, secret_key) data = read_file(file_path) decoded_data = decode(data) decrypted_data = decrypt(decoded_data, secret_key) temp_file = "#{file_path}.temp" write_file(temp_file, decrypted_data) editor = get_text_editor() execute("#{editor} #{temp_file}") execute("rm #{temp_file}") end |
#validate_options(option) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/MySecret/system.rb', line 30 def (option) = [0, 1, 2, 3, 4, 5, 6, 7, 8] is_valid = .include? option if !is_valid puts "error: invalid option" return false elsif option == 0 return false end return true end |
#write_file(file_path, data) ⇒ Object
28 29 30 31 32 |
# File 'lib/MySecret/file.rb', line 28 def write_file(file_path, data) File.open(file_path, "w") do |line| line.puts data end end |
#write_note(file_path, secret_key) ⇒ Object
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/MySecret/note.rb', line 6 def write_note(file_path, secret_key) editor = get_text_editor() execute("#{editor} #{file_path}") data = read_file(file_path) encrypted_data = encrypt(data, secret_key) encoded_data = encode(encrypted_data) write_file(file_path, encoded_data) end |