Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Ruby class for validation and generating numbers that satisfies luhn
Ruby
tree: 36378e7156

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib Simplified the checksum calculation a bit
spec Moved a test for better grouping
.gitignore Refactored into a gem, added general luhn calculation, verification a…
Gemfile Refactored into a gem, added general luhn calculation, verification a…
Gemfile.lock Fixed faster checksum calculation
LICENSE Refactored into a gem, added general luhn calculation, verification a…
README.rdoc Corrected gemspec and readme
Rakefile
luhn.gemspec Renamed gemspec

README.rdoc

Ruby class for handling basic Luhn number generation and verification

Includes a class to handle Swedish civic numbers (Personnummer) That interface supports checking validity (length, valid date and satisfies luhn), returning the sex, the control digit and generating random civic numbers.

Install

$ gem install luhn

Usage:

Basic Luhn

Luhn.valid?('0465827879483596') # true
Luhn.control_digit('046582787948359') # 6
Luhn.generate(n) # returns a random number of n length that satisfies luhn

'0465827879483596'.valid_luhn? # true
0465827879483596.valid_luhn?   # true

Swedish civic numbers

civic_number = Luhn::CivicNumber.new('19391030-3183')
civic_number.valid? # true
civic_number.sex    # 'male'
civic_number.male?  # true
civic_number.control_digit # 3

'391030-3183'.civic_number? # true
3910303183.civic_number?    # true

About the Luhn algorithm (from en.wikipedia.org/wiki/Luhn_formula)

The Luhn algorithm or Luhn formula, also known as the “modulus 10” or “mod 10” algorithm, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers in US and Canadian Social Insurance Numbers. It was created by IBM scientist Hans Peter Luhn and described in U.S. Patent No. 2,950,048, filed on January 6, 1954, and granted on August 23, 1960.

Copyright

Copyright © 2010 Joel Junström. See LICENSE for details.

Something went wrong with that request. Please try again.