/** * 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)); }
protected String value() throws ChimeraFsException { if (_checksum == null) { Set<Checksum> results = _fs.getInodeChecksums(this); StringBuilder sb = new StringBuilder(); Iterator<Checksum> it = results.iterator(); if (it.hasNext()) { Checksum result = it.next(); sb.append(result.getType()).append(':').append( result.getValue()); } while (it.hasNext()) { Checksum result = it.next(); sb.append(", ").append(result.getType()).append(':').append( result.getValue()); } sb.append(NEWLINE); _checksum = sb.toString(); } return _checksum; } }
/** * 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()); }
/** * * @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); }); }
/** * 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); } }