bkerley-affine 0.1.0
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/LICENSE +20 -0
- data/README.rdoc +11 -0
- data/Rakefile +56 -0
- data/VERSION.yml +4 -0
- data/lib/affine/cipher.rb +36 -0
- data/lib/affine/errors.rb +14 -0
- data/lib/affine.rb +5 -0
- data/test/affine_test.rb +48 -0
- data/test/test_helper.rb +10 -0
- metadata +63 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Bryce Kerley
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
= affine
|
2
|
+
|
3
|
+
Simple affine cipher
|
4
|
+
|
5
|
+
a = Affine::Cipher.new(2176782371, 65182241782, 123235151)
|
6
|
+
r = rand(123235150) # (should be smaller then the modulus)
|
7
|
+
r == a.decipher(a.encipher r)
|
8
|
+
|
9
|
+
== Copyright
|
10
|
+
|
11
|
+
Copyright (c) 2009 Bryce Kerley. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "affine"
|
8
|
+
gem.summary = %Q{Simple affine cipher for Ruby}
|
9
|
+
gem.email = "bkerley@brycekerley.net"
|
10
|
+
gem.homepage = "http://github.com/bkerley/affine"
|
11
|
+
gem.authors = ["Bryce Kerley"]
|
12
|
+
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
end
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
Rake::TestTask.new(:test) do |test|
|
21
|
+
test.libs << 'lib' << 'test'
|
22
|
+
test.pattern = 'test/**/*_test.rb'
|
23
|
+
test.verbose = true
|
24
|
+
end
|
25
|
+
|
26
|
+
begin
|
27
|
+
require 'rcov/rcovtask'
|
28
|
+
Rcov::RcovTask.new do |test|
|
29
|
+
test.libs << 'test'
|
30
|
+
test.pattern = 'test/**/*_test.rb'
|
31
|
+
test.verbose = true
|
32
|
+
end
|
33
|
+
rescue LoadError
|
34
|
+
task :rcov do
|
35
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
task :default => :test
|
41
|
+
|
42
|
+
require 'rake/rdoctask'
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
44
|
+
if File.exist?('VERSION.yml')
|
45
|
+
config = YAML.load(File.read('VERSION.yml'))
|
46
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
47
|
+
else
|
48
|
+
version = ""
|
49
|
+
end
|
50
|
+
|
51
|
+
rdoc.rdoc_dir = 'rdoc'
|
52
|
+
rdoc.title = "affine #{version}"
|
53
|
+
rdoc.rdoc_files.include('README*')
|
54
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
55
|
+
end
|
56
|
+
|
data/VERSION.yml
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module Affine
|
2
|
+
class Cipher
|
3
|
+
def initialize(modulus, a_key, b_key)
|
4
|
+
raise CoprimeError.new(modulus, a_key) if modulus.gcd(a_key) != 1
|
5
|
+
@modulus = modulus
|
6
|
+
@a_key = a_key
|
7
|
+
@b_key = b_key
|
8
|
+
@a_inv = extended_gcd(a_key, modulus)
|
9
|
+
end
|
10
|
+
def encipher(plaintext)
|
11
|
+
raise RangeError.new(plaintext, @modulus) if plaintext > @modulus
|
12
|
+
((@a_key * plaintext) + @b_key) % @modulus
|
13
|
+
end
|
14
|
+
def decipher(ciphertext)
|
15
|
+
raise RangeError.new(ciphertext, @modulus) if ciphertext > @modulus
|
16
|
+
(@a_inv * (ciphertext - @b_key)) % @modulus
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
# from http://snippets.dzone.com/posts/show/6074
|
21
|
+
def extended_gcd(b,m,_recursion_depth=0)
|
22
|
+
if b % m == 0
|
23
|
+
temp = [0,1]
|
24
|
+
return temp
|
25
|
+
else
|
26
|
+
temp = extended_gcd(m, b % m, _recursion_depth+1)
|
27
|
+
temp2 = [temp[1], temp[0]-temp[1] * ((b/m).to_i)]
|
28
|
+
if _recursion_depth == 0
|
29
|
+
return temp2[0] % m
|
30
|
+
else
|
31
|
+
return temp2
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Affine
|
2
|
+
class AffineError < StandardError
|
3
|
+
end
|
4
|
+
class CoprimeError < AffineError
|
5
|
+
def initialize(a, b)
|
6
|
+
super("Expected #{a} to be coprime with #{b}")
|
7
|
+
end
|
8
|
+
end
|
9
|
+
class RangeError < AffineError
|
10
|
+
def initialize(n, mod)
|
11
|
+
super("Expected input #{n} to be smaller than modulus #{mod}")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/affine.rb
ADDED
data/test/affine_test.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class AffineTest < Test::Unit::TestCase
|
4
|
+
context 'affine cipher 5,8 mod 26' do
|
5
|
+
setup do
|
6
|
+
@a = Affine::Cipher.new(26, 5, 8)
|
7
|
+
end
|
8
|
+
|
9
|
+
should 'be created correctly' do
|
10
|
+
assert_nothing_raised do
|
11
|
+
Affine::Cipher.new(26, 5, 8)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
should 'encipher ITSCOOL correctly' do
|
16
|
+
# example from http://en.wikipedia.org/wiki/Affine_cipher
|
17
|
+
plain = 'ITSCOOL'.split('').map{ |c| c[0] - 65}
|
18
|
+
coded = plain.map{ |c| @a.encipher c }
|
19
|
+
check ='WZUSAAL'.split('').map{ |c| c[0] - 65}
|
20
|
+
assert_equal check, coded
|
21
|
+
end
|
22
|
+
should 'decipiher WZUSAAL correctly' do
|
23
|
+
# example from http://en.wikipedia.org/wiki/Affine_cipher
|
24
|
+
check ='WZUSAAL'.split('').map{ |c| c[0] - 65}
|
25
|
+
coded = check.map{ |c| @a.decipher c }
|
26
|
+
plain = 'ITSCOOL'.split('').map{ |c| c[0] - 65}
|
27
|
+
assert_equal plain, coded
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'affine cipher 65182241782, 123235151 mod 2176782371' do
|
32
|
+
setup do
|
33
|
+
@a = Affine::Cipher.new(2176782371, 65182241782, 123235151)
|
34
|
+
end
|
35
|
+
should 'get created correctly' do
|
36
|
+
assert_nothing_raised do
|
37
|
+
Affine::Cipher.new(2176782371, 65182241782, 123235151)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
should 'pass a few random identity checks' do
|
41
|
+
# I'm not doing this by hand
|
42
|
+
1000.times do
|
43
|
+
r = rand(123235150)
|
44
|
+
assert_equal r, @a.decipher(@a.encipher r)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bkerley-affine
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bryce Kerley
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-14 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: bkerley@brycekerley.net
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- LICENSE
|
27
|
+
- README.rdoc
|
28
|
+
- Rakefile
|
29
|
+
- VERSION.yml
|
30
|
+
- lib/affine.rb
|
31
|
+
- lib/affine/cipher.rb
|
32
|
+
- lib/affine/errors.rb
|
33
|
+
- test/affine_test.rb
|
34
|
+
- test/test_helper.rb
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://github.com/bkerley/affine
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options:
|
39
|
+
- --charset=UTF-8
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.2.0
|
58
|
+
signing_key:
|
59
|
+
specification_version: 2
|
60
|
+
summary: Simple affine cipher for Ruby
|
61
|
+
test_files:
|
62
|
+
- test/affine_test.rb
|
63
|
+
- test/test_helper.rb
|