/** * @param d The {@link Digest} instance - ignored if {@code null} * @return The matching {@link org.apache.sshd.common.digest.BuiltinDigests} whose algorithm matches * (case <U>insensitive</U>) the digets's algorithm - {@code null} if no match */ public static BuiltinDigests fromDigest(Digest d) { return fromAlgorithm((d == null) ? null : d.getAlgorithm()); }
/** * @param d The {@link Digest} instance - ignored if {@code null} * @return The matching {@link org.apache.sshd.common.digest.BuiltinDigests} whose algorithm matches * (case <U>insensitive</U>) the digets's algorithm - {@code null} if no match */ public static BuiltinDigests fromDigest(Digest d) { return fromAlgorithm((d == null) ? null : d.getAlgorithm()); }
/** * @param d The {@link Digest} to use * @param buf The data buffer to be fingerprint-ed * @param offset The offset of the data in the buffer * @param len The length of data - ignored if non-positive * @return The fingerprint - {@code null} if non-positive length * @throws Exception If failed to calculate the fingerprint * @see #getRawFingerprint(Digest, byte[], int, int) */ public static String getFingerPrint(Digest d, byte[] buf, int offset, int len) throws Exception { if (len <= 0) { return null; } byte[] data = getRawFingerprint(d, buf, offset, len); String algo = d.getAlgorithm(); if (BuiltinDigests.md5.getAlgorithm().equals(algo)) { return algo + ":" + BufferUtils.toHex(':', data).toLowerCase(); } Base64.Encoder encoder = Base64.getEncoder(); return algo.replace("-", "").toUpperCase() + ":" + encoder.encodeToString(data).replaceAll("=", ""); }
/** * @param <D> The generic type of digest factory * @param algo The required algorithm name - ignored if {@code null}/empty * @param comp The {@link Comparator} to use to compare algorithm names * @param digests The factories to check - ignored if {@code null}/empty * @return The first {@link DigestFactory} whose algorithm matches the required one * according to the comparator - {@code null} if no match found */ public static <D extends Digest> D findDigestByAlgorithm(String algo, Comparator<? super String> comp, Collection<? extends D> digests) { if (GenericUtils.isEmpty(algo) || GenericUtils.isEmpty(digests)) { return null; } for (D d : digests) { if (comp.compare(algo, d.getAlgorithm()) == 0) { return d; } } return null; }
/** * @param d The {@link Digest} to use * @param buf The data buffer to be fingerprint-ed * @param offset The offset of the data in the buffer * @param len The length of data - ignored if non-positive * @return The fingerprint - {@code null} if non-positive length * @throws Exception If failed to calculate the fingerprint * @see #getRawFingerprint(Digest, byte[], int, int) */ public static String getFingerPrint(Digest d, byte[] buf, int offset, int len) throws Exception { if (len <= 0) { return null; } byte[] data = getRawFingerprint(d, buf, offset, len); String algo = d.getAlgorithm(); if (BuiltinDigests.md5.getAlgorithm().equals(algo)) { return algo + ":" + BufferUtils.toHex(':', data).toLowerCase(); } Base64.Encoder encoder = Base64.getEncoder(); return algo.replace("-", "").toUpperCase() + ":" + encoder.encodeToString(data).replaceAll("=", ""); }
/** * @param <D> The generic type of digest factory * @param algo The required algorithm name - ignored if {@code null}/empty * @param comp The {@link Comparator} to use to compare algorithm names * @param digests The factories to check - ignored if {@code null}/empty * @return The first {@link DigestFactory} whose algorithm matches the required one * according to the comparator - {@code null} if no match found */ public static <D extends Digest> D findDigestByAlgorithm(String algo, Comparator<? super String> comp, Collection<? extends D> digests) { if (GenericUtils.isEmpty(algo) || GenericUtils.isEmpty(digests)) { return null; } for (D d : digests) { if (comp.compare(algo, d.getAlgorithm()) == 0) { return d; } } return null; }
@Override public int compareTo(Digest that) { if (that == null) { return -1; // push null(s) to end } else if (this == that) { return 0; } String thisAlg = getAlgorithm(); String thatAlg = that.getAlgorithm(); int nRes = GenericUtils.safeCompare(thisAlg, thatAlg, false); if (nRes != 0) { return nRes; // debug breakpoint } nRes = Integer.compare(this.getBlockSize(), that.getBlockSize()); if (nRes != 0) { return nRes; // debug breakpoint } return 0; }
@Override public int compareTo(Digest that) { if (that == null) { return -1; // push null(s) to end } else if (this == that) { return 0; } String thisAlg = getAlgorithm(); String thatAlg = that.getAlgorithm(); int nRes = GenericUtils.safeCompare(thisAlg, thatAlg, false); if (nRes != 0) { return nRes; // debug breakpoint } nRes = Integer.compare(this.getBlockSize(), that.getBlockSize()); if (nRes != 0) { return nRes; // debug breakpoint } return 0; }
log.trace("doCheckFileHash({})[{}] offset={}, length={} - algo={}, hash={}", session, file, startOffset, length, digest.getAlgorithm(), BufferUtils.toHex(':', hashValue)); log.trace("doCheckFileHash({})({})[{}] offset={}, length={} - algo={}, hash={}", session, file, count, startOffset, length, digest.getAlgorithm(), BufferUtils.toHex(':', hashValue));