card_redactor 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 56468a4a284a7403df9e10a2b90c175aa909156d
4
+ data.tar.gz: c579b8286fdc3dc0150d61ba986a51fd1dde9176
5
+ SHA512:
6
+ metadata.gz: 889cf86f76eee8ea4f3547fb61414ab1a41320a5ffcb063ce93cf85cf00c0719029de1c9b8dc5aac21870491b63f9e46db2deeb7b5c4691a59dcb52c8c7d5008
7
+ data.tar.gz: be1f05511637842980c335985dd0949208d4d1cc66570327619f41531fcd8cbcbc5c0559f36de75a75255f9cfe1843357c190d18a754437ddf54cfa323ba3eae
data/.editorconfig ADDED
@@ -0,0 +1,10 @@
1
+ # editorconfig.org
2
+ root = true
3
+
4
+ [*]
5
+ indent_style = space
6
+ indent_size = 2
7
+ end_of_line = lf
8
+ charset = utf-8
9
+ trim_trailing_whitespace = true
10
+ insert_final_newline = true
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in card_redactor.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # CardRedactor
2
+
3
+ A gem for detecting sensitive [credit card PANs](https://en.wikipedia.org/wiki/Payment_card_number) in strings, and redacting them. All digits, except the trailing 4, are replaced with X. The original format of the number is kept intact. Supports Visa, Mastercard, AMEX, and Discover. Useful for cases where user input may inadvertently contain credit card numbers, and you want to ensure they aren't stored.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'card_redactor', '~> 1.0.0'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install card_redactor
20
+
21
+ ## Usage
22
+
23
+ Firstly, `CardRedactor` can be used to detect the presence of a credit card PAN in a string:
24
+
25
+ ```ruby
26
+ >> card = "4111-1111-1111-1111"
27
+ => "4111-1111-1111-1111"
28
+ >> CardRedactor.contains_card?(card)
29
+ => true
30
+ ```
31
+
32
+ Secondly, you can redact those PANs, replacing all but the last 4 digits with Xs:
33
+
34
+ ```ruby
35
+ >> CardRedactor.redact(card)
36
+ => "XXXX-XXXX-XXXX-1111"
37
+ ```
38
+
39
+ It'll work with natural language sentences, and leave numbers that aren't credit cards alone:
40
+
41
+ ```ruby
42
+ >> card = "A more complex example, with an NZ bank account 12-1212-343434-01 and a credit card 4111111111111111, wow!"
43
+ => "A more complex example, with an NZ bank account 12-1212-343434-01 and a credit card 4111111111111111, wow!"
44
+ >> CardRedactor.contains_card?(card)
45
+ => true
46
+ >> CardRedactor.redact(card)
47
+ => "A more complex example, with an NZ bank account 12-1212-343434-01 and a credit card XXXXXXXXXXXX1111, wow!"
48
+ ```
49
+
50
+ and strings that contain more than one PAN:
51
+
52
+ ```ruby
53
+ >> card = "I've got 4111111111111111 and 3759-876513-21001"
54
+ => "I've got 4111111111111111 and 3759-876513-21001"
55
+ >> CardRedactor.redact(card)
56
+ => "I've got XXXXXXXXXXXX1111 and XXXX-XXXXXX-X1001"
57
+ ```
58
+
59
+ ## Development
60
+
61
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
62
+
63
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
64
+
65
+ ## Contributing
66
+
67
+ Bug reports and pull requests are welcome on GitHub at https://github.com/pocketsmith/card-redactor.
68
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "card_redactor"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "card_redactor"
7
+ spec.version = "1.0.0"
8
+ spec.authors = ["Regan McEntyre"]
9
+ spec.email = ["regan@pocketsmith.com"]
10
+
11
+ spec.summary = "Credit card redactor"
12
+ spec.description = "A library to detect and redact credit card numbers (PANs)"
13
+ spec.homepage = "https://www.pocketsmith.com"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "activesupport", ">= 2.3"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.13"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
@@ -0,0 +1,56 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext'
3
+
4
+ class CardRedactor
5
+
6
+ # Matches Visa, Mastercard, AMEX, Discover
7
+ # Read more: http://www.richardsramblings.com/regex/credit-card-numbers/
8
+ CARD_PATTERN = /\b(?:3[47]\d{2}([\ \-+]?)\d{6}\1\d|(?:(?:4\d|5[1-5]|65)\d{2}|6011)([\ \-+]?)\d{4}\2\d{4}\2)\d{4}\b/
9
+
10
+ class << self
11
+
12
+ def contains_card?(string)
13
+ card_matches(string).any?
14
+ end
15
+
16
+ def card_matches(string)
17
+ # Scan to get all cards in the string, but get the Matchdata instead
18
+ # Read more: http://stackoverflow.com/a/13817639/881691
19
+ [].tap { |matches| string.scan(CARD_PATTERN) { matches << $~ } }
20
+ end
21
+
22
+ def redact(string)
23
+ matches = card_matches(string)
24
+
25
+ return string if matches.none?
26
+
27
+ matches.each do |match|
28
+ card = match[0]
29
+
30
+ parts = []
31
+
32
+ # In Ruby 1.9, there's no reliable way to deal with chars in strings
33
+ # individually apart from splitting them into an array. In 2.x, we can
34
+ # use array access notation to get at individual characters in a byte-safe manner.
35
+ card.split("").reverse.each_with_index do |char, index|
36
+ if index < 4 || char !~ /\d/
37
+ parts << char
38
+ else
39
+ # If we've gone past the last 4 digits, redact numbers
40
+ parts << "X"
41
+ end
42
+ end
43
+
44
+ # Turn the numbers around the right way and implode them into a string
45
+ redacted_card = parts.reverse.join("")
46
+
47
+ # Replace the plaintext card with the redacted card in the original string
48
+ string = string.sub(card, redacted_card)
49
+ end
50
+
51
+ string
52
+ end
53
+
54
+ end
55
+
56
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: card_redactor
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Regan McEntyre
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '2.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: A library to detect and redact credit card numbers (PANs)
70
+ email:
71
+ - regan@pocketsmith.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".editorconfig"
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - card_redactor.gemspec
86
+ - lib/card_redactor.rb
87
+ homepage: https://www.pocketsmith.com
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.5.1
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Credit card redactor
111
+ test_files: []