@Override public String toString() { return getName(); } }
@Override public String toString() { return type.getType() + ":" + value; }
@Override public int hashCode() { return value.hashCode() ^ type.hashCode(); }
public Checksum(MessageDigest digest) { this(ChecksumType.getChecksumType(digest.getAlgorithm()), digest.digest()); }
/** * 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()); }
/** * Convert the internal dCache code for a checksum type to the * corresponding ChecksumType object. * @throws IllegalArgumentException if the code does not correspond to a * checksum type. */ public static final ChecksumType getChecksumType(int i) { for (ChecksumType type: ChecksumType.values()) { if (type.type == i) { return type; } } throw new IllegalArgumentException("Unknown checksum type: " + i); }
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; }
/** * Create a Checksum that describes the supplied data. * @param data The data with which to calculate the checksum. * @return A Checksum of the supplied data using this algorithm. */ public Checksum calculate(byte[] data) { return new Checksum(this, createMessageDigest().digest(data)); }
/** * * @param inode */ List<Checksum> getInodeChecksums(FsInode inode) { return _jdbc.query("SELECT isum, itype FROM t_inodes_checksum WHERE inumber=?", ps -> ps.setLong(1, inode.ino()), (rs, rowNum) -> { String checksum = rs.getString("isum"); int type = rs.getInt("itype"); return new Checksum(ChecksumType.getChecksumType(type), checksum); }); }
public static final boolean isValid(String s) { return Arrays.stream(ChecksumType.values()) .map(ChecksumType::getName) .anyMatch(x -> x.equalsIgnoreCase(s)); }
/** * 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(); }
/** * Create a new checksum instance for an already computed digest * of a particular type. * * @param digest the input must have the following format: * <type>:<hexadecimal digest> * @throws IllegalArgumentException if argument has wrong form * @throws NullPointerException if argument is null */ public static Checksum parseChecksum(String digest) { checkNotNull(digest, "value may not be null"); int del = digest.indexOf(DELIMITER); if (del < 1) { throw new IllegalArgumentException("Not a dCache checksum: " + digest); } String type = digest.substring(0, del); String checksum = digest.substring(del + 1); return new Checksum(ChecksumType.getChecksumType(type), checksum); } }
/** * Convert a String containing the dCache internal canonical name for * this checksum type to the corresponding value. The case of the name * is ignored. * @throws IllegalArgumentException if the name is unknown */ public static final ChecksumType getChecksumType(String s) { for (ChecksumType type: ChecksumType.values()) { if (type.name.equalsIgnoreCase(s) || String.valueOf(type.type).equals(s)) { return type; } } throw new IllegalArgumentException("Unknown checksum type: " + s); }
/** * Create a MessageDigest object that generates this type of checksum. */ public MessageDigest createMessageDigest() { try { return MessageDigest.getInstance(getName()); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("This is a bug in ChecksumType: " + e.getMessage(), e); } }