Module: Cdigits::Luhn

Defined in:
lib/cdigits/luhn.rb,
lib/cdigits/luhn/store.rb,
lib/cdigits/luhn/placeholder.rb,
lib/cdigits/luhn/random_table.rb

Overview

Implementation of Luhn mod N algorithm

Defined Under Namespace

Classes: Placeholder, RandomTable, Store

Constant Summary collapse

NUMBER_CHARACTERS =

0 to 9 string array

(0..9).map(&:to_s).freeze
HEX_CHARACTERS =

0 to 9 and a to f string array

(NUMBER_CHARACTERS + ('a'..'f').to_a).freeze
ALPHANUMERIC_CHARACTERS =

0 to 9 and a to z string array

(NUMBER_CHARACTERS + ('a'..'z').to_a).freeze
EASY_CHARACTERS =
Note:

any good idea?

Note:

Misread charcters

  • 0 and O(óu)

  • 0 and D(díː)

  • 0 and Q(kjúː)

  • 1 and I(ái)

  • 2 and Z(zíː)

Note:

Misheard charcters

  • D(díː) and B(bíː)

  • M(ém) and N(én)

  • 9(kyu:) and Q(kjúː) … Japanese only

Non(hard to)-Misread/Misheard characters

Returns:

  • (Array<String>)
(NUMBER_CHARACTERS + ('A'..'Z').to_a - %w[D I M O Q Z]).freeze

Class Method Summary collapse

Class Method Details

.alphanumeric(placeholder = nil) ⇒ String

Generate code with Luhn mod 36 algorithm

Examples:

Cdigits::Luhn.alphanumeric # => 'a0gpmk4ye4'
Cdigits::Luhn.alphanumeric('2###-0###-0###-1##?') # => '22u3-04s1f-0z9c-1lmo'

Returns:

  • (String)


59
60
61
# File 'lib/cdigits/luhn.rb', line 59

def alphanumeric(placeholder = nil)
  generate(placeholder, ALPHANUMERIC_CHARACTERS)
end

.alphanumeric?(code) ⇒ Boolean

Validate code with Luhn mod 36 algorithm

Parameters:

  • code (String)

Returns:

  • (Boolean)


66
67
68
# File 'lib/cdigits/luhn.rb', line 66

def alphanumeric?(code)
  valid?(code, ALPHANUMERIC_CHARACTERS)
end

.easy(placeholder = nil) ⇒ String

Generate code with Luhn mod 30 algorithm Valid characters are 0 to 9 and A to Z without D/I/M/O/Q/Z

Examples:

Cdigits::Luhn.easy # => '5F20603XER'
Cdigits::Luhn.easy('2###-0###-0###-1##?') # => '2P2M-0191-05XL-1BYN'

Returns:

  • (String)


92
93
94
# File 'lib/cdigits/luhn.rb', line 92

def easy(placeholder = nil)
  generate(placeholder, EASY_CHARACTERS)
end

.easy?(code) ⇒ Boolean

Validate code with Luhn mod 30 algorithm

Parameters:

  • code (String)

Returns:

  • (Boolean)


99
100
101
# File 'lib/cdigits/luhn.rb', line 99

def easy?(code)
  valid?(code, EASY_CHARACTERS)
end

.generate(placeholder, characters) ⇒ String

Generate code

Parameters:

  • placeholder (String)
  • characters (Array<String>)

Returns:

  • (String)


107
108
109
110
# File 'lib/cdigits/luhn.rb', line 107

def generate(placeholder, characters)
  placeholder ||= '+########?'
  instance(characters).fill(placeholder)
end

.hex(placeholder = nil) ⇒ String

Generate code with Luhn mod 16 algorithm

Examples:

Cdigits::Luhn.hex # => 'd6fd358a29'
Cdigits::Luhn.hex('2###-0###-0###-1##?') # => '2582-08fe-02fe-1d80'

Returns:

  • (String)


39
40
41
# File 'lib/cdigits/luhn.rb', line 39

def hex(placeholder = nil)
  generate(placeholder, HEX_CHARACTERS)
end

.hex?(code) ⇒ Boolean

Validate code with Luhn mod 16 algorithm

Parameters:

  • code (String)

Returns:

  • (Boolean)


46
47
48
# File 'lib/cdigits/luhn.rb', line 46

def hex?(code)
  valid?(code, HEX_CHARACTERS)
end

.instance(characters) ⇒ Cdigits::Luhn::Placeholder



121
122
123
# File 'lib/cdigits/luhn.rb', line 121

def instance(characters)
  ::Cdigits::Luhn::Placeholder.new(characters)
end

.number(placeholder = nil) ⇒ String

Generate code with Luhn mod 10 algorithm

Examples:

Cdigits::Luhn.number # => '123456782'
Cdigits::Luhn.number('2###-0###-0###-1##?') # => 2960-0093-0751-1449

Returns:

  • (String)


19
20
21
# File 'lib/cdigits/luhn.rb', line 19

def number(placeholder = nil)
  generate(placeholder, NUMBER_CHARACTERS)
end

.number?(code) ⇒ Boolean

Validate code with Luhn mod 10 algorithm

Parameters:

  • code (String)

Returns:

  • (Boolean)


26
27
28
# File 'lib/cdigits/luhn.rb', line 26

def number?(code)
  valid?(code, NUMBER_CHARACTERS)
end

.valid?(code, characters) ⇒ Boolean

Parameters:

  • code (String)
  • characters (Array<String>)

Returns:

  • (Boolean)


115
116
117
# File 'lib/cdigits/luhn.rb', line 115

def valid?(code, characters)
  instance(characters).valid?(code)
end