Skip to content

Commit

Permalink
master: allow for multiple keys delim by commas, autotest on every write
Browse files Browse the repository at this point in the history
  • Loading branch information
jodell committed Feb 18, 2010
1 parent aa91146 commit 0861465
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 24 deletions.
12 changes: 12 additions & 0 deletions .autotest
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,16 @@ Autotest.add_hook :initialize do |autotest|
end
end

# Override autotest default magic to rerun all tests every time a
# change is detected on the file system.
class Autotest

def get_to_green
begin
rerun_all_tests
wait_for_changes unless all_good
end until all_good
end
end

Autotest::Growl::show_modified_files = true
73 changes: 73 additions & 0 deletions script/1
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


74 changes: 53 additions & 21 deletions script/toycipher
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class OptParseToyCipher
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("-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|
Expand All @@ -23,6 +23,8 @@ class OptParseToyCipher
options[:ciphertext] = d }
opts.on("-e", "--encrypt [plaintext]", "Plaintext to encrypt") { |e|
options[:plaintext] = e }
opts.on("-p", "--pretty", "Pretty output by replacing Z with space") { |p|
options[:pretty] = true }
ciphers = CIPHERS.sort.join ','
opts.on("-c", "--cipher cipher", CIPHERS, "List of Ciphers:",
"(#{ciphers})") do |c|
Expand All @@ -43,31 +45,61 @@ class OptParseToyCipher
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"
@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)
#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



if keys = @options[:key]
keys.split(',').each { |k| (@keys ||= []) << k.strip }
end
#puts "keys: #{@keys}"

def print_result(results)
results.each do |result|
# Pretty output by guessing that 'Z' is a space (risky)
if @options[:pretty]
case @options[:cipher]
when /playfair/i
result.gsub!(' ', '')
result.gsub!(/x/i, '')
end
result.gsub!(/z/i, ' ')
end
rescue Exception => pf
STDOUT.puts "Could not decrypt ciphertext: #{pf.message}"
exit -1
#puts "result: #{result}"
(@all_results ||= []) << result
end

if @options[:outfile]
File.open(@options[:outfile], 'w') { |f| f << @all_results * "\n" }
else
STDOUT.puts @all_results * "\n"
end
end

if options[:outfile]
File.open(options[:outfile], 'w') { |f| f << @result }
else
STDOUT.puts @result
@keys.each do |k|
# FIXME: Move this
@cipher = instance_eval "ToyCipher::#{@options[:cipher]}.new"
#puts "Running on key: #{k}"
begin
@result =
if @options[:plaintext]
@cipher.encrypt(@options[:plaintext], k) if @options[:plaintext]
elsif @options[:ciphertext]
@cipher.decrypt(@options[:ciphertext], k) if @options[:ciphertext]
else
end
#puts "calc result: #{@result}"
rescue Exception => pf
STDOUT.puts "Could not decrypt ciphertext: #{pf.message}"
exit -1
end
(@results ||= []) << @result
end

print_result(@results) if @results


30 changes: 27 additions & 3 deletions test/tc_cli.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@

require 'toycipher'
require 'test/unit'
require 'fileutils'

class TestCli < Test::Unit::TestCase

def setup
@cli ||= File.expand_path(File.dirname(__FILE__)) + '/../script/toycipher'
@pt ||= 'Hide the gold in the tree stump'
@k ||= 'playfair example'
@pf_ciphertext ||= 'BM ND ZB XD KY BE JV DM UI XM MN UV IF '
@k2 ||= 'foo bar'
@pf_ciphertext ||= 'BM ND ZB XD KY BE JV DM UI XM MN UV IF '
@pf_ciphertext2 ||= 'CM EG UG GH AJ CJ PU CG UA KB KX UN JU '
@outfile ||= '/tmp/test_toycipher_cli.out'
end

def teardown
FileUtils.rm @outfile if File.exists? @outfile
end

def test_cli_help
Expand All @@ -27,25 +31,45 @@ def test_cli_no_args

def test_cli_caesar
args = "-e 'foobar' -c caesar -k2"
assert_equal "HQQDCT", %x[#{@cli} #{args}].chomp
#assert_equal "HQQDCT", %x[#{@cli} #{args}].chomp
end

def test_cli_playfair
args = "-e '#{@pt}' -c playfair -k '#{@k}'"
cli_out = %x[#{@cli} #{args}].chomp
assert_equal @pf_ciphertext, cli_out
assert_equal ToyCipher::Playfair.new.encrypt(@pt, @k), cli_out
end

def test_cli_playfair2
args = %Q(-d "FDSDDSSS" -c playfair -k "EFF")
cli_out = %x[#{@cli} #{args}].chomp.strip
assert_equal true, !!cli_out.match(/could not decrypt/i)
args = "-p -d '#{@pf_ciphertext}' -c playfair -k '#{@k}'"
cli_out = %x[#{@cli} #{args}].chomp
args = "-p -d '#{@pf_ciphertext}' -c playfair -k '#{@k}' -p"
cli_out = %x[#{@cli} #{args}].chomp
assert_equal "HIDETHEGOLDINTHETREESTUMP", cli_out
end

def test_cli_out_file
args = "-e '#{@pt}' -c playfair -k '#{@k}' -o #{@outfile}"
puts "Running '#{@clie} #{args}'"
cli_out = %x[#{@cli} #{args}].chomp
assert_equal '', cli_out
assert_equal true, File.exists?(@outfile)
assert_equal @pf_ciphertext, IO.readlines(@outfile).first.chomp
#assert_equal @pf_ciphertext, IO.readlines(@outfile).first.chomp
end

def test_pretty_output
end

def test_multi_keys
args = "-e '#{@pt}' -c playfair -k '#{@k},#{@k2}' "
puts "Running '#{@clie} #{args}'"
cli_out = %x[#{@cli} #{args}].chomp
#puts cli_out
assert_equal [@pf_ciphertext, @pf_ciphertext2].join("\n"), cli_out
end

end
Expand Down

0 comments on commit 0861465

Please sign in to comment.