sjekksum 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sjekksum/damm.rb +46 -9
- data/lib/sjekksum/luhn.rb +45 -9
- data/lib/sjekksum/shared.rb +8 -2
- data/lib/sjekksum/verhoeff.rb +48 -9
- data/lib/sjekksum/version.rb +2 -1
- data/lib/sjekksum.rb +120 -19
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e6d1ff16d06db67ad288e906d6550e945c1550e4
|
4
|
+
data.tar.gz: daf3d1554d22f668c7f61af87a86feb00f8e0c39
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f20e2ba3177709dd77cc43fd92b541f889b2fc192bbe9fdba0eae82492c7dac8c3f5f90fa34231efb865f0c675f68f607feef574708f8566ef5cde3b2126c721
|
7
|
+
data.tar.gz: 5b13d80284d30c3afdb52161f2f90f5c532e4823bd5cfde53e51646f1be57a5b33561954c39fce7357957fa51fbc8b88c252d9b2a08b9bbc5da59e27e25d7ae9
|
data/lib/sjekksum/damm.rb
CHANGED
@@ -1,8 +1,15 @@
|
|
1
1
|
module Sjekksum
|
2
|
+
#
|
3
|
+
# Module for calculation and validation of Damm checksums
|
4
|
+
#
|
5
|
+
# @see http://en.wikipedia.org/wiki/Damm_algorithm Damm algorithm
|
6
|
+
#
|
2
7
|
module Damm
|
3
8
|
extend self
|
4
9
|
extend Shared
|
5
10
|
|
11
|
+
# The totally anti-symmetric quasigroup
|
12
|
+
# @see http://archiv.ub.uni-marburg.de/diss/z2004/0516/pdf/dhmd.pdf [PDF] Damm, H. Michael (2004). Total anti-symmetrische Quasigruppen (Dr. rer. nat.). Philipps-Universität Marburg.
|
6
13
|
QUASIGROUP = [
|
7
14
|
[0, 3, 1, 7, 5, 9, 8, 6, 4, 2],
|
8
15
|
[7, 0, 9, 2, 1, 5, 4, 8, 6, 3],
|
@@ -16,21 +23,51 @@ module Sjekksum
|
|
16
23
|
[2, 5, 8, 1, 4, 3, 6, 7, 9, 0]
|
17
24
|
].freeze
|
18
25
|
|
19
|
-
|
20
|
-
|
21
|
-
|
26
|
+
#
|
27
|
+
# Calculates Damm checksum
|
28
|
+
#
|
29
|
+
# @example
|
30
|
+
# Sjekksum::Damm.of(572) #=> 4
|
31
|
+
#
|
32
|
+
# @param number [Integer] number for which the checksum should be calculated
|
33
|
+
#
|
34
|
+
# @return [Integer] calculated checksum
|
35
|
+
def of number
|
36
|
+
raise_on_type_mismatch number
|
37
|
+
digits = number.to_s.chars.map(&:to_i)
|
22
38
|
digits.reduce(0){|check, digit| QUASIGROUP[check][digit] }
|
23
39
|
end
|
40
|
+
alias_method :checksum, :of
|
24
41
|
|
25
|
-
|
26
|
-
|
27
|
-
|
42
|
+
#
|
43
|
+
# Damm validation of provided number
|
44
|
+
#
|
45
|
+
# @example
|
46
|
+
# Sjekksum::Damm.valid?(5724) #=> true
|
47
|
+
#
|
48
|
+
# @param number [Integer] number with included checksum
|
49
|
+
#
|
50
|
+
# @return [Boolean]
|
51
|
+
def valid? number
|
52
|
+
raise_on_type_mismatch number
|
53
|
+
self.of(number).zero?
|
28
54
|
end
|
55
|
+
alias_method :is_valid?, :valid?
|
29
56
|
|
30
|
-
|
31
|
-
|
32
|
-
|
57
|
+
#
|
58
|
+
# Transforms a number by appending the Damm checksum digit
|
59
|
+
#
|
60
|
+
# @example
|
61
|
+
# Sjekksum::Damm.convert(572) #=> 5724
|
62
|
+
#
|
63
|
+
# @param number [Integer] number without a checksum
|
64
|
+
#
|
65
|
+
# @return [Integer] final number including the checksum
|
66
|
+
def convert number
|
67
|
+
raise_on_type_mismatch number
|
68
|
+
(number * 10) + self.of(number)
|
33
69
|
end
|
70
|
+
alias_method :transform, :convert
|
34
71
|
|
35
72
|
end
|
36
73
|
end
|
data/lib/sjekksum/luhn.rb
CHANGED
@@ -1,26 +1,62 @@
|
|
1
1
|
module Sjekksum
|
2
|
+
#
|
3
|
+
# Module for calculation and validation of Luhn checksums
|
4
|
+
#
|
5
|
+
# @see http://en.wikipedia.org/wiki/Luhn_algorithm Luhn algorithm
|
6
|
+
#
|
2
7
|
module Luhn
|
3
8
|
extend self
|
4
9
|
extend Shared
|
5
10
|
|
6
|
-
|
7
|
-
|
8
|
-
|
11
|
+
#
|
12
|
+
# Calculates Luhn checksum
|
13
|
+
#
|
14
|
+
# @example
|
15
|
+
# Sjekksum::Luhn.of(7992739871) #=> 3
|
16
|
+
#
|
17
|
+
# @param number [Integer] number for which the checksum should be calculated
|
18
|
+
#
|
19
|
+
# @return [Integer] calculated checksum
|
20
|
+
def of number
|
21
|
+
raise_on_type_mismatch number
|
22
|
+
digits = number.to_s.chars.map(&:to_i)
|
9
23
|
sum = digits.reverse.map.with_index do |digit, idx|
|
10
24
|
idx.even? ? (digit * 2).divmod(10).reduce(&:+) : digit
|
11
25
|
end.reverse.reduce(&:+)
|
12
26
|
|
13
27
|
(10 - sum % 10) % 10
|
14
28
|
end
|
29
|
+
alias_method :checksum, :of
|
15
30
|
|
16
|
-
|
17
|
-
|
18
|
-
|
31
|
+
#
|
32
|
+
# Luhn validation of provided number
|
33
|
+
#
|
34
|
+
# @example
|
35
|
+
# Sjekksum::Luhn.valid?(79927398713) #=> true
|
36
|
+
#
|
37
|
+
# @param number [Integer] number with included checksum
|
38
|
+
#
|
39
|
+
# @return [Boolean]
|
40
|
+
def valid? number
|
41
|
+
raise_on_type_mismatch number
|
42
|
+
self.of(number.div(10)) == (number % 10)
|
19
43
|
end
|
44
|
+
alias_method :is_valid?, :valid?
|
20
45
|
|
21
|
-
|
22
|
-
|
23
|
-
|
46
|
+
#
|
47
|
+
# Transforms a number by appending the Luhn checksum digit
|
48
|
+
#
|
49
|
+
# @example
|
50
|
+
# Sjekksum::Luhn.convert(7992739871) #=> 79927398713
|
51
|
+
#
|
52
|
+
# @param number [Integer] number without a checksum
|
53
|
+
#
|
54
|
+
# @return [Integer] final number including the checksum
|
55
|
+
def convert number
|
56
|
+
raise_on_type_mismatch number
|
57
|
+
(number * 10) + self.of(number)
|
24
58
|
end
|
59
|
+
alias_method :transform, :convert
|
60
|
+
|
25
61
|
end
|
26
62
|
end
|
data/lib/sjekksum/shared.rb
CHANGED
@@ -1,10 +1,16 @@
|
|
1
1
|
module Sjekksum
|
2
|
+
#
|
3
|
+
# Shared helper methods
|
4
|
+
#
|
2
5
|
module Shared
|
3
6
|
|
4
7
|
private
|
5
8
|
|
6
|
-
|
7
|
-
|
9
|
+
#
|
10
|
+
# Checks input type and raises if requirement is not met (currently the value has to be an integer)
|
11
|
+
# @param number [Object]
|
12
|
+
def raise_on_type_mismatch number
|
13
|
+
raise ArgumentError unless number.is_a?(Integer)
|
8
14
|
end
|
9
15
|
|
10
16
|
end
|
data/lib/sjekksum/verhoeff.rb
CHANGED
@@ -1,8 +1,14 @@
|
|
1
1
|
module Sjekksum
|
2
|
+
#
|
3
|
+
# Module for calculation and validation of Verhoeff checksums
|
4
|
+
#
|
5
|
+
# @see http://en.wikipedia.org/wiki/Verhoeff_algorithm Verhoeff algorithm
|
6
|
+
#
|
2
7
|
module Verhoeff
|
3
8
|
extend self
|
4
9
|
extend Shared
|
5
10
|
|
11
|
+
# Table of multiplication in the dihedral group D5
|
6
12
|
DIHEDRAL_GROUP_D5 = [
|
7
13
|
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
|
8
14
|
[ 1, 2, 3, 4, 0, 6, 7, 8, 9, 5 ],
|
@@ -16,6 +22,7 @@ module Sjekksum
|
|
16
22
|
[ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ]
|
17
23
|
].freeze
|
18
24
|
|
25
|
+
# The permutation table
|
19
26
|
PERMUTATION = [
|
20
27
|
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
|
21
28
|
[ 1, 5, 7, 6, 2, 8, 3, 0, 9, 4 ],
|
@@ -27,25 +34,57 @@ module Sjekksum
|
|
27
34
|
[ 7, 0, 4, 6, 9, 1, 3, 2, 5, 8 ]
|
28
35
|
].freeze
|
29
36
|
|
37
|
+
# The multiplicative inverse of a digit in the dihedral group D5
|
30
38
|
INVERSE = [ 0, 4, 3, 2, 1, 5, 6, 7, 8, 9 ].freeze
|
31
39
|
|
32
|
-
|
33
|
-
|
34
|
-
|
40
|
+
#
|
41
|
+
# Calculates Verhoeff checksum
|
42
|
+
#
|
43
|
+
# @example
|
44
|
+
# Sjekksum::Verhoeff.of(142857) #=> 0
|
45
|
+
#
|
46
|
+
# @param number [Integer] number for which the checksum should be calculated
|
47
|
+
#
|
48
|
+
# @return [Integer] calculated checksum
|
49
|
+
def of number
|
50
|
+
raise_on_type_mismatch number
|
51
|
+
digits = number.to_s.chars.map(&:to_i)
|
35
52
|
INVERSE[digits.reverse_each.with_index.reduce(0) { |check, (digit, idx)|
|
36
53
|
d_row = DIHEDRAL_GROUP_D5[check]
|
37
54
|
d_row[ PERMUTATION[idx.next % 8][digit] ]
|
38
55
|
}]
|
39
56
|
end
|
57
|
+
alias_method :checksum, :of
|
40
58
|
|
41
|
-
|
42
|
-
|
43
|
-
|
59
|
+
#
|
60
|
+
# Verhoeff validation of provided number
|
61
|
+
#
|
62
|
+
# @example
|
63
|
+
# Sjekksum::Verhoeff.valid?(1428570) #=> true
|
64
|
+
#
|
65
|
+
# @param number [Integer] number with included checksum
|
66
|
+
#
|
67
|
+
# @return [Boolean]
|
68
|
+
def valid? number
|
69
|
+
raise_on_type_mismatch number
|
70
|
+
self.of(number.div(10)) == (number % 10)
|
44
71
|
end
|
72
|
+
alias_method :is_valid?, :valid?
|
45
73
|
|
46
|
-
|
47
|
-
|
48
|
-
|
74
|
+
#
|
75
|
+
# Transforms a number by appending the Verhoeff checksum digit
|
76
|
+
#
|
77
|
+
# @example
|
78
|
+
# Sjekksum::Verhoeff.convert(142857) #=> 1428570
|
79
|
+
#
|
80
|
+
# @param number [Integer] number without a checksum
|
81
|
+
#
|
82
|
+
# @return [Integer] final number including the checksum
|
83
|
+
def convert number
|
84
|
+
raise_on_type_mismatch number
|
85
|
+
(number * 10) + self.of(number)
|
49
86
|
end
|
87
|
+
alias_method :transform, :convert
|
88
|
+
|
50
89
|
end
|
51
90
|
end
|
data/lib/sjekksum/version.rb
CHANGED
data/lib/sjekksum.rb
CHANGED
@@ -1,55 +1,156 @@
|
|
1
1
|
require "sjekksum/version"
|
2
|
-
|
3
2
|
require "sjekksum/shared"
|
4
3
|
require "sjekksum/damm"
|
5
4
|
require "sjekksum/luhn"
|
6
5
|
require "sjekksum/verhoeff"
|
7
6
|
|
7
|
+
#
|
8
|
+
# Base module with convenient shortcuts
|
9
|
+
#
|
8
10
|
module Sjekksum
|
9
11
|
extend self
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
+
#
|
14
|
+
# Calculates Damm checksum
|
15
|
+
#
|
16
|
+
# @example
|
17
|
+
# Sjekksum.damm(572) #=> 4
|
18
|
+
#
|
19
|
+
# @see Sjekksum::Damm#of
|
20
|
+
#
|
21
|
+
# @param number [Integer] number for which the checksum should be calculated
|
22
|
+
#
|
23
|
+
# @return [Integer] calculated checksum
|
24
|
+
def damm_of number
|
25
|
+
Damm.of number
|
13
26
|
end
|
14
27
|
alias_method :damm, :damm_of
|
15
28
|
|
16
|
-
|
17
|
-
|
29
|
+
#
|
30
|
+
# Calculates Luhn checksum
|
31
|
+
#
|
32
|
+
# @example
|
33
|
+
# Sjekksum.luhn(7992739871) #=> 3
|
34
|
+
#
|
35
|
+
# @see Sjekksum::Luhn#of
|
36
|
+
#
|
37
|
+
# @param number [Integer] number for which the checksum should be calculated
|
38
|
+
#
|
39
|
+
# @return [Integer] calculated checksum
|
40
|
+
def luhn_of number
|
41
|
+
Luhn.of number
|
18
42
|
end
|
19
43
|
alias_method :luhn, :luhn_of
|
20
44
|
|
21
|
-
|
22
|
-
|
45
|
+
#
|
46
|
+
# Calculates Verhoeff checksum
|
47
|
+
#
|
48
|
+
# @example
|
49
|
+
# Sjekksum.verhoeff(142857) #=> 0
|
50
|
+
#
|
51
|
+
# @see Sjekksum::Verhoeff#of
|
52
|
+
#
|
53
|
+
# @param number [Integer] number for which the checksum should be calculated
|
54
|
+
#
|
55
|
+
# @return [Integer] calculated checksum
|
56
|
+
def verhoeff_of number
|
57
|
+
Verhoeff.of number
|
23
58
|
end
|
24
59
|
alias_method :verhoeff, :verhoeff_of
|
25
60
|
|
26
|
-
|
27
|
-
|
61
|
+
#
|
62
|
+
# Damm validation of provided number
|
63
|
+
#
|
64
|
+
# @example
|
65
|
+
# Sjekksum.damm?(5724) #=> true
|
66
|
+
#
|
67
|
+
# @see Sjekksum::Damm#valid?
|
68
|
+
#
|
69
|
+
# @param number [Integer] number with included checksum
|
70
|
+
#
|
71
|
+
# @return [Boolean]
|
72
|
+
def valid_damm? number
|
73
|
+
Damm.valid? number
|
28
74
|
end
|
29
75
|
alias_method :damm?, :valid_damm?
|
30
76
|
|
31
|
-
|
32
|
-
|
77
|
+
#
|
78
|
+
# Luhn validation of provided number
|
79
|
+
#
|
80
|
+
# @example
|
81
|
+
# Sjekksum.luhn?(79927398713) #=> true
|
82
|
+
#
|
83
|
+
# @see Sjekksum::Luhn#valid?
|
84
|
+
#
|
85
|
+
# @param number [Integer] number with included checksum
|
86
|
+
#
|
87
|
+
# @return [Boolean]
|
88
|
+
def valid_luhn? number
|
89
|
+
Luhn.valid? number
|
33
90
|
end
|
34
91
|
alias_method :luhn?, :valid_luhn?
|
35
92
|
|
36
|
-
|
37
|
-
|
93
|
+
#
|
94
|
+
# Verhoeff validation of provided number
|
95
|
+
#
|
96
|
+
# @example
|
97
|
+
# Sjekksum.verhoeff?(1428570) #=> true
|
98
|
+
#
|
99
|
+
# @see Sjekksum::Verhoeff#valid?
|
100
|
+
#
|
101
|
+
# @param number [Integer] number with included checksum
|
102
|
+
#
|
103
|
+
# @return [Boolean]
|
104
|
+
def valid_verhoeff? number
|
105
|
+
Verhoeff.valid? number
|
38
106
|
end
|
39
107
|
alias_method :verhoeff?, :valid_verhoeff?
|
40
108
|
|
41
|
-
|
42
|
-
|
109
|
+
#
|
110
|
+
# Transforms a number by appending the Damm checksum digit
|
111
|
+
#
|
112
|
+
# @example
|
113
|
+
# Sjekksum.damm!(572) #=> 5724
|
114
|
+
#
|
115
|
+
# @see Sjekksum::Damm#convert
|
116
|
+
#
|
117
|
+
# @param number [Integer] number without a checksum
|
118
|
+
#
|
119
|
+
# @return [Integer] final number including the checksum
|
120
|
+
def make_damm number
|
121
|
+
Damm.convert number
|
43
122
|
end
|
44
123
|
alias_method :damm!, :make_damm
|
45
124
|
|
46
|
-
|
47
|
-
|
125
|
+
#
|
126
|
+
# Transforms a number by appending the Luhn checksum digit
|
127
|
+
#
|
128
|
+
# @example
|
129
|
+
# Sjekksum.luhn!(7992739871) #=> 79927398713
|
130
|
+
#
|
131
|
+
# @see Sjekksum::Luhn#convert
|
132
|
+
#
|
133
|
+
# @param number [Integer] number without a checksum
|
134
|
+
#
|
135
|
+
# @return [Integer] final number including the checksum
|
136
|
+
def make_luhn number
|
137
|
+
Luhn.convert number
|
48
138
|
end
|
49
139
|
alias_method :luhn!, :make_luhn
|
50
140
|
|
51
|
-
|
52
|
-
|
141
|
+
#
|
142
|
+
# Transforms a number by appending the Verhoeff checksum digit
|
143
|
+
#
|
144
|
+
# @example
|
145
|
+
# Sjekksum.verhoeff!(142857) #=> 1428570
|
146
|
+
#
|
147
|
+
# @see Sjekksum::Verhoeff#convert
|
148
|
+
#
|
149
|
+
# @param number [Integer] number without a checksum
|
150
|
+
#
|
151
|
+
# @return [Integer] final number including the checksum
|
152
|
+
def make_verhoeff number
|
153
|
+
Verhoeff.convert number
|
53
154
|
end
|
54
155
|
alias_method :verhoeff!, :make_verhoeff
|
55
156
|
|