bai-verhoeff 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/.gitignore +5 -0
- data/README.markdown +16 -0
- data/VERSION +1 -0
- data/lib/verhoeff.rb +1 -0
- data/lib/verhoeff/verhoeff.rb +43 -0
- data/rakefile +53 -0
- data/test/checksum_test.rb +19 -0
- data/test/test_helper.rb +7 -0
- data/verhoeff.gemspec +45 -0
- metadata +63 -0
data/README.markdown
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
This is pure Ruby 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.0.1
|
data/lib/verhoeff.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "verhoeff/verhoeff"
|
@@ -0,0 +1,43 @@
|
|
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
|
+
def self.checksum_digit_of(arg)
|
31
|
+
INV[arg.to_s.split("").reverse.enum_for(:each_with_index).inject(0) { |check,(x,i)|
|
32
|
+
D[check][P[i.next % 8][x[0] - ?0]]
|
33
|
+
}]
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.checksum_of(arg)
|
37
|
+
arg.to_i * 10 + checksum_digit_of(arg)
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.valid?(num)
|
41
|
+
checksum_digit_of(num.to_s[0..-2]) == num.to_s[-1] - ?0
|
42
|
+
end
|
43
|
+
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 = "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 = "beawesomeinstead@yahoo.com"
|
11
|
+
gem.homepage = "http://github.com/bai/verhoeff"
|
12
|
+
gem.authors = ["beawesomeinstead"]
|
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 File.dirname(__FILE__) + '/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,45 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{verhoeff}
|
5
|
+
s.version = "1.0.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["beawesomeinstead"]
|
9
|
+
s.date = %q{2009-08-04}
|
10
|
+
s.description = %q{Pure-Ruby implementation of the Verhoeff checksum algorithm.}
|
11
|
+
s.email = %q{beawesomeinstead@yahoo.com}
|
12
|
+
s.extra_rdoc_files = [
|
13
|
+
"README.markdown"
|
14
|
+
]
|
15
|
+
s.files = [
|
16
|
+
".gitignore",
|
17
|
+
"README.markdown",
|
18
|
+
"VERSION",
|
19
|
+
"lib/verhoeff.rb",
|
20
|
+
"lib/verhoeff/verhoeff.rb",
|
21
|
+
"rakefile",
|
22
|
+
"test/checksum_test.rb",
|
23
|
+
"test/test_helper.rb",
|
24
|
+
"verhoeff.gemspec"
|
25
|
+
]
|
26
|
+
s.homepage = %q{http://github.com/bai/verhoeff}
|
27
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
28
|
+
s.require_paths = ["lib"]
|
29
|
+
s.rubygems_version = %q{1.3.5}
|
30
|
+
s.summary = %q{Pure-Ruby implementation of the Verhoeff checksum algorithm.}
|
31
|
+
s.test_files = [
|
32
|
+
"test/checksum_test.rb",
|
33
|
+
"test/test_helper.rb"
|
34
|
+
]
|
35
|
+
|
36
|
+
if s.respond_to? :specification_version then
|
37
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
38
|
+
s.specification_version = 3
|
39
|
+
|
40
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
41
|
+
else
|
42
|
+
end
|
43
|
+
else
|
44
|
+
end
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bai-verhoeff
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- beawesomeinstead
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-08-04 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Pure-Ruby implementation of the Verhoeff checksum algorithm.
|
17
|
+
email: beawesomeinstead@yahoo.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.markdown
|
24
|
+
files:
|
25
|
+
- .gitignore
|
26
|
+
- README.markdown
|
27
|
+
- VERSION
|
28
|
+
- lib/verhoeff.rb
|
29
|
+
- lib/verhoeff/verhoeff.rb
|
30
|
+
- rakefile
|
31
|
+
- test/checksum_test.rb
|
32
|
+
- test/test_helper.rb
|
33
|
+
- verhoeff.gemspec
|
34
|
+
has_rdoc: false
|
35
|
+
homepage: http://github.com/bai/verhoeff
|
36
|
+
licenses:
|
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.3.5
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: Pure-Ruby implementation of the Verhoeff checksum algorithm.
|
61
|
+
test_files:
|
62
|
+
- test/checksum_test.rb
|
63
|
+
- test/test_helper.rb
|