/** * @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()); }
@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; }
/** * Method used while putting new keys into use that will resize the key used to * initialize the cipher to the needed length. * * @param e the key to resize * @param kdfSize the cipher key-derivation-factor (in bytes) * @param hash the hash algorithm * @param k the key exchange k parameter * @param h the key exchange h parameter * @return the resized key * @throws Exception if a problem occur while resizing the key */ protected byte[] resizeKey(byte[] e, int kdfSize, Digest hash, byte[] k, byte[] h) throws Exception { for (Buffer buffer = null; kdfSize > e.length; buffer = BufferUtils.clear(buffer)) { if (buffer == null) { buffer = new ByteArrayBuffer(); } buffer.putMPInt(k); buffer.putRawBytes(h); buffer.putRawBytes(e); hash.update(buffer.array(), 0, buffer.available()); byte[] foo = hash.digest(); byte[] bar = new byte[e.length + foo.length]; System.arraycopy(e, 0, bar, 0, e.length); System.arraycopy(foo, 0, bar, e.length, foo.length); e = bar; } return e; }
digest.init(); digest.update(digestBuf, 0, readLen); byte[] hashValue = digest.digest(); if (traceEnabled) { log.trace("doCheckFileHash({})[{}] offset={}, length={} - algo={}, hash={}", session, file, startOffset, length, digest.getAlgorithm(), BufferUtils.toHex(':', hashValue)); digest.update(digestBuf, 0, readLen); byte[] hashValue = digest.digest(); // NOTE: this also resets the hash for the next read if (traceEnabled) { log.trace("doCheckFileHash({})({})[{}] offset={}, length={} - algo={}, hash={}", session, file, count, startOffset, length, digest.getAlgorithm(), BufferUtils.toHex(':', hashValue));
@Override public void init(Session s, byte[] v_s, byte[] v_c, byte[] i_s, byte[] i_c) throws Exception { super.init(s, v_s, v_c, i_s, i_c); dh = factory.create(); hash = dh.getHash(); hash.init(); f = dh.getE(); }
/** * Method used while putting new keys into use that will resize the key used to * initialize the cipher to the needed length. * * @param e the key to resize * @param blockSize the cipher block size (in bytes) * @param hash the hash algorithm * @param k the key exchange k parameter * @param h the key exchange h parameter * @return the resized key * @throws Exception if a problem occur while resizing the key */ protected byte[] resizeKey(byte[] e, int blockSize, Digest hash, byte[] k, byte[] h) throws Exception { for (Buffer buffer = null; blockSize > e.length; buffer = BufferUtils.clear(buffer)) { if (buffer == null) { buffer = new ByteArrayBuffer(); } buffer.putMPInt(k); buffer.putRawBytes(h); buffer.putRawBytes(e); hash.update(buffer.array(), 0, buffer.available()); byte[] foo = hash.digest(); byte[] bar = new byte[e.length + foo.length]; System.arraycopy(e, 0, bar, 0, e.length); System.arraycopy(foo, 0, bar, e.length, foo.length); e = bar; } return e; }
@Override public void init(Session s, byte[] v_s, byte[] v_c, byte[] i_s, byte[] i_c) throws Exception { super.init(s, v_s, v_c, i_s, i_c); dh = factory.create(); hash = dh.getHash(); hash.init(); f = dh.getE(); }
hash.update(buf, 0, pos); byte[] iv_c2s = hash.digest(); int j = pos - sessionId.length - 1; hash.update(buf, 0, pos); byte[] iv_s2c = hash.digest(); hash.update(buf, 0, pos); byte[] e_c2s = hash.digest(); hash.update(buf, 0, pos); byte[] e_s2c = hash.digest(); hash.update(buf, 0, pos); byte[] mac_c2s = hash.digest(); hash.update(buf, 0, pos); byte[] mac_s2c = hash.digest();
@Override public void init(Session s, byte[] v_s, byte[] v_c, byte[] i_s, byte[] i_c) throws Exception { super.init(s, v_s, v_c, i_s, i_c); dh = getDH(); hash = dh.getHash(); hash.init(); e = dh.getE(); if (log.isDebugEnabled()) { log.debug("init({})[{}] Send SSH_MSG_KEXDH_INIT", this, s); } Buffer buffer = s.createBuffer(SshConstants.SSH_MSG_KEXDH_INIT, e.length + Integer.SIZE); buffer.putMPInt(e); s.writePacket(buffer); }
/** * @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()); }
@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; }
hash.update(buf, 0, pos); byte[] iv_c2s = hash.digest(); int j = pos - sessionId.length - 1; hash.update(buf, 0, pos); byte[] iv_s2c = hash.digest(); hash.update(buf, 0, pos); byte[] e_c2s = hash.digest(); hash.update(buf, 0, pos); byte[] e_s2c = hash.digest(); hash.update(buf, 0, pos); byte[] mac_c2s = hash.digest(); hash.update(buf, 0, pos); byte[] mac_s2c = hash.digest();
@Override public void init(Session s, byte[] v_s, byte[] v_c, byte[] i_s, byte[] i_c) throws Exception { super.init(s, v_s, v_c, i_s, i_c); dh = getDH(); hash = dh.getHash(); hash.init(); e = dh.getE(); if (log.isDebugEnabled()) { log.debug("init({})[{}] Send SSH_MSG_KEXDH_INIT", this, s); } Buffer buffer = s.createBuffer(SshConstants.SSH_MSG_KEXDH_INIT, e.length + Integer.SIZE); buffer.putMPInt(e); s.writePacket(buffer); }
/** * @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; }