Advertisement
fqrmix

Untitled

Jan 23rd, 2024 (edited)
1,282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.33 KB | None | 0 0
  1. /**
  2.  * The `CardNumberGenerator` class generates random credit card numbers adhering to the Luhn algorithm.
  3.  *
  4.  * @property cartIdLength The length of the card ID portion in the generated card number.
  5.  * @property bin The BIN (Bank Identification Number) value representing the card network.
  6.  */
  7. class CardNumberGenerator {
  8.  
  9.     private var cartIdLength: Int = DEFAULT_CARD_ID_LENGTH
  10.     private var bin: Int = DEFAULT_BIN
  11.  
  12.     // Constants and ranges for configuration
  13.     companion object {
  14.         private const val LUHN_CHECKSUM_LENGTH = 1
  15.         private const val MIN_DIGIT = 0
  16.         private const val MAX_DIGIT = 9
  17.         private const val CARD_NUMBER_MIN_LENGTH = 13
  18.         private const val CARD_NUMBER_MAX_LENGTH = 19
  19.  
  20.         private val MIR_BIN_RANGE = 220000..220499
  21.         private val MASTERCARD_BIN_RANGE = 510000..559999
  22.         private val VISA_BIN_RANGE = 450000..459999
  23.         private val DEFAULT_BIN = Bin.entries.random().getValue()
  24.  
  25.         private val CARD_ID_MIN_LENGTH = CARD_NUMBER_MIN_LENGTH - DEFAULT_BIN.toString().length - LUHN_CHECKSUM_LENGTH
  26.         private val CARD_ID_MAX_LENGTH = CARD_NUMBER_MIN_LENGTH - DEFAULT_BIN.toString().length - LUHN_CHECKSUM_LENGTH
  27.         private val CARD_NUMBER_LENGTH_RANGE = CARD_NUMBER_MIN_LENGTH..CARD_NUMBER_MAX_LENGTH
  28.         private val CARD_ID_LENGTH_RANGE = CARD_ID_MIN_LENGTH..CARD_ID_MAX_LENGTH
  29.         private val DEFAULT_CARD_ID_LENGTH = CARD_ID_LENGTH_RANGE.random()
  30.         private val DIGIT_RANGE = MIN_DIGIT..MAX_DIGIT
  31.     }
  32.  
  33.     /**
  34.      * Enum representing different card networks.
  35.      */
  36.     enum class Bin {
  37.         MASTERCARD {
  38.             override fun getValue(): Int {
  39.                 return MASTERCARD_BIN_RANGE.random()
  40.             }
  41.         },
  42.         VISA {
  43.             override fun getValue(): Int {
  44.                 return VISA_BIN_RANGE.random()
  45.             }
  46.         },
  47.         MIR {
  48.             override fun getValue(): Int {
  49.                 return MIR_BIN_RANGE.random()
  50.             }
  51.         };
  52.  
  53.         abstract fun getValue(): Int
  54.     }
  55.  
  56.     /**
  57.      * Sets the bin value for the card network.
  58.      *
  59.      * @param bin The card network represented by the `Bin` enum.
  60.      * @return The current instance of `CardNumberGenerator`.
  61.      */
  62.     fun setBin(bin: Bin): CardNumberGenerator {
  63.         this.bin = bin.getValue()
  64.         return this
  65.     }
  66.  
  67.     /**
  68.      * Sets the custom length for the generated card number.
  69.      *
  70.      * @param customLength The desired length of the generated card number.
  71.      * @return The current instance of `CardNumberGenerator`.
  72.      * @throws IllegalArgumentException if the specified length is not in the valid range.
  73.      */
  74.     fun setLength(customLength: Int): CardNumberGenerator {
  75.         if (customLength in CARD_NUMBER_LENGTH_RANGE) {
  76.             this.cartIdLength = customLength - LUHN_CHECKSUM_LENGTH - bin.toString().length
  77.             return this
  78.         } else {
  79.             throw IllegalArgumentException("Length is not in valid range: $CARD_NUMBER_LENGTH_RANGE")
  80.         }
  81.     }
  82.  
  83.     /**
  84.      * Generates a random card number based on the configured parameters.
  85.      *
  86.      * @return The randomly generated card number.
  87.      */
  88.     fun generate(): String {
  89.         val preparedNumber = bin.toString() + getRandomCardId()
  90.         return preparedNumber + luhnCalculate(preparedNumber).toString()
  91.     }
  92.    
  93.     // Internal utility functions for Luhn algorithm
  94.     private fun luhnCheckSum(number: String): Int {
  95.         var sum = 0
  96.         val parity = number.length % 2
  97.         for (i in number.length - 1 downTo 0) {
  98.             var digit = Character.getNumericValue(number[i])
  99.  
  100.             if (i % 2 == parity) {
  101.                 digit *= 2
  102.             }
  103.             if (digit > 9) {
  104.                 digit -= 9
  105.             }
  106.             sum += digit
  107.         }
  108.         return sum % 10
  109.     }
  110.  
  111.     private fun luhnCalculate(partNumber: String): Int {
  112.         val checksum = luhnCheckSum(partNumber + '0')
  113.         return if ((checksum % 10) == 0) 0 else 10 - (checksum % 10)
  114.     }
  115.  
  116.     // Internal utility functions for generating random card ID
  117.     private fun getRandomCardId(): String {
  118.         var cardID = ""
  119.         while (cardID.length < this.cartIdLength) {
  120.             cardID += DIGIT_RANGE.random().toString()
  121.         }
  122.         return cardID
  123.     }
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement