Java Almanac and Examples Collection
The Verhoeff algorithm is a checksum formula for error detection developed by the Dutch mathematician Jacobus Verhoeff and was first published in 1969.
Apache Commons provides the class VerhoeffCheckDigit
.
@Test
public void calculateChecksum()throws CheckDigitException{
// given
String companyCode="90368002";
String expectedChecksum="3";
// when
String actual=VerhoeffCheckDigit.VERHOEFF_CHECK_DIGIT.calculate(companyCode);
logger.info("Checksum for {} is {}",companyCode,actual);
//then
assertEquals(expectedChecksum,actual);
}
@Test
public void validateChecksum(){
// given
String code="903680023";
String faultyCode="1256369654";
// when && then
assertTrue(VerhoeffCheckDigit.VERHOEFF_CHECK_DIGIT.isValid(code));
assertFalse(VerhoeffCheckDigit.VERHOEFF_CHECK_DIGIT.isValid(faultyCode));
}