1 /** 2 * com.sekati.crypt.Luhn 3 * @version 1.0.1 4 * @author jason m horwitz | sekati.com 5 * Copyright (C) 2007 jason m horwitz, Sekat LLC. All Rights Reserved. 6 * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php 7 */ 8 9 import com.sekati.crypt.IHash; 10 11 /** 12 * Validate a number with the Luhn Algorithm (aka Mod10) which is standard for pre-validating card numbers before 13 * being processed for approval. 14 * @see <a href="http://en.wikipedia.org/wiki/Luhn_algorithm">http://en.wikipedia.org/wiki/Luhn_algorithm</a> 15 */ 16 class com.sekati.crypt.Luhn implements IHash { 17 18 /** 19 * Validate a credit card number with mod10 20 * @param strNumber (String) 21 * @return Boolean 22 */ 23 public static function mod10(strNumber:String):Boolean { 24 25 // Seperate each number into it's own index in an array. 26 var aNumbers:Array = strNumber.split( "" ); 27 28 // Hold the sums of some calculations that will be made shortly. 29 var nSum_1:Number = 0; 30 var nSum_2:Number = 0; 31 var nSum_Total:Number = 0; 32 33 // Check to see if the length of the card number is odd or even. This will 34 // be used to determine which indicies are doubled before being summed up. 35 var nParity:Number = aNumbers.length % 2; 36 37 // Loop through the card numbers. 38 for(var i:Number = 0; i < aNumbers.length ; i++) { 39 // Type cast each digit to a number. 40 aNumbers[i] = Number( aNumbers[i] ); 41 42 // Compare the parity of the index to the parity of the card number length 43 // to determine how the value of the current index is handled. 44 if(i % 2 == nParity) { 45 // Double each number. 46 aNumbers[i] *= 2; 47 48 // If the resulting value is greater than '9', subtract '9' from it. 49 aNumbers[i] = aNumbers[i] > 9 ? aNumbers[i] - 9 : aNumbers[i]; 50 51 // Add each value together. 52 nSum_1 += aNumbers[i]; 53 } else { 54 // Add each value together. 55 nSum_2 += aNumbers[i]; 56 } 57 } 58 // Find the total sum of the two groups. 59 nSum_Total = nSum_1 + nSum_2; 60 61 // If the sum is divisible by '10', the card number is valid. 62 return (nSum_Total % 10 == 0); 63 } 64 65 private function Luhn() { 66 } 67 }