// Function: luhn_check // Description: Runs Luhn's algorithm on a given number. // Uses Functions: get_last_digit // Parameters: Number being tested. // Pre-Conditions: Positive integer input. // Post-Conditions: Bool output. // Return: false - passes algorithm, true - does not pass algorithm. public static bool luhn_check(ulong num) { int sum = 0; int checkDigit = CardFunctions.get_last_digit(num); int[] digits = num.ToString().Select(Convert.ToInt32).ToArray(); // Convert num into array of digits. int everyOtherNumber = 0; // For tracking every other number. for (int i = digits.Length - 2; i >= 0; i--) // Run through digits in reverse order, skipping the ending digit. { digits[i] -= 48; // Convert char value to digit value. if (everyOtherNumber % 2 == 0) { digits[i] *= 2; if (digits[i] > 9) { digits[i] -= 9; } } sum += digits[i]; everyOtherNumber++; } digits[digits.Length - 1] -= 48; // Convert check digit char value to digit value. // Number does not pass the test if the calculated check digit does not match the original last digit. if ((sum * 9) % 10 != checkDigit) { return(true); } else { return(false); } }
// Function: get_corrected_number // Description: Runs Luhn's algorithm on a given number, and then returns the number with the proper check digit. // Uses Functions: get_last_digit // Parameters: Number being corrected. // Pre-Conditions: Positive integer input. // Post-Conditions: Number passes Luhn's algorithm. // Return: public static ulong get_corrected_number(ulong num) { int sum = 0; int checkDigit = CardFunctions.get_last_digit(num); int[] digits = num.ToString().Select(Convert.ToInt32).ToArray(); // Convert num into array of digits. int everyOtherNumber = 0; // For tracking every other number. for (int i = digits.Length - 2; i >= 0; i--) // Run through digits in reverse order, skipping the ending digit. { digits[i] -= 48; // Convert char value to digit value. if (everyOtherNumber % 2 == 0) { digits[i] *= 2; if (digits[i] > 9) { digits[i] -= 9; } } sum += digits[i]; everyOtherNumber++; } return(num - Convert.ToUInt32(checkDigit) + Convert.ToUInt32(sum * 9) % 10); // Replace the last digit with the correct checkDigit. }
// Event function: textEntryBox_TextChanged // Activates when the text entry box text changes. private void textEntryBox_TextChanged(object sender, EventArgs e) { ulong numberEntry = 0; string displayMessage = "Enter a card number!"; bool exceptionThrown = false; correctedNumber.Text = ""; try { numberEntry = Convert.ToUInt64(textEntryBox.Text); } catch (System.FormatException) { exceptionThrown = true; } if (exceptionThrown) // If user did not enter a proper number. { pictureBox.Image = Properties.Resources.failure; displayMessage = "Not a valid card number!"; } else if (!CardFunctions.is_correct_length(numberEntry)) // If card number is not correct length. { pictureBox.Image = Properties.Resources.failure; displayMessage = "Card number is not the correct length!"; } else if (CardFunctions.luhn_check(numberEntry)) // If card number does not pass luhn's algorithm. { pictureBox.Image = Properties.Resources.failure; displayMessage = "Card number does not pass Luhn's Algorithm!"; correctedNumber.Text = "*Corrected number: " + CardFunctions.get_corrected_number(numberEntry); } else // Card number is valid. { pictureBox.Image = Properties.Resources.success; displayMessage = "This is a valid card number!"; } titleLabel.Text = displayMessage; }