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
021/** Default {@link LUHNCalculator} that computes/validates the LUHN check digit. */
022public class DefaultLUHNCalculator implements LUHNCalculator {
023    /** Default constructor; no instance state to initialise. */
024    public DefaultLUHNCalculator() {}
025    /**
026     * Compute card's check digit (LUHN)
027     * @param p PAN (without checkdigit)
028     * @return the checkdigit
029     */
030    public char calculate (String p)
031        throws InvalidCardException
032    {
033        int i, crc;
034        int odd = p.length() % 2;
035
036        for (i=crc=0; i<p.length(); i++) {
037            char c = p.charAt(i);
038            if (!Character.isDigit (c)) {
039                throw new IllegalArgumentException("Invalid PAN " + p);
040            }
041            c = (char) (c - '0');
042            if (i % 2 != odd)
043                crc+= c*2 >= 10 ? c*2 -9 : c*2;
044            else
045                crc+=c;
046        }
047
048        return (char) ((crc % 10 == 0 ? 0 : 10 - crc % 10) + '0');
049    }
050
051    /**
052     * Verify Card's PAN
053     * @param p full card PAN
054     * @return true if pan LUHN's matches
055     */
056    public boolean verify (String p) throws InvalidCardException {
057        if (p == null || p.length() < 5)
058            throw new InvalidCardException ("Invalid PAN " + p);
059
060        return p.charAt(p.length()-1) == calculate (p.substring(0, p.length()-1));
061    }
062}