Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * The `CardNumberGenerator` class generates random credit card numbers adhering to the Luhn algorithm.
- *
- * @property cartIdLength The length of the card ID portion in the generated card number.
- * @property bin The BIN (Bank Identification Number) value representing the card network.
- */
- class CardNumberGenerator {
- private var cartIdLength: Int = DEFAULT_CARD_ID_LENGTH
- private var bin: Int = DEFAULT_BIN
- // Constants and ranges for configuration
- companion object {
- private const val LUHN_CHECKSUM_LENGTH = 1
- private const val MIN_DIGIT = 0
- private const val MAX_DIGIT = 9
- private const val CARD_NUMBER_MIN_LENGTH = 13
- private const val CARD_NUMBER_MAX_LENGTH = 19
- private val MIR_BIN_RANGE = 220000..220499
- private val MASTERCARD_BIN_RANGE = 510000..559999
- private val VISA_BIN_RANGE = 450000..459999
- private val DEFAULT_BIN = Bin.entries.random().getValue()
- private val CARD_ID_MIN_LENGTH = CARD_NUMBER_MIN_LENGTH - DEFAULT_BIN.toString().length - LUHN_CHECKSUM_LENGTH
- private val CARD_ID_MAX_LENGTH = CARD_NUMBER_MIN_LENGTH - DEFAULT_BIN.toString().length - LUHN_CHECKSUM_LENGTH
- private val CARD_NUMBER_LENGTH_RANGE = CARD_NUMBER_MIN_LENGTH..CARD_NUMBER_MAX_LENGTH
- private val CARD_ID_LENGTH_RANGE = CARD_ID_MIN_LENGTH..CARD_ID_MAX_LENGTH
- private val DEFAULT_CARD_ID_LENGTH = CARD_ID_LENGTH_RANGE.random()
- private val DIGIT_RANGE = MIN_DIGIT..MAX_DIGIT
- }
- /**
- * Enum representing different card networks.
- */
- enum class Bin {
- MASTERCARD {
- override fun getValue(): Int {
- return MASTERCARD_BIN_RANGE.random()
- }
- },
- VISA {
- override fun getValue(): Int {
- return VISA_BIN_RANGE.random()
- }
- },
- MIR {
- override fun getValue(): Int {
- return MIR_BIN_RANGE.random()
- }
- };
- abstract fun getValue(): Int
- }
- /**
- * Sets the bin value for the card network.
- *
- * @param bin The card network represented by the `Bin` enum.
- * @return The current instance of `CardNumberGenerator`.
- */
- fun setBin(bin: Bin): CardNumberGenerator {
- this.bin = bin.getValue()
- return this
- }
- /**
- * Sets the custom length for the generated card number.
- *
- * @param customLength The desired length of the generated card number.
- * @return The current instance of `CardNumberGenerator`.
- * @throws IllegalArgumentException if the specified length is not in the valid range.
- */
- fun setLength(customLength: Int): CardNumberGenerator {
- if (customLength in CARD_NUMBER_LENGTH_RANGE) {
- this.cartIdLength = customLength - LUHN_CHECKSUM_LENGTH - bin.toString().length
- return this
- } else {
- throw IllegalArgumentException("Length is not in valid range: $CARD_NUMBER_LENGTH_RANGE")
- }
- }
- /**
- * Generates a random card number based on the configured parameters.
- *
- * @return The randomly generated card number.
- */
- fun generate(): String {
- val preparedNumber = bin.toString() + getRandomCardId()
- return preparedNumber + luhnCalculate(preparedNumber).toString()
- }
- // Internal utility functions for Luhn algorithm
- private fun luhnCheckSum(number: String): Int {
- var sum = 0
- val parity = number.length % 2
- for (i in number.length - 1 downTo 0) {
- var digit = Character.getNumericValue(number[i])
- if (i % 2 == parity) {
- digit *= 2
- }
- if (digit > 9) {
- digit -= 9
- }
- sum += digit
- }
- return sum % 10
- }
- private fun luhnCalculate(partNumber: String): Int {
- val checksum = luhnCheckSum(partNumber + '0')
- return if ((checksum % 10) == 0) 0 else 10 - (checksum % 10)
- }
- // Internal utility functions for generating random card ID
- private fun getRandomCardId(): String {
- var cardID = ""
- while (cardID.length < this.cartIdLength) {
- cardID += DIGIT_RANGE.random().toString()
- }
- return cardID
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement