-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
master: allow for multiple keys delim by commas, autotest on every write
- Loading branch information
Showing
4 changed files
with
165 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#!/usr/bin/env ruby | ||
|
||
$:.unshift File.join(File.dirname(__FILE__), "../lib") | ||
require 'optparse' | ||
require 'toycipher' | ||
|
||
CIPHERS = ToyCipher::CIPHERS | ||
|
||
class OptParseToyCipher | ||
def self.parse args | ||
begin | ||
#puts "CIPHERS: #{CIPHERS * ', '}" | ||
options = {} | ||
option_parser = OptionParser.new do |opts| | ||
opts.on("-h", "--help", "Print this help screen") { puts opts; exit 0; } | ||
opts.on("-k", "--key key", "Encryption Key") { |k| options[:key] = k } | ||
opts.on("-o", "--output-file [file]", "Output file for results, defaults to STDOUT") { |f| | ||
options[:outfile] = f } | ||
opts.on("-i", "--input-file file", "Input file for encryption or decryption") do |f| | ||
options[:infile] = f | ||
end | ||
opts.on("-d", "--decrypt [ciphertext]", "Ciphertext to decrypt") { |d| | ||
options[:ciphertext] = d } | ||
opts.on("-e", "--encrypt [plaintext]", "Plaintext to encrypt") { |e| | ||
options[:plaintext] = e } | ||
ciphers = CIPHERS.sort.join ',' | ||
opts.on("-c", "--cipher cipher", CIPHERS, "List of Ciphers:", | ||
"(#{ciphers})") do |c| | ||
options[:cipher] = c | ||
options[:cipher][0] = c[0].chr.upcase # :( | ||
end | ||
opts.on("-t", "--transpose", "Transpose m x n lines of text to be n x m") do | ||
options[:transpose] = true | ||
end | ||
opts.on("-v", "--verbose") { options[:verbose], options[:debug] = true, true } | ||
end | ||
option_parser.parse!(args) | ||
options | ||
rescue OptionParser::ParseError => error | ||
puts error.message | ||
exit -1 | ||
end | ||
end | ||
end | ||
|
||
options = OptParseToyCipher.parse ARGV | ||
puts "options: #{options.inspect}" if options[:debug] | ||
raise ArgumentError, "No cipher specified! Expected one of #{CIPHERS * ', '}" unless options[:cipher] && | ||
CIPHERS.include?(options[:cipher].downcase) | ||
c = instance_eval "ToyCipher::#{options[:cipher]}.new" | ||
#puts options[:plaintext] | ||
|
||
begin | ||
@result = | ||
if options[:plaintext] | ||
c.encrypt(options[:plaintext], options[:key]) if !!options[:plaintext] | ||
elsif options[:ciphertext] | ||
c.decrypt(options[:ciphertext], options[:key]) if !!options[:ciphertext] | ||
else | ||
|
||
end | ||
rescue Exception => pf | ||
STDOUT.puts "Could not decrypt ciphertext: #{pf.message}" | ||
exit -1 | ||
end | ||
|
||
if options[:outfile] | ||
File.open(options[:outfile], 'w') { |f| f << @result } | ||
else | ||
STDOUT.puts @result | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters