001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.validator.routines.checkdigit; 018 019import java.io.Serializable; 020 021/** 022 * <b>Verhoeff</b> (Dihedral) Check Digit calculation/validation. 023 * <p> 024 * Check digit calculation for numeric codes using a 025 * <a href="http://en.wikipedia.org/wiki/Dihedral_group">Dihedral Group</a> 026 * of order 10. 027 * <p> 028 * See <a href="http://en.wikipedia.org/wiki/Verhoeff_algorithm">Wikipedia 029 * - Verhoeff algorithm</a> for more details. 030 * 031 * @since 1.4 032 */ 033public final class VerhoeffCheckDigit implements CheckDigit, Serializable { 034 035 private static final long serialVersionUID = 4138993995483695178L; 036 037 /** Singleton Verhoeff Check Digit instance */ 038 public static final CheckDigit VERHOEFF_CHECK_DIGIT = new VerhoeffCheckDigit(); 039 040 /** D - multiplication table */ 041 private static final int[][] D_TABLE = { 042 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 043 {1, 2, 3, 4, 0, 6, 7, 8, 9, 5}, 044 {2, 3, 4, 0, 1, 7, 8, 9, 5, 6}, 045 {3, 4, 0, 1, 2, 8, 9, 5, 6, 7}, 046 {4, 0, 1, 2, 3, 9, 5, 6, 7, 8}, 047 {5, 9, 8, 7, 6, 0, 4, 3, 2, 1}, 048 {6, 5, 9, 8, 7, 1, 0, 4, 3, 2}, 049 {7, 6, 5, 9, 8, 2, 1, 0, 4, 3}, 050 {8, 7, 6, 5, 9, 3, 2, 1, 0, 4}, 051 {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}}; 052 053 /** P - permutation table */ 054 private static final int[][] P_TABLE = { 055 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 056 {1, 5, 7, 6, 2, 8, 3, 0, 9, 4}, 057 {5, 8, 0, 3, 7, 9, 6, 1, 4, 2}, 058 {8, 9, 1, 6, 0, 4, 3, 5, 2, 7}, 059 {9, 4, 5, 3, 1, 2, 6, 8, 7, 0}, 060 {4, 2, 8, 6, 5, 7, 3, 9, 0, 1}, 061 {2, 7, 9, 3, 8, 0, 6, 4, 1, 5}, 062 {7, 0, 4, 6, 9, 1, 3, 2, 5, 8}}; 063 064 /** inv: inverse table */ 065 private static final int[] INV_TABLE = {0, 4, 3, 2, 1, 5, 6, 7, 8, 9}; 066 067 /** 068 * Calculate a Verhoeff <i>Check Digit</i> for a code. 069 * 070 * @param code The code to calculate the Check Digit for 071 * @return The calculated Check Digit 072 * @throws CheckDigitException if an error occurs calculating 073 * the check digit for the specified code 074 */ 075 @Override 076 public String calculate(final String code) throws CheckDigitException { 077 if (code == null || code.isEmpty()) { 078 throw new CheckDigitException("Code is missing"); 079 } 080 final int checksum = calculateChecksum(code, false); 081 return Integer.toString(INV_TABLE[checksum]); 082 } 083 084 /** 085 * Calculate the checksum. 086 * 087 * @param code The code to calculate the checksum for. 088 * @param includesCheckDigit Whether the code includes the Check Digit or not. 089 * @return The checksum value 090 * @throws CheckDigitException if the code contains an invalid character (i.e. not numeric) 091 */ 092 private int calculateChecksum(final String code, final boolean includesCheckDigit) throws CheckDigitException { 093 int checksum = 0; 094 for (int i = 0; i < code.length(); i++) { 095 final int idx = code.length() - (i + 1); 096 final int num = Character.getNumericValue(code.charAt(idx)); 097 if (num < 0 || num > 9) { // CHECKSTYLE IGNORE MagicNumber 098 throw new CheckDigitException("Invalid Character[" + 099 i + "] = '" + (int)code.charAt(idx) + "'"); 100 } 101 final int pos = includesCheckDigit ? i : i + 1; 102 checksum = D_TABLE[checksum][P_TABLE[pos % 8][num]]; // CHECKSTYLE IGNORE MagicNumber 103 } 104 return checksum; 105 } 106 107 /** 108 * Validate the Verhoeff <i>Check Digit</i> for a code. 109 * 110 * @param code The code to validate 111 * @return {@code true} if the check digit is valid, 112 * otherwise {@code false} 113 */ 114 @Override 115 public boolean isValid(final String code) { 116 if (code == null || code.isEmpty()) { 117 return false; 118 } 119 try { 120 return calculateChecksum(code, true) == 0; 121 } catch (final CheckDigitException e) { 122 return false; 123 } 124 } 125 126}