Class: Kount::SecurityMash

Inherits:
Object
  • Object
show all
Defined in:
lib/kount/security_mash.rb

Overview

This class implements the Kount KHASH for cards and gift cards. rubocop:disable Style/Documentation

Class Method Summary collapse

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

Parameters:

  • plain_text (String)

    String to be hashed

Returns:

  • (String)

    KHASH version of string



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.

Parameters:

  • plain_text (String)

    String to be hashed

Returns:

  • (String)

    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.

Parameters:

  • plain_text (String)

    String to be hashed

  • ptyp (String)

    Payment type code: CARD, GIFT, or OTHER

Returns:

  • (String)

    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.

Parameters:

  • val (String)

    Token that may or may not be khashed

Returns:

  • (Boolean)

    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

Parameters:

  • data (String)

    Data to hash

  • len (int)

    Length of hash to retain

Returns:

  • (String)

    Hashed data



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