verhoeff 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +3 -0
- data/Manifest.txt +7 -0
- data/README.txt +55 -0
- data/Rakefile +26 -0
- data/bin/verhoeff +0 -0
- data/lib/monkey_patches.rb +21 -0
- data/lib/verhoeff.rb +42 -0
- data/spec/checksum_spec.rb +18 -0
- data/spec/monkey_patches_spec.rb +24 -0
- metadata +70 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
verhoeff
|
2
|
+
by Jay Phillips (your name)
|
3
|
+
Codemecca LLC (http://codemecca.com)
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Pure-Ruby implementation of the Verhoeff checksum algorithm. Useful for attaching to numbers which will later be entered by an error-prone human. Typos in the number can be found quickly with the checksum before any further work is performed.
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
A library that returns
|
12
|
+
|
13
|
+
== SYNOPSIS:
|
14
|
+
|
15
|
+
Verhoeff.checkum_of 12345 # => "123451"
|
16
|
+
Verhoeff.checkum_of 54321 # => "543217"
|
17
|
+
Verhoeff.checks_out? 543217 # => true
|
18
|
+
Verhoeff.checks_out? 543211 # => false
|
19
|
+
|
20
|
+
Or, if you've required verhoeff/monkey_patches, you can do this:
|
21
|
+
|
22
|
+
656.checksum # => 6569
|
23
|
+
"657".checksum # => 6576
|
24
|
+
6567.checks_out? # => true
|
25
|
+
"6560".checks_out? # => false
|
26
|
+
|
27
|
+
|
28
|
+
== INSTALL:
|
29
|
+
|
30
|
+
* sudo gem install verhoeff
|
31
|
+
|
32
|
+
== LICENSE:
|
33
|
+
|
34
|
+
(The MIT License)
|
35
|
+
|
36
|
+
Copyright (c) 2007 Jay Phillips
|
37
|
+
|
38
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
39
|
+
a copy of this software and associated documentation files (the
|
40
|
+
'Software'), to deal in the Software without restriction, including
|
41
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
42
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
43
|
+
permit persons to whom the Software is furnished to do so, subject to
|
44
|
+
the following conditions:
|
45
|
+
|
46
|
+
The above copyright notice and this permission notice shall be
|
47
|
+
included in all copies or substantial portions of the Software.
|
48
|
+
|
49
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
50
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
51
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
52
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
53
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
54
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
55
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
require './lib/verhoeff.rb'
|
6
|
+
require 'spec/rake/spectask'
|
7
|
+
require 'rake/gempackagetask'
|
8
|
+
|
9
|
+
Hoe.new('verhoeff', Verhoeff::VERSION) do |p|
|
10
|
+
p.rubyforge_name = 'verhoeff'
|
11
|
+
p.author = 'Jay Phillips'
|
12
|
+
p.email = 'jay -at- codemecca.com'
|
13
|
+
p.summary = 'A pure-Ruby implementation of the Verhoeff checksum algorithm'
|
14
|
+
p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
|
15
|
+
p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
|
16
|
+
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
|
17
|
+
p.spec_extras = {
|
18
|
+
"files" => Dir['**/*']
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
Spec::Rake::SpecTask.new "spec" do |t|
|
23
|
+
t.spec_opts = ["-c"]
|
24
|
+
t.spec_files = Dir['spec/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
data/bin/verhoeff
ADDED
File without changes
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/verhoeff.rb'
|
2
|
+
|
3
|
+
module Verhoeff
|
4
|
+
module ChecksumMethods
|
5
|
+
def checksum
|
6
|
+
Verhoeff.checksum_of self
|
7
|
+
end
|
8
|
+
def checksum_digit
|
9
|
+
Verhoeff.checksum_digit_of self
|
10
|
+
end
|
11
|
+
def checks_out?
|
12
|
+
Verhoeff.checks_out? self
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
[Numeric,String].each do |klass|
|
18
|
+
klass.class_eval do
|
19
|
+
include Verhoeff::ChecksumMethods
|
20
|
+
end
|
21
|
+
end
|
data/lib/verhoeff.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'enumerator'
|
2
|
+
module Verhoeff
|
3
|
+
VERSION = '1.0.0'
|
4
|
+
|
5
|
+
# Verhoeff Multiplication Table
|
6
|
+
M = [ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
7
|
+
[1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
|
8
|
+
[2, 3, 4, 0, 1, 7, 8, 9, 5, 6],
|
9
|
+
[3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
|
10
|
+
[4, 0, 1, 2, 3, 9, 5, 6, 7, 8],
|
11
|
+
[5, 9, 8, 7, 6, 0, 4, 3, 2, 1],
|
12
|
+
[6, 5, 9, 8, 7, 1, 0, 4, 3, 2],
|
13
|
+
[7, 6, 5, 9, 8, 2, 1, 0, 4, 3],
|
14
|
+
[8, 7, 6, 5, 9, 3, 2, 1, 0, 4],
|
15
|
+
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0] ]
|
16
|
+
|
17
|
+
P = [ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
18
|
+
[1, 5, 7, 6, 2, 8, 3, 0, 9, 4],
|
19
|
+
[5, 8, 0, 3, 7, 9, 6, 1, 4, 2],
|
20
|
+
[8, 9, 1, 6, 0, 4, 3, 5, 2, 7],
|
21
|
+
[9, 4, 5, 3, 1, 2, 6, 8, 7, 0],
|
22
|
+
[4, 2, 8, 6, 5, 7, 3, 9, 0, 1],
|
23
|
+
[2, 7, 9, 3, 8, 0, 6, 4, 1, 5],
|
24
|
+
[7, 0, 4, 6, 9, 1, 3, 2, 5, 8] ]
|
25
|
+
|
26
|
+
INV = [0, 4, 3, 2, 1, 5, 6, 7, 8, 9]
|
27
|
+
|
28
|
+
def self.checksum_digit_of(arg)
|
29
|
+
INV[arg.to_s.split("").reverse.enum_for(:each_with_index).inject(0) { |check,(x,i)|
|
30
|
+
M[check][P[i.next % 8][x[0] - ?0]]
|
31
|
+
}]
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.checksum_of(arg)
|
35
|
+
arg.to_i * 10 + checksum_digit_of(arg)
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.checks_out?(num)
|
39
|
+
checksum_digit_of(num.to_s[0..-2]) == num.to_s[-1] - ?0
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../lib/verhoeff.rb'
|
2
|
+
|
3
|
+
describe "The Verhoeff checksum algorithm implementation" do
|
4
|
+
it "should work with correct, previously calculated checksums" do
|
5
|
+
(1..23).inject(1) do |checksum,i|
|
6
|
+
Verhoeff.checksum_of checksum
|
7
|
+
end.should eql(150493068613366131371194)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should work when given a String argument and return a Fixnum" do
|
11
|
+
Verhoeff.checksum_of("999").should equal(9998)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should validate a properly generated checksum" do
|
15
|
+
Verhoeff.checks_out?(13375).should be_true
|
16
|
+
Verhoeff.checks_out?(13735).should be_false
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../lib/monkey_patches.rb"
|
2
|
+
|
3
|
+
KNOWN_CASES = {
|
4
|
+
123 => 3,
|
5
|
+
765 => 7,
|
6
|
+
43167312675318282 => 3,
|
7
|
+
01 => 5
|
8
|
+
}
|
9
|
+
|
10
|
+
describe "The Verhoeff checksum monkey patches" do
|
11
|
+
|
12
|
+
it "should monkey patch Numeric and String properly" do
|
13
|
+
check = lambda do |k,v|
|
14
|
+
k.checksum_digit.should eql(v)
|
15
|
+
k.checksum.should eql(k.to_i*10+v)
|
16
|
+
(k.to_i*10+v).should be_checks_out
|
17
|
+
end
|
18
|
+
KNOWN_CASES.each_pair &check
|
19
|
+
KNOWN_CASES.each_pair do |k,v|
|
20
|
+
check.call(k.to_s,v)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.2
|
3
|
+
specification_version: 1
|
4
|
+
name: verhoeff
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 1.0.0
|
7
|
+
date: 2007-07-15 00:00:00 -07:00
|
8
|
+
summary: A pure-Ruby implementation of the Verhoeff checksum algorithm
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: jay -at- codemecca.com
|
12
|
+
homepage: " by Jay Phillips (your name)"
|
13
|
+
rubyforge_project: verhoeff
|
14
|
+
description: "== FEATURES/PROBLEMS: A library that returns == SYNOPSIS: Verhoeff.checkum_of 12345 # => \"123451\" Verhoeff.checkum_of 54321 # => \"543217\" Verhoeff.checks_out? 543217 # => true Verhoeff.checks_out? 543211 # => false Or, if you've required verhoeff/monkey_patches, you can do this: 656.checksum # => 6569 \"657\".checksum # => 6576 6567.checks_out? # => true \"6560\".checks_out? # => false"
|
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
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Jay Phillips
|
31
|
+
files:
|
32
|
+
- bin
|
33
|
+
- bin/verhoeff
|
34
|
+
- History.txt
|
35
|
+
- lib
|
36
|
+
- lib/monkey_patches.rb
|
37
|
+
- lib/verhoeff.rb
|
38
|
+
- Manifest.txt
|
39
|
+
- pkg
|
40
|
+
- Rakefile
|
41
|
+
- README.txt
|
42
|
+
- spec
|
43
|
+
- spec/checksum_spec.rb
|
44
|
+
- spec/monkey_patches_spec.rb
|
45
|
+
- test
|
46
|
+
test_files: []
|
47
|
+
|
48
|
+
rdoc_options:
|
49
|
+
- --main
|
50
|
+
- README.txt
|
51
|
+
extra_rdoc_files:
|
52
|
+
- History.txt
|
53
|
+
- Manifest.txt
|
54
|
+
- README.txt
|
55
|
+
executables:
|
56
|
+
- verhoeff
|
57
|
+
extensions: []
|
58
|
+
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
dependencies:
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: hoe
|
64
|
+
version_requirement:
|
65
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 1.2.1
|
70
|
+
version:
|