iso7812 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in iso7812.gemspec
4
+ gemspec
5
+
6
+ gem "rake", :group => :test
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Eduardo Mourao
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ # Ruby ISO-7812 Implementation
2
+
3
+ This is the full ISO-7812 implementation, in ruby! This is battle tested, production ready. Use at will.
4
+
5
+ I also included the specification itself in the 'doc' folder (PDF). I paid for it, but you shouldn't. Information should be free.
6
+
7
+ The project is 100% documented and you shouldn't have any problem understanding the standard just by reading the comments.
8
+
9
+ I recommend that you copy those files into your own project and not use this as a gem.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'iso7812'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install iso7812
24
+
25
+ ## Usage
26
+
27
+ require 'iso7812'
28
+
29
+ include Iso7812::CardNumber
30
+
31
+ # See available methods on card_number.rb
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'iso7812/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+
8
+ gem.add_development_dependency "rspec"
9
+
10
+ gem.name = "iso7812"
11
+ gem.version = Iso7812::VERSION
12
+ gem.authors = ["Eduardo Mourao"]
13
+ gem.email = ["eduardo.a20@gmail.com"]
14
+ gem.description = %q{Full implementation of the ISO-7812 standard}
15
+ gem.summary = %q{Full implementation of the ISO-7812 standard}
16
+ gem.homepage = ""
17
+
18
+ gem.files = `git ls-files`.split($/)
19
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
20
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
21
+ gem.require_paths = ["lib"]
22
+ end
@@ -0,0 +1,6 @@
1
+ require "iso7812/version"
2
+ require "iso7812/card_number"
3
+
4
+ module Iso7812
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,96 @@
1
+
2
+ ##
3
+ #
4
+ # The ISO/IEC 7812 specifies a numbering system for the identification of issuers of cards
5
+ # that require an issuer identification number (IIN) to operate in international,
6
+ # interindustry and/or intra-industry interchange.
7
+ #
8
+ module Iso7812
9
+
10
+ module CardNumber
11
+ ##
12
+ # The Structure of a card
13
+ #
14
+ # POS | DESCRIPTION
15
+ # --------------------
16
+ # 0 | MII
17
+ # 0..5 | IIN
18
+ # 6..n | IAI (max length = 12)
19
+ # n+1 | Check digit
20
+ #
21
+
22
+ # Generates a valid card number based on the parameters given
23
+ # IIN - Issuer Identification Number
24
+ # IAI - Individual Account Identificaation
25
+ def generate(iin, iai)
26
+ p = "#{iin}#{iai}"
27
+ "#{p}#{checkdigit(p).to_s}"
28
+ end
29
+
30
+ # Returns true if card number is valid
31
+ # PAN - Primary Account Number (card number)
32
+ #
33
+ # This code is written to convey clarity.
34
+ def valid?(pan)
35
+ # must be numbers
36
+ return false unless pan =~ /^[0-9]+$/
37
+
38
+ # 8 is the minimum size
39
+ return false unless pan.length > 7
40
+
41
+ # 19 is maximum size
42
+ return false unless pan.length < 20
43
+
44
+ # check digit
45
+ return pan[pan.length-1] == checkdigit(pan.chop).to_s
46
+ end
47
+
48
+ # Returns the IIN of this PAN
49
+ # IIN - Issuer Identification Number
50
+ # PAN - Primary Account Number (card number)
51
+ def iin(pan)
52
+ pan[0..5]
53
+ end
54
+
55
+ # Returns the MII of this PAN or IIN
56
+ # IIN - Issuer Identification Number
57
+ # PAN - Primary Account Number (card number)
58
+ # MII - Major Industry Identifier
59
+ #
60
+ # There are ten single-digit MIIs:
61
+ # 0 for assignment by ISO/TC 68 and for other future industry assignments
62
+ # 1 airlines
63
+ # 2 airlines and other future industry assignments
64
+ # 3 travel and entertainment and banking/financial
65
+ # 4 banking/financial
66
+ # 5 banking/financial
67
+ # 6 merchandising and banking/financial
68
+ # 7 petroleum and other future industry assignments
69
+ # 8 healthcare, telecommunications and other future industry assignments
70
+ # 9 for assignment by national standards bodies
71
+ # New industry assignments shall be approved by the RMG.
72
+ # The MII does not in any way reflect or limit the application in which the card is usable.
73
+ # Single-digit MIIs are assigned using the applicant’s description of their main area of business
74
+ # on the application form (see Annex A of ISO/IEC 7812-2:2000).
75
+ def mii(pan_or_iin)
76
+ pan_or_iin[0]
77
+ end
78
+
79
+ # The individual account identification shall be followed by a check digit.
80
+ # This digit shall be calculated on all the preceding digits of the PAN
81
+ # and shall be computed according to the Luhn formula for modulus-10 check digit.
82
+ def checkdigit(number)
83
+ digits = number.to_s.reverse.scan(/\d/).map { |x| x.to_i }
84
+ digits = digits.each_with_index.map { |d, i|
85
+ d *= 2 if i.even?
86
+ d > 9 ? d - 9 : d
87
+ }
88
+ sum = digits.inject(0) { |m, x| m + x }
89
+ mod = 10 - sum % 10
90
+ mod==10 ? 0 : mod
91
+ end
92
+
93
+
94
+ end
95
+
96
+ end
@@ -0,0 +1,3 @@
1
+ module Iso7812
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ include Iso7812::CardNumber
4
+
5
+ describe "CardNumber" do
6
+
7
+ it "should correctly validates a card" do
8
+ valid?("378761728695330").should eq true
9
+ valid?("4485006611888499").should eq true
10
+ valid?("5351196743543506").should eq true
11
+ valid?("6011925914749389").should eq true
12
+
13
+ valid?("378761728695331").should eq false
14
+ valid?("4485006611888491").should eq false
15
+ valid?("5351196743543501").should eq false
16
+ valid?("6011925914749381").should eq false
17
+
18
+ valid?("6011").should eq false
19
+ end
20
+
21
+ it "should correctly generate a card" do
22
+ generate("448500", "661188849").should eq("4485006611888499")
23
+ end
24
+
25
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'iso7812'
5
+
6
+ RSpec.configure do |config|
7
+
8
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iso7812
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Eduardo Mourao
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Full implementation of the ISO-7812 standard
31
+ email:
32
+ - eduardo.a20@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - .rspec
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - iso7812.gemspec
44
+ - lib/iso7812.rb
45
+ - lib/iso7812/card_number.rb
46
+ - lib/iso7812/version.rb
47
+ - spec/card_number_spec.rb
48
+ - spec/spec_helper.rb
49
+ homepage: ''
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ segments:
62
+ - 0
63
+ hash: -2394687770318694498
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ segments:
71
+ - 0
72
+ hash: -2394687770318694498
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.24
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Full implementation of the ISO-7812 standard
79
+ test_files:
80
+ - spec/card_number_spec.rb
81
+ - spec/spec_helper.rb