accutronic 0.1.8 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,62 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "accu-encrypt"
4
- EncryptDecrypt.interactive()
4
+ require "optparse"
5
+
6
+ class HandleArguments < Hash
7
+ def initialize(args)
8
+ super()
9
+ args.each do |object|
10
+ if object.strip.downcase == "-d" then
11
+ self[:mode] = :double
12
+ args.delete(object)
13
+ end
14
+ end
15
+ command = args.first
16
+ if command then
17
+ command = command.strip.downcase.intern
18
+ if command == :encrypt then
19
+ file = args[1]
20
+ handle_file_command(command,file)
21
+ self[:file] = file
22
+ elsif command == :decrypt then
23
+ file = args[1]
24
+ handle_file_command(command,file)
25
+ self[:file] = file
26
+ elsif command != :interactive then
27
+ raise "Command #{command} does not exist!"
28
+ end
29
+ else
30
+ command = :interactive
31
+ end
32
+ self[:command] = command
33
+ end
34
+
35
+ def handle_file_command(command,file)
36
+ if file then
37
+ if not File.file? file then
38
+ raise "File must be a valid file!"
39
+ end
40
+ else
41
+ raise "File must be passed when using command #{command}!"
42
+ end
43
+ end
44
+ end
45
+ arguments = HandleArguments.new(ARGV)
46
+ command = arguments[:command]
47
+ file = arguments[:file]
48
+ mode = arguments[:mode]
49
+ if command == :encrypt then
50
+ password = EncryptDecrypt.get_password
51
+ puts "Encrypting file #{file}"
52
+ EncryptDecrypt.encrypt_file(file,password,mode)
53
+ puts "Encrypted #{file} successfully!"
54
+ elsif command == :decrypt then
55
+ password = EncryptDecrypt.get_password
56
+ puts "Decrypting file #{file}"
57
+ EncryptDecrypt.decrypt_file(file,password,mode)
58
+ puts "Decrypted #{file} successfully!"
59
+ elsif command == :interactive then
60
+ EncryptDecrypt.set_mode(mode)
61
+ EncryptDecrypt.interactive()
62
+ end
@@ -57,7 +57,7 @@ module EncryptDecrypt
57
57
  if response == "y" then
58
58
  puts "Connection accepted."
59
59
  send_code(1)
60
- @password = get_password "Please enter password for communications: "
60
+ @password = EncryptDecrypt.get_password "Please enter password for communications: "
61
61
  @listen_thread, @interactive_thread = nil, nil
62
62
  @mode = EncryptDecrypt.get_mode
63
63
  if listen_code() == 2 then
@@ -78,11 +78,6 @@ module EncryptDecrypt
78
78
  server.close
79
79
  end
80
80
 
81
- # Reads a password using the highline gem.
82
- def get_password(prompt="Enter Password")
83
- ask(prompt) {|q| q.echo = false}
84
- end
85
-
86
81
  # Listens for encrypted messages.
87
82
  def listen_encrypted()
88
83
  @listen_thread = Thread.new {
@@ -208,7 +203,7 @@ module EncryptDecrypt
208
203
  @listen_thread, @interactive_thread = nil, nil
209
204
  @mode = EncryptDecrypt.get_mode
210
205
  if listen_code() == 1 then
211
- @password = get_password "Please enter password for communications: "
206
+ @password = EncryptDecrypt.get_password "Please enter password for communications: "
212
207
  send_code(2)
213
208
  if listen_code() == 2 then
214
209
  puts "Ready code received.\nSuccessfully connected!"
@@ -225,11 +220,6 @@ module EncryptDecrypt
225
220
  end
226
221
  end
227
222
 
228
- # Reads a password using the highline gem.
229
- def get_password(prompt="Enter Password")
230
- ask(prompt) {|q| q.echo = false}
231
- end
232
-
233
223
  # Listens for encrypted messages.
234
224
  def listen_encrypted()
235
225
  @listen_thread = Thread.new {
@@ -344,7 +334,7 @@ module EncryptDecrypt
344
334
  end
345
335
 
346
336
  # Returns interactive help in a string.
347
- def self.InteractiveHelp()
337
+ def self.interactive_help()
348
338
  "---------\nThis program encrypts and decrypts files by offsetting the binary representation of the current character by the binary representation of the first character in an ongoing password. The ongoing password is made up of the password appended to the beginning of the original plaintext. Thus, the pattern is theoretically unpredictable as it is impossible to exactly where the original password starts and the obviously unpredictable plaintext encryption begins. However, this algorithm's security is heavily correlated to the length and complexity of the opening password. A complex but memorable password is recommended for secure encryption.\n---------\n"
349
339
  end
350
340
 
@@ -450,7 +440,7 @@ module EncryptDecrypt
450
440
  file = ""
451
441
  ready = false
452
442
  while not ready do
453
- file = FileLibrary.SelectFile
443
+ file = FileLibrary.select_file
454
444
  if verbose then
455
445
  puts "---------\nWARNING!!!!!\n---------\nEncoding a file could un-retrievably destroy the contents of this file. Are you sure you would like to use this file? |Y N Q|"
456
446
  selection2 = gets.downcase
@@ -471,7 +461,7 @@ module EncryptDecrypt
471
461
  end
472
462
  end
473
463
  if File.exists? file then
474
- password = self.get_password("Password: ")
464
+ password = EncryptDecrypt.get_password
475
465
  if verbose then
476
466
  puts "Finally, are you sure you would like to encrypt this file using this password? |Y N|"
477
467
  selection2 = gets.downcase
@@ -489,7 +479,7 @@ module EncryptDecrypt
489
479
  file = ""
490
480
  ready = false
491
481
  while not ready do
492
- file = FileLibrary.SelectFile
482
+ file = FileLibrary.select_file
493
483
  if verbose then
494
484
  puts "---------\nWARNING!!!!!\n---------\nDecoding a file which has not been encrypted will destroy the contents.\nDecoding a file with the incorrect password will likely also destroy the file.\n---------\nAre you sure you would like to use this file? |Y N Q|"
495
485
  selection2 = gets.downcase
@@ -510,7 +500,7 @@ module EncryptDecrypt
510
500
  end
511
501
  end
512
502
  if File.exists? file then
513
- password = self.get_password("Password: ")
503
+ password = EncryptDecrypt.get_password
514
504
  if verbose then
515
505
  puts "Finally, are you sure you would like to decrypt this file using this password? |Y N|"
516
506
  selection2 = gets.downcase
@@ -525,7 +515,7 @@ module EncryptDecrypt
525
515
  end
526
516
  end
527
517
  elsif selection == "i" then
528
- puts EncryptDecrypt.InteractiveHelp
518
+ puts EncryptDecrypt.interactive_help
529
519
  elsif selection == "m" then
530
520
  puts "Select mode: Double - D Normal - N Verbose - V Unverbose - U"
531
521
  selection2 = ""
@@ -598,7 +588,7 @@ module EncryptDecrypt
598
588
  manager.new_page(page1_win1)
599
589
  # Page 2 - Help
600
590
  page2_win1 = WindowTerminal::Window.new(WindowTerminal::Orientation.new,"Help")
601
- page2_text1 = WindowTerminal::ColoredWrappedText.new(WindowTerminal::Orientation.new,self.InteractiveHelp.gsub(/-/,""),6)
591
+ page2_text1 = WindowTerminal::ColoredWrappedText.new(WindowTerminal::Orientation.new,self.interactive_help.gsub(/-/,""),6)
602
592
  page2_win1.add_objects(page2_text1)
603
593
  manager.new_page(page2_win1)
604
594
  return page1_win1.objects[2]
@@ -623,7 +613,7 @@ module EncryptDecrypt
623
613
  file = ""
624
614
  ready = false
625
615
  while not ready do
626
- file = FileLibrary.SelectFile_With_Windows(manager)
616
+ file = FileLibrary.select_file_with_windows(manager)
627
617
  manager.show
628
618
  if verbose then
629
619
  text.set_text "---------\nWARNING!!!!!\n---------\nEncoding a file could un-retrievably destroy the contents of this file. Are you sure you would like to use this file? |Y N Q|"
@@ -679,7 +669,7 @@ module EncryptDecrypt
679
669
  file = ""
680
670
  ready = false
681
671
  while not ready do
682
- file = FileLibrary.SelectFile_With_Windows(manager)
672
+ file = FileLibrary.select_file_with_windows(manager)
683
673
  manager.show
684
674
  if verbose then
685
675
  text.set_text "---------\nWARNING!!!!!\n---------\nDecoding a file which has not been encrypted will destroy the contents.\nDecoding a file with the incorrect password will likely also destroy the file.\n---------\nAre you sure you would like to use this file? |Y N Q|"
@@ -821,4 +811,17 @@ module EncryptDecrypt
821
811
  def self.get_mode()
822
812
  @@mode.dup
823
813
  end
814
+
815
+ # Set current encryption
816
+ # mode.
817
+ def self.set_mode(arg)
818
+ if arg then
819
+ @@mode = arg
820
+ end
821
+ end
822
+
823
+ # Reads a password using the highline gem.
824
+ def self.get_password(prompt="Password: ")
825
+ ask(prompt) {|q| q.echo = false}
826
+ end
824
827
  end
@@ -36,7 +36,7 @@ module FileLibrary
36
36
  # and input text and returning
37
37
  # a matching file or the original
38
38
  # text if no match is found.
39
- def self.Completion(dir,text)
39
+ def self.completion(dir,text)
40
40
  #--
41
41
  result = text
42
42
  range = (0)..(text.length-1)
@@ -53,7 +53,7 @@ module FileLibrary
53
53
 
54
54
  # Selects a file using a simple command-line
55
55
  # navigation system.
56
- def self.SelectFile()
56
+ def self.select_file()
57
57
  #--
58
58
  #origDir = Dir.pwd
59
59
  currentDirectory = ""
@@ -73,12 +73,8 @@ module FileLibrary
73
73
  request = gets
74
74
  request.slice!(-1)
75
75
  while (not (File.exists? request)) do
76
- puts "Invalid: |" + request + "|"
77
- puts "Please enter a valid file name or directory."
78
- request = gets
79
- request.slice!(-1)
80
76
  if not File.exists? request then
81
- request = self.Completion(currentDirectory,request)
77
+ request = self.completion(currentDirectory,request)
82
78
  if File.exists? request then
83
79
  if not File.directory? request then
84
80
  puts "Is this file correct: |" + request + "| |Y N| "
@@ -90,6 +86,12 @@ module FileLibrary
90
86
  end
91
87
  end
92
88
  end
89
+ if not File.exists request then
90
+ puts "Invalid: |" + request + "|"
91
+ puts "Please enter a valid file name or directory."
92
+ request = gets
93
+ request.slice!(-1)
94
+ end
93
95
  end
94
96
  if File.directory? request then
95
97
  Dir.chdir(request)
@@ -105,7 +107,7 @@ module FileLibrary
105
107
 
106
108
  # An implementation of SelectFile
107
109
  # using WindowTerminal.
108
- def self.SelectFile_With_Windows(manager=WindowTerminal::WindowManager.new)
110
+ def self.select_file_with_windows(manager=WindowTerminal::WindowManager.new)
109
111
  #--
110
112
  # Declarations
111
113
  #origDir = Dir.pwd
@@ -166,7 +168,7 @@ module FileLibrary
166
168
  #WindowTerminal.screen_render
167
169
  }
168
170
  if not File.exists? request then
169
- request = self.Completion(currentDirectory,request)
171
+ request = self.completion(currentDirectory,request)
170
172
  if File.exists? request then
171
173
  if not File.directory? request then
172
174
  text2.set_text "Is this file correct: |" + request + "| |Y N| "
@@ -64,6 +64,17 @@ module WindowTerminal
64
64
  def to_s
65
65
  "#{@x}|#{@y}"
66
66
  end
67
+
68
+ # Allows for array-like indexing.
69
+ # 0 - @x, 1 - @y
70
+ def [](index)
71
+ assert RangeError "Index must be either 0 or 1!" if not (index == 0 or index == 1)
72
+ if index == 0 then
73
+ return @x.dup
74
+ else
75
+ return @y.dup
76
+ end
77
+ end
67
78
  end
68
79
 
69
80
  # Aclass for use with the Window object
@@ -186,6 +197,12 @@ module WindowTerminal
186
197
  return line
187
198
  #++
188
199
  end
200
+
201
+ # Makes to_s return something
202
+ # more meaningful.
203
+ def to_s
204
+ "<ColoredText @orientation=#{@orientation.to_s}, @y=#{@y} >"
205
+ end
189
206
  end
190
207
 
191
208
  # A subclass of Text for colored window text.
@@ -215,12 +232,19 @@ module WindowTerminal
215
232
  end
216
233
  text
217
234
  end
235
+ private :colorize
218
236
 
219
237
  # Sets text while taking into
220
238
  # account colorization.
221
239
  def set_text(text)
222
240
  super(colorize(text))
223
241
  end
242
+
243
+ # Makes to_s return something
244
+ # more meaningful.
245
+ def to_s
246
+ "<ColoredText @orientation=#{@orientation.to_s}, @y=#{@y}, @color=#{@color.to_s} >"
247
+ end
224
248
  end
225
249
 
226
250
  # A subclass of Text which allows for
@@ -404,6 +428,12 @@ module WindowTerminal
404
428
  WindowTerminal.remove_window self
405
429
  end
406
430
  end
431
+
432
+ # Allows for array-like indexing
433
+ # of the Window's objects.
434
+ def [](index)
435
+ @objects[index]
436
+ end
407
437
  end
408
438
 
409
439
  # Makes handling and switching between
@@ -530,6 +560,13 @@ module WindowTerminal
530
560
  win.add_object text
531
561
  return [win]
532
562
  end
563
+
564
+ # Calls generate() with
565
+ # (argument 1) + 1.
566
+ def [](index)
567
+ raise TypeError "Num must be an integer!" if (not num.is_a? Fixnum)
568
+ generate(index+1)
569
+ end
533
570
  end
534
571
 
535
572
  # Gets current terminal size based
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: accutronic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-05-22 00:00:00.000000000 Z
12
+ date: 2014-05-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: abc