crypt-rot13 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES +7 -0
- data/MANIFEST +9 -0
- data/README +48 -0
- data/lib/crypt/rot13.rb +44 -0
- data/test/tc_rot13.rb +84 -0
- metadata +44 -0
data/CHANGES
ADDED
data/MANIFEST
ADDED
data/README
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
== Description
|
2
|
+
A package for performing simple character rotation (i.e. Caesar Cipher)
|
3
|
+
encryption. There are many like it, but this one is mine.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
=== Manual Installation
|
8
|
+
ruby test/tc_rot13.rb (optional)
|
9
|
+
ruby install.rb
|
10
|
+
|
11
|
+
=== Gem Installation
|
12
|
+
ruby crypt-rot13.gem
|
13
|
+
gem install crypt-rot13-<version>.gem
|
14
|
+
|
15
|
+
== Synopsis
|
16
|
+
require "crypt/rot13"
|
17
|
+
include Crypt
|
18
|
+
|
19
|
+
str = Rot13.new("Hello World", 3) # Caesar cipher
|
20
|
+
puts str # "Khoor Zruog"
|
21
|
+
|
22
|
+
== Notes
|
23
|
+
* Not unicode friendly. Only works on ASCII values 65-90 and 97-122.
|
24
|
+
|
25
|
+
== Acknowledgements
|
26
|
+
Thanks go to Gaius Julius Caesar (d. 44 BC) for creating one of the first,
|
27
|
+
and most simple, encryption schemes. Hail Caesar!
|
28
|
+
|
29
|
+
== Known Bugs
|
30
|
+
None that I'm aware of.
|
31
|
+
|
32
|
+
Please log any bugs you find on the SourceForge project page at
|
33
|
+
http://www.rubyforge.org/projects/shards.
|
34
|
+
|
35
|
+
== License
|
36
|
+
Ruby's
|
37
|
+
|
38
|
+
== Warranty
|
39
|
+
This package is provided "as is" and without any express or
|
40
|
+
implied warranties, including, without limitation, the implied
|
41
|
+
warranties of merchantability and fitness for a particular purpose.
|
42
|
+
|
43
|
+
== Author
|
44
|
+
Daniel J. Berger
|
45
|
+
djberg96 at gmail dot com
|
46
|
+
imperator on IRC (irc.freenode.net)
|
47
|
+
|
48
|
+
|
data/lib/crypt/rot13.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
module Crypt
|
2
|
+
class Rot13Error < ArgumentError; end
|
3
|
+
class Rot13 < String
|
4
|
+
VERSION = "1.0.1"
|
5
|
+
|
6
|
+
# Returns a new Rot13 object. The object is a string with the letters
|
7
|
+
# each rotated by +degree+.
|
8
|
+
#
|
9
|
+
# You cannot use a multiple of 26 as the degree or a Rot13Error will
|
10
|
+
# be raised. So, your days of double rot13 encryption are over.
|
11
|
+
def initialize(str="", degree=13)
|
12
|
+
unless str.empty?
|
13
|
+
str = rotate_string(str, degree)
|
14
|
+
end
|
15
|
+
super(str)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Rotates the object by +degree+.
|
19
|
+
def rotate(degree)
|
20
|
+
rotate_string(self,degree)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def rotate_string(str,degree)
|
25
|
+
case degree.modulo(26)
|
26
|
+
when 0
|
27
|
+
raise Rot13Error, "degree must not be a multiple of 26"
|
28
|
+
when 13
|
29
|
+
str.tr!("a-zA-Z","n-za-mN-ZA-M")
|
30
|
+
else
|
31
|
+
str = str.unpack("C"*str.length).map!{ |e|
|
32
|
+
if e >= 97 && e <= 122
|
33
|
+
e = ((e - 97 + degree) % 26) + 97
|
34
|
+
elsif e >= 65 && e <= 90
|
35
|
+
e = ((e - 65 + degree) % 26) + 65
|
36
|
+
else
|
37
|
+
e = e
|
38
|
+
end
|
39
|
+
}.pack("C"*str.length)
|
40
|
+
end
|
41
|
+
return str
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/test/tc_rot13.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
##########################################################
|
2
|
+
# tc_rot13.rb
|
3
|
+
#
|
4
|
+
# Test suite for the crypt-rot13 package.
|
5
|
+
##########################################################
|
6
|
+
base = File.basename(Dir.pwd)
|
7
|
+
if base == "test" || base =~ /crypt-rot13/
|
8
|
+
Dir.chdir("..") if base == "test"
|
9
|
+
$LOAD_PATH.unshift(Dir.pwd + "/lib")
|
10
|
+
end
|
11
|
+
|
12
|
+
require "crypt/rot13"
|
13
|
+
require "test/unit"
|
14
|
+
include Crypt
|
15
|
+
|
16
|
+
class TC_Rot13 < Test::Unit::TestCase
|
17
|
+
def setup
|
18
|
+
@r1 = Rot13.new('abc')
|
19
|
+
@r2 = Rot13.new('ABC')
|
20
|
+
@r3 = Rot13.new('xyz')
|
21
|
+
@r4 = Rot13.new('XYZ')
|
22
|
+
@r5 = Rot13.new('a1b2c3@#$')
|
23
|
+
@r6 = Rot13.new('abc',3)
|
24
|
+
@r7 = Rot13.new('ABC',3)
|
25
|
+
@r8 = Rot13.new('xyz',23)
|
26
|
+
@r9 = Rot13.new('XYZ',23)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_version
|
30
|
+
assert_equal("1.0.1", Rot13::VERSION)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_constructor
|
34
|
+
assert_respond_to(Rot13,:new)
|
35
|
+
assert_nothing_raised{ Rot13.new }
|
36
|
+
assert_nothing_raised{ Rot13.new("foo") }
|
37
|
+
assert_nothing_raised{ Rot13.new("foo",7) }
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_available_methods
|
41
|
+
assert_respond_to(@r1, :rotate)
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_degree
|
45
|
+
assert_raises(Rot13Error){ Rot13.new("foo",26) }
|
46
|
+
assert_raises(Rot13Error){ Rot13.new("foo",52) }
|
47
|
+
assert_nothing_raised{ Rot13.new("foo",25) }
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_string_class
|
51
|
+
assert_kind_of(String,@r1)
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_return_value_default_degree
|
55
|
+
assert_equal('nop', @r1)
|
56
|
+
assert_equal('NOP', @r2)
|
57
|
+
assert_equal('klm', @r3)
|
58
|
+
assert_equal('KLM', @r4)
|
59
|
+
assert_equal('n1o2p3@#$', @r5)
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_return_value_custom_degree
|
63
|
+
assert_equal('def', @r6)
|
64
|
+
assert_equal('DEF', @r7)
|
65
|
+
assert_equal('uvw', @r8)
|
66
|
+
assert_equal('UVW', @r9)
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_rotate_instance_method
|
70
|
+
assert_equal('abc', @r6.rotate(-3))
|
71
|
+
end
|
72
|
+
|
73
|
+
def teardown
|
74
|
+
@r1 = nil
|
75
|
+
@r2 = nil
|
76
|
+
@r3 = nil
|
77
|
+
@r4 = nil
|
78
|
+
@r5 = nil
|
79
|
+
@r6 = nil
|
80
|
+
@r7 = nil
|
81
|
+
@r8 = nil
|
82
|
+
@r9 = nil
|
83
|
+
end
|
84
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.10
|
3
|
+
specification_version: 1
|
4
|
+
name: crypt-rot13
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 1.0.1
|
7
|
+
date: 2005-06-06
|
8
|
+
summary: "Character rotation encryption, i.e. Caesar Cipher"
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: djberg96@gmail.com
|
12
|
+
homepage: http://www.rubyforge.org/projects/shards
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
-
|
22
|
+
- ">"
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 0.0.0
|
25
|
+
version:
|
26
|
+
platform: ruby
|
27
|
+
authors:
|
28
|
+
- Daniel J. Berger
|
29
|
+
files:
|
30
|
+
- lib/crypt/rot13.rb
|
31
|
+
- README
|
32
|
+
- CHANGES
|
33
|
+
- MANIFEST
|
34
|
+
- test/tc_rot13.rb
|
35
|
+
test_files:
|
36
|
+
- test/tc_rot13.rb
|
37
|
+
rdoc_options: []
|
38
|
+
extra_rdoc_files:
|
39
|
+
- README
|
40
|
+
- CHANGES
|
41
|
+
executables: []
|
42
|
+
extensions: []
|
43
|
+
requirements: []
|
44
|
+
dependencies: []
|