ruby-luhn 0.0.2
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.
- checksums.yaml +7 -0
- data/lib/luhn.rb +55 -0
- metadata +42 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bb4048a369aa49edb7417eae1b80615864a7749bd64ed0101b3da06fa3397644
|
4
|
+
data.tar.gz: 005da7adc844ce4e466b80c3df64e977ca1f818bdded4c1d22e9b8b7d0c76dfa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1c2d4edf5a9acd49393980c627d8b00487fb3eb3a7c7ab3908a4eb548fb8ea8c9cb1a5908fbf984e6491b7470bf11ca22d2c8755d1b5d0e01c3a4a4edb7c4fd5
|
7
|
+
data.tar.gz: c2111f39f371c10204ad0e243a97003923a6e6e5cd063e0bf61c871a707fa387468967e245c3e8bbee5774de1789181465ac679057ed4eb316db0f4ec18dfdc4
|
data/lib/luhn.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Implements the Luhn algorithm mod n to generate checksums
|
4
|
+
class Luhn
|
5
|
+
class << self
|
6
|
+
# Calculates the checksum of the given number
|
7
|
+
#
|
8
|
+
# @param number [String] the number to calculate the checksum
|
9
|
+
# @param base [Integer] the base used to calculate the checksum
|
10
|
+
# @return [String] the checksum of the given number
|
11
|
+
# @raise [ArgumentError] if the number is not valid in the chosen base
|
12
|
+
def generate(number, base: 10)
|
13
|
+
total = 0
|
14
|
+
|
15
|
+
characters = number.split('')
|
16
|
+
characters.reverse.each_with_index do |character, index|
|
17
|
+
value = parse(character, base)
|
18
|
+
|
19
|
+
# Duplicates only digits on an even position
|
20
|
+
value *= 2 if index.even?
|
21
|
+
|
22
|
+
# Sums digits of number
|
23
|
+
# e.g: if the digit was Z => Z * 2 = 1Y
|
24
|
+
# and we sum 1 + Y = 1 + 34 = 35
|
25
|
+
total += value / base # Sums first digit
|
26
|
+
total += value % base # Sums second digit
|
27
|
+
end
|
28
|
+
|
29
|
+
# Return the value we'd have to sum to the total to have a number divisible by 36
|
30
|
+
(-total % base).to_s(base).upcase
|
31
|
+
end
|
32
|
+
|
33
|
+
# Checks if the last digit of the number is the checksum of the rest
|
34
|
+
#
|
35
|
+
# @param number [String] the number to be checked
|
36
|
+
# @param base [Integer] the base used to calculate the checksum
|
37
|
+
# @return [Boolean] the validity of the checksum
|
38
|
+
# @raise [ArgumentError] if the number is not valid in the chosen base
|
39
|
+
def validate(number, base: 10)
|
40
|
+
number[-1] == generate(number[0...-1], base: base)
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def parse(digit, base)
|
46
|
+
number = digit.to_i(base)
|
47
|
+
|
48
|
+
if number.zero? && digit != '0'
|
49
|
+
raise ArgumentError, "The number is not a valid base #{base} number"
|
50
|
+
end
|
51
|
+
|
52
|
+
number
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
metadata
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-luhn
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jorge Leites
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2011-09-29 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/luhn.rb
|
20
|
+
homepage:
|
21
|
+
licenses: []
|
22
|
+
metadata: {}
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
requirements:
|
29
|
+
- - ">="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: '2.4'
|
32
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
requirements: []
|
38
|
+
rubygems_version: 3.0.3
|
39
|
+
signing_key:
|
40
|
+
specification_version: 4
|
41
|
+
summary: A lightweight implementation of the Luhn algorithm
|
42
|
+
test_files: []
|