Sjekksum

Gem Version Build Status Support via Gratipay

A gem to provide some checksum algorithms like Damm, ISBN-10/13, Luhn, UPC and Verhoeff.


All used algorithms for generating and validating checksums must calculate a single check digit only.

More extensive checksum algorithms like Adler-32, CRC, BSD checksum, SYSV checksum, all hash algorithms and friends are not part of this project.

Furthermore this gem does not allow any other input type than integers or strings of integer digits to simplify transformations here (so all extended algorithms allowing characters are not included as well).

For the string inputs all non-integer bytes are thrown away:

"01-234.567 89"
#=>
"0123456789" # cleaned string
#=>
[0,1,2,3,4,5,6,7,8,9] # internal digits representation

Shortly: This gem tries to follow the UNIX philosophy in parts: »Write programs that do one thing and do it well.«

Installation

Add this line to your application's Gemfile:

gem "sjekksum"

And then execute:

bundle

Or install it yourself as:

gem install sjekksum

Usage

require "sjekksum"

Sjekksum.damm(572)                     #=> 4
Sjekksum.damm!(572)                    #=> 5724
Sjekksum.damm?(5724)                   #=> true

Sjekksum.isbn10("147743025")           #=> 3
Sjekksum.isbn10!("147743025")          #=> "1477430253"
Sjekksum.isbn10?("1477430253")         #=> true

Sjekksum.isbn13("978-0-306-40615-")    #=> 7
Sjekksum.isbn13!("978-0-306-40615-")   #=> "978-0-306-40615-7"
Sjekksum.isbn13?("978-0-306-40615-7")  #=> true

Sjekksum.luhn(7992739871)              #=> 3
Sjekksum.luhn!(7992739871)             #=> 79927398713
Sjekksum.luhn?(79927398713)            #=> true

Sjekksum.upc("03600024145")            #=> 7
Sjekksum.upc!("03600024145")           #=> "036000241457"
Sjekksum.upc?("036000241457")          #=> true

Sjekksum.verhoeff(142857)              #=> 0
Sjekksum.verhoeff!(142857)             #=> 1428570
Sjekksum.verhoeff?(1428570)            #=> true

Sjekksum.primitive(232323)             #=> 6
Sjekksum.primitive!(232323)            #=> 2323236
Sjekksum.primitive?(2323236)           #=> true

Sjekksum.primitive97(23569)            #=> 5
Sjekksum.primitive97!(23569)           #=> 235695
Sjekksum.primitive97?(235695)          #=> true

Documentation

... can be found at http://rdoc.info/github/asaaki/sjekksum/master/frames

License

MIT/X11

Copyright (c) 2013—2014 Christoph Grabo