Skip to content

Commit

Permalink
use rake test task, initial stab at a gem, dedication
Browse files Browse the repository at this point in the history
  • Loading branch information
jodell committed Jun 3, 2010
1 parent b63c7bf commit 55bcab5
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 35 deletions.
27 changes: 26 additions & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,35 @@ A ruby implementation of classic ciphers, including:
* One-time Pad
* Vigenere
* Caesar
* Morse Code (TODO)
* String to phone number extension: 'abc'.phone # => '234'

== CLI

Supports a command line script (SEE EXAMPLE)
Supports a command line interface:

> toycipher -e caesar -k G foo
MVV

(SEE EXAMPLE)

== Dedication

This library is dedicated primarily to

* LosT (http://ten-five-seven.org)
* G. Mark Hardy (http://www.gmarkhardy.com)

for their diabolical conundrums that inspired this library.

Thanks also to Defcon & Shmoocon for hosting these events:

* http://www.defcon.org
* http://www.shmoocon.org

and of course to members of Mobile Disco for late night puzzle solving (hax!)

* http://haxbymobiledisco.com

== Author

Expand Down
2 changes: 1 addition & 1 deletion ROADMAP
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
-==-
0.5
-==-
* String extension for phone numbers
* Ruby gem support
* Basic Morse Code!
* String extension for phone numbers

1.0
* Large text testing
Expand Down
36 changes: 31 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,41 @@

require 'rake'
require 'rake/testtask'
require 'rake/gempackagetask'

$: << File.expand_path(File.dirname(__FILE__) + '/test')

task :default => :test

desc 'Run tests'
task :test do
sh "ruby test/ts_toycipher.rb"
end
desc 'Run all tests'
task :test => 'test:unit'

namespace 'test' do
namespace :test do
Rake::TestTask.new :unit do |t|
t.libs << 'test'
t.pattern = "test/tc_*.rb"
t.verbose = true
end

end

spec = Gem::Specification.new do |s|
s.name = "toycipher"
s.version = "0.5" # FIXME
s.author = "Jeffrey ODell"
s.email = "[email protected]"
s.homepage = "http://github.com/jodell/toycipher"
s.platform = Gem::Platform::RUBY
s.summary = "Ruby implementation of popular ciphers for recreational and educational cryptanalysis"
s.files = FileList["{bin,lib}/**/*"].to_a
s.require_path = "lib"
s.autorequire = "name"
s.test_files = FileList["{test}/**/tc_*.rb"].to_a
s.has_rdoc = false
#s.extra_rdoc_files = ["README"]
end

Rake::GemPackageTask.new(spec) do |pkg|
pkg.need_tar = true
end

26 changes: 17 additions & 9 deletions ext/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,45 @@ class String
# Only tested in 1.8.7
# 1.8 syntax
#
# Touchtone phone:
# Support two Touchtone phone keypads:
#
# legacy:
# 1 2 3
# abc def ghi
# 4 5 6
# jkl mno pqr
# 7 8 9
# stu vwx yz
#
#
# default:
# 1 2 3
# abc def
# 4 5 6
# ghi jlk mno
# 7 8 9
# pqrs tuv wxyz
#
# really?
#
#
#
def phone
def phone(type = :default)
raise "Ruby version #{RUBY_VERSION} not supported!" unless RUBY_VERSION =~ /^1.8/
self.downcase.split(//).inject('') do |acc, b| # better syntax in 1.9
if b =~ /[a-z]/
index = b[0] % LETTER_A_VALUE
acc += ((index / 3) + 1).to_s
unless type == :legacy
val = ((b[0] % LETTER_A_VALUE / 3) + 2)
val -= 1 if %w(s v y z).include?(b)
acc += val.to_s
else # not real
acc += ((b[0] % LETTER_A_VALUE / 3) + 1).to_s
end
else
acc += b
end
end
end
end

puts "abcdefghijklmnopqrstuvwxyz".phone
puts "ABCDEFGHIJKLMNOPQRSTUVWXYZ".phone
puts "THIS IS A TEST".phone
#puts "abcdefghijklmnopqrstuvwxyz".phone
#puts "ABCDEFGHIJKLMNOPQRSTUVWXYZ".phone(:legacy)
#puts "THIS IS A TEST".phone
Binary file added pkg/toycipher-0.5.gem
Binary file not shown.
2 changes: 1 addition & 1 deletion test/shmoocon2010.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'toycipher'
require 'test/unit'

class TestCaesarCipher < Test::Unit::TestCase
class TestShmooCipher < Test::Unit::TestCase
SHMOO_CIPHER = {
'1' => 'VFLASGGGGIUGAAGYBDAWHOEVHUUVLLHGJYOLGFGPGHALGGGOAAGGJPLLHZKAGZSLRXHSRYHKFPVKISTF',
'2' => 'XBMGRMBULEMPBMSRGMEBYRGMGRGHFMAGNMRLRZOMGXMJRMLNBMEMUAZEGNQEQBGPSZRYZLDPYQDUGEPL',
Expand Down
9 changes: 7 additions & 2 deletions test/tc_caesar.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$: << File.expand_path(File.dirname(__FILE__)) + '/../lib/'

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

class TestCaesarCipher < Test::Unit::TestCase
Expand All @@ -15,6 +15,11 @@ def setup
def teardown
end

def test_0
assert_not_nil ToyCipher
assert_not_nil ToyCipher::Caesar
end

def test_encrypt
assert_equal @cipher.encrypt(@plaintext, @offset), @ciphertext
@cipher.plaintext = @plaintext
Expand Down
3 changes: 1 addition & 2 deletions test/tc_cli.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

$: << File.expand_path(File.dirname(__FILE__)) + '/../lib/'
require 'test_helper'
require 'toycipher'
require 'test/unit'
require 'fileutils'

def debug?
Expand Down
33 changes: 33 additions & 0 deletions test/tc_ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

require 'toycipher'
require 'string'

class TestToyCipherExt < Test::Unit::TestCase

def setup
end

def test_phone
assert_equal '2', 'a'.phone
assert_equal '2', 'a'.phone(:foo)
assert_equal '1', 'a'.phone(:legacy)
assert_equal '3', 'd'.phone
end

def test_basic
assert_equal '22233344455566677778889999', "abcdefghijklmnopqrstuvwxyz".phone
assert_equal '11122233344455566677788899', "ABCDEFGHIJKLMNOPQRSTUVWXYZ".phone(:legacy)
assert_equal '8447 47 2 8378', "THIS IS A TEST".phone
end

def test_non_alpha
assert_equal '123', '123'.phone
assert_equal '*&2-==[][][', '*&2-==[][]['.phone
end

def test_mixed_alpha_non_alpha
assert_equal '843 78425 27696 369 586733 6837 843 5299 364!!!', 'The quick brown fox jumped over the lazy dog!!!'.phone
end

end

1 change: 0 additions & 1 deletion test/tc_playfair.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

require 'toycipher'
require 'test/unit'

class TestPlayfairCipher < Test::Unit::TestCase

Expand Down
2 changes: 0 additions & 2 deletions test/tc_toycipher.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
$: << File.expand_path(File.dirname(__FILE__) + '/../lib/')
require 'test/unit'
require 'toycipher'

class TestToyCipher < Test::Unit::TestCase
Expand Down
1 change: 0 additions & 1 deletion test/tc_vigenere.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

require 'toycipher'
require 'test/unit'

class TestVigenereCipher < Test::Unit::TestCase

Expand Down
5 changes: 5 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$: << File.join(File.dirname(__FILE__), "..", "lib")
$: << File.expand_path(File.dirname(__FILE__)) + '/../ext/'

require 'test/unit'

10 changes: 0 additions & 10 deletions test/ts_toycipher.rb

This file was deleted.

0 comments on commit 55bcab5

Please sign in to comment.