oklasoft-verhoeff 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/README.markdown +16 -0
- data/VERSION +1 -0
- data/lib/verhoeff.rb +1 -0
- data/lib/verhoeff/verhoeff.rb +45 -0
- data/rakefile +53 -0
- data/test/checksum_test.rb +19 -0
- data/test/test_helper.rb +7 -0
- data/verhoeff.gemspec +49 -0
- metadata +77 -0
data/README.markdown
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
This is pure Ruby (1.9 compatible) implementation of the Verchoeff algorithm.
|
2
|
+
|
3
|
+
### Trivia
|
4
|
+
The Verhoeff algorithm uses the properties of D5 (the dihedral group of order 10) — a non-commutative system of operations on ten elements, corresponding to the results of rotating or reflecting (flipping) a regular pentagon.
|
5
|
+
|
6
|
+
### Implementation
|
7
|
+
The Verhoeff algorithm implemented using three tables: a multiplication table D, a permutation table P, and an inverse table INV. All tables are precomputed.
|
8
|
+
|
9
|
+
### Example
|
10
|
+
Verhoeff.checkum_of 12345 # => "123451"
|
11
|
+
Verhoeff.checkum_of 54321 # => "543217"
|
12
|
+
Verhoeff.valid? 543217 # => true
|
13
|
+
Verhoeff.valid? 543211 # => false
|
14
|
+
|
15
|
+
### References
|
16
|
+
* Verhoeff, J. “Error Detecting Decimal Codes”, Mathematical Centre Tract 29, The Mathematical Centre, Amsterdam, 1969.
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.1.0
|
data/lib/verhoeff.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "verhoeff/verhoeff"
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'enumerator'
|
2
|
+
|
3
|
+
module Verhoeff
|
4
|
+
D = [
|
5
|
+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
6
|
+
[1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
|
7
|
+
[2, 3, 4, 0, 1, 7, 8, 9, 5, 6],
|
8
|
+
[3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
|
9
|
+
[4, 0, 1, 2, 3, 9, 5, 6, 7, 8],
|
10
|
+
[5, 9, 8, 7, 6, 0, 4, 3, 2, 1],
|
11
|
+
[6, 5, 9, 8, 7, 1, 0, 4, 3, 2],
|
12
|
+
[7, 6, 5, 9, 8, 2, 1, 0, 4, 3],
|
13
|
+
[8, 7, 6, 5, 9, 3, 2, 1, 0, 4],
|
14
|
+
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
|
15
|
+
]
|
16
|
+
|
17
|
+
P = [
|
18
|
+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
19
|
+
[1, 5, 7, 6, 2, 8, 3, 0, 9, 4],
|
20
|
+
[5, 8, 0, 3, 7, 9, 6, 1, 4, 2],
|
21
|
+
[8, 9, 1, 6, 0, 4, 3, 5, 2, 7],
|
22
|
+
[9, 4, 5, 3, 1, 2, 6, 8, 7, 0],
|
23
|
+
[4, 2, 8, 6, 5, 7, 3, 9, 0, 1],
|
24
|
+
[2, 7, 9, 3, 8, 0, 6, 4, 1, 5],
|
25
|
+
[7, 0, 4, 6, 9, 1, 3, 2, 5, 8]
|
26
|
+
]
|
27
|
+
|
28
|
+
INV = [0, 4, 3, 2, 1, 5, 6, 7, 8, 9]
|
29
|
+
|
30
|
+
ZERO_ORDINAL = '0'.each_byte.first
|
31
|
+
|
32
|
+
def self.checksum_digit_of(arg)
|
33
|
+
INV[arg.to_s.each_byte.reverse_each.enum_for(:each_with_index).inject(0) { |check,(x,i)|
|
34
|
+
D[check][P[i.next % 8][x - ZERO_ORDINAL]]
|
35
|
+
}]
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.checksum_of(arg)
|
39
|
+
arg.to_i * 10 + checksum_digit_of(arg)
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.valid?(num)
|
43
|
+
checksum_digit_of(num.to_s[0..-2]) == num.to_s.each_byte.reverse_each.next - ZERO_ORDINAL
|
44
|
+
end
|
45
|
+
end
|
data/rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "oklasoft-verhoeff"
|
8
|
+
gem.summary = %Q{Pure-Ruby implementation of the Verhoeff checksum algorithm.}
|
9
|
+
gem.description = %Q{Pure-Ruby implementation of the Verhoeff checksum algorithm.}
|
10
|
+
gem.email = "oklasoft@lupus.omrf.org"
|
11
|
+
gem.homepage = "http://github.com/oklasoft/verhoeff"
|
12
|
+
gem.authors = ["beawesomeinstead","oklasoft"]
|
13
|
+
end
|
14
|
+
rescue LoadError
|
15
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'rake/testtask'
|
19
|
+
Rake::TestTask.new(:test) do |test|
|
20
|
+
test.libs << 'lib' << 'test'
|
21
|
+
test.pattern = 'test/**/*_test.rb'
|
22
|
+
test.verbose = true
|
23
|
+
end
|
24
|
+
|
25
|
+
begin
|
26
|
+
require 'rcov/rcovtask'
|
27
|
+
Rcov::RcovTask.new do |test|
|
28
|
+
test.libs << 'test'
|
29
|
+
test.pattern = 'test/**/*_test.rb'
|
30
|
+
test.verbose = true
|
31
|
+
end
|
32
|
+
rescue LoadError
|
33
|
+
task :rcov do
|
34
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
require 'rake/rdoctask'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
if File.exist?('VERSION.yml')
|
41
|
+
config = YAML.load(File.read('VERSION.yml'))
|
42
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
43
|
+
elsif File.exist?('VERSION')
|
44
|
+
version = File.read('VERSION')
|
45
|
+
else
|
46
|
+
version = ""
|
47
|
+
end
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "verhoeff #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ChecksumTest < Test::Unit::TestCase
|
4
|
+
context "The Verhoeff checksum algorithm" do
|
5
|
+
should "work with correct, previously calculated checksums" do
|
6
|
+
checksum = (1..23).inject(1) { |checksum, i| Verhoeff.checksum_of checksum }
|
7
|
+
assert_equal 150493068613366131371194, checksum
|
8
|
+
end
|
9
|
+
|
10
|
+
should "work when given a String argument and return a Fixnum" do
|
11
|
+
assert_equal 9998, Verhoeff.checksum_of("999")
|
12
|
+
end
|
13
|
+
|
14
|
+
should "validate a properly generated checksum" do
|
15
|
+
assert_equal true, Verhoeff.valid?(13375)
|
16
|
+
assert_equal false, Verhoeff.valid?(13735)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/test/test_helper.rb
ADDED
data/verhoeff.gemspec
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{verhoeff}
|
8
|
+
s.version = "1.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["beawesomeinstead", "oklasoft"]
|
12
|
+
s.date = %q{2010-11-05}
|
13
|
+
s.description = %q{Pure-Ruby implementation of the Verhoeff checksum algorithm.}
|
14
|
+
s.email = %q{oklasoft@lupus.omrf.org}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.markdown"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".gitignore",
|
20
|
+
"README.markdown",
|
21
|
+
"VERSION",
|
22
|
+
"lib/verhoeff.rb",
|
23
|
+
"lib/verhoeff/verhoeff.rb",
|
24
|
+
"rakefile",
|
25
|
+
"test/checksum_test.rb",
|
26
|
+
"test/test_helper.rb",
|
27
|
+
"verhoeff.gemspec"
|
28
|
+
]
|
29
|
+
s.homepage = %q{http://github.com/oklasoft/verhoeff}
|
30
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
31
|
+
s.require_paths = ["lib"]
|
32
|
+
s.rubygems_version = %q{1.3.7}
|
33
|
+
s.summary = %q{Pure-Ruby implementation of the Verhoeff checksum algorithm.}
|
34
|
+
s.test_files = [
|
35
|
+
"test/checksum_test.rb",
|
36
|
+
"test/test_helper.rb"
|
37
|
+
]
|
38
|
+
|
39
|
+
if s.respond_to? :specification_version then
|
40
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
41
|
+
s.specification_version = 3
|
42
|
+
|
43
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
44
|
+
else
|
45
|
+
end
|
46
|
+
else
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: oklasoft-verhoeff
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 1.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- beawesomeinstead
|
14
|
+
- oklasoft
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2010-11-05 00:00:00 -05:00
|
20
|
+
default_executable:
|
21
|
+
dependencies: []
|
22
|
+
|
23
|
+
description: Pure-Ruby implementation of the Verhoeff checksum algorithm.
|
24
|
+
email: oklasoft@lupus.omrf.org
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files:
|
30
|
+
- README.markdown
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- README.markdown
|
34
|
+
- VERSION
|
35
|
+
- lib/verhoeff.rb
|
36
|
+
- lib/verhoeff/verhoeff.rb
|
37
|
+
- rakefile
|
38
|
+
- test/checksum_test.rb
|
39
|
+
- test/test_helper.rb
|
40
|
+
- verhoeff.gemspec
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: http://github.com/oklasoft/verhoeff
|
43
|
+
licenses: []
|
44
|
+
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --charset=UTF-8
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
hash: 3
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
hash: 3
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project:
|
71
|
+
rubygems_version: 1.3.7
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: Pure-Ruby implementation of the Verhoeff checksum algorithm.
|
75
|
+
test_files:
|
76
|
+
- test/checksum_test.rb
|
77
|
+
- test/test_helper.rb
|