Class: Kount::SecurityMash
- Inherits:
-
Object
- Object
- Kount::SecurityMash
- Defined in:
- lib/kount/security_mash.rb
Overview
This class implements the Kount KHASH for cards and gift cards.
Class Method Summary collapse
-
.hash_credit_card(plain_text, ksalt) ⇒ String
Hash a credit card number.
-
.hash_gift_card(plain_text, ksalt, merchant_id) ⇒ String
KHASH version of string.
-
.hash_token(plain_text, ptyp, ksalt, merchant_id = '') ⇒ String
KHASH version of string.
-
.khashed?(val) ⇒ Boolean
True if token is already khashed.
-
.mash(data, len, m) ⇒ String
Compute a base64 hash of the provided data.
Class Method Details
.hash_credit_card(plain_text, ksalt) ⇒ String
Hash a credit card number. Preserves first six characters of the input so that hashed cards can be categorized by Bank Identification Number (BIN).
Example usage:
hashed = Kount::SecurityMash.hash_credit_card("4111111111111111")
Expect: 411111WMS5YA6FUZA1KC
hashed = Kount::SecurityMash.hash_credit_card("5199185454061655")
Expect: 5199182NOQRXNKTTFL11
hashed = Kount::SecurityMash.hash_credit_card("4259344583883")
Expect: 425934FEXQI1QS6TH2O5
32 33 34 35 36 37 |
# File 'lib/kount/security_mash.rb', line 32 def self.hash_credit_card(plain_text, ksalt) return plain_text if khashed?(plain_text) first_six = plain_text[0..5] mashed = mash(plain_text, 14, ksalt) "#{first_six}#{mashed}" end |
.hash_gift_card(plain_text, ksalt, merchant_id) ⇒ String
Returns KHASH version of string.
49 50 51 52 |
# File 'lib/kount/security_mash.rb', line 49 def self.hash_gift_card(plain_text, ksalt, merchant_id) mashed = mash(plain_text, 14, ksalt) "#{merchant_id}#{mashed}" end |
.hash_token(plain_text, ptyp, ksalt, merchant_id = '') ⇒ String
Returns KHASH version of string.
10 11 12 13 14 15 16 |
# File 'lib/kount/security_mash.rb', line 10 def self.hash_token(plain_text, ptyp, ksalt, merchant_id = '') if ptyp == 'CARD' hash_credit_card(plain_text, ksalt) else hash_gift_card(plain_text, ksalt, merchant_id) end end |
.khashed?(val) ⇒ Boolean
Returns True if token is already khashed.
78 79 80 |
# File 'lib/kount/security_mash.rb', line 78 def self.khashed?(val) true if val =~ /(\d{6}[A-Z0-9]{14})/ end |
.mash(data, len, m) ⇒ String
Compute a base64 hash of the provided data.
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/kount/security_mash.rb', line 60 def self.mash(data, len, m) a = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' r = Digest::SHA1.hexdigest("#{data}.#{m}") c = '' len = 17 if len > 17 limit = 2 * len i = 0 while i < limit c << a[r[i..i + 6].to_i(16) % 36] i += 2 end c end |