private static String normalise(ChecksumType type, String value) { String normalised = value.trim().toLowerCase(); /** * Due to bug in checksum calculation module, some ADLER32 * sums are stored without leading zeros. */ if (type == ADLER32) { normalised = padStart(normalised, type.getNibbles(), '0'); } return normalised; }
/** * Check whether the supplied value is consistent with the given * ChecksumType. * @param type The checksum algorithm. * @param value The checksum value to verify. * @return true if value contains only hexadecimal characters and has the * correct length for the supplied algorithm. */ public static boolean isValid(ChecksumType type, String value) { String normalised = normalise(type, value); return HEXADECIMAL.matchesAllOf(normalised) && normalised.length() == type.getNibbles(); }
/** * Creates a new instance of Checksum based on supplied type and a * string of the checksum value in hexadecimal. If the type is ADLER32 * then the value may omit any leading zeros. * @param type The checksum algorithm. * @param value The hexadecimal representation of the checksum value. * @throws NullPointerException if either argument is null * @throws IllegalArgumentException if the value contains non-hexadecimal * characters or has the wrong length for the checksum type. */ public Checksum(ChecksumType type, String value) { checkNotNull(type, "type may not be null"); checkNotNull(value, "value may not be null"); this.type = type; this.value = normalise(type, value); checkArgument(HEXADECIMAL.matchesAllOf(this.value), "checksum value \"%s\" contains non-hexadecimal digits", value); checkArgument(this.value.length() == type.getNibbles(), "%s requires %d hexadecimal digits but \"%s\" has %d", type.getName(), type.getNibbles(), value, this.value.length()); }