001/* 002 * jPOS Project [http://jpos.org] 003 * Copyright (C) 2000-2026 jPOS Software SRL 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018 019package org.jpos.core; 020 021public class DefaultLUHNCalculator implements LUHNCalculator { 022 /** 023 * Compute card's check digit (LUHN) 024 * @param p PAN (without checkdigit) 025 * @return the checkdigit 026 */ 027 public char calculate (String p) 028 throws InvalidCardException 029 { 030 int i, crc; 031 int odd = p.length() % 2; 032 033 for (i=crc=0; i<p.length(); i++) { 034 char c = p.charAt(i); 035 if (!Character.isDigit (c)) { 036 throw new IllegalArgumentException("Invalid PAN " + p); 037 } 038 c = (char) (c - '0'); 039 if (i % 2 != odd) 040 crc+= c*2 >= 10 ? c*2 -9 : c*2; 041 else 042 crc+=c; 043 } 044 045 return (char) ((crc % 10 == 0 ? 0 : 10 - crc % 10) + '0'); 046 } 047 048 /** 049 * Verify Card's PAN 050 * @param p full card PAN 051 * @return true if pan LUHN's matches 052 */ 053 public boolean verify (String p) throws InvalidCardException { 054 if (p == null || p.length() < 5) 055 throw new InvalidCardException ("Invalid PAN " + p); 056 057 return p.charAt(p.length()-1) == calculate (p.substring(0, p.length()-1)); 058 } 059}