/** @throws UnsupportedOperationException if not supported */ public MessageDigest getDigestInstance() { if (digestName.equals("SHA-1")) return SHA1.getInstance(); if (digestName.equals("SHA-256")) return SHA256Generator.getDigestInstance(); try { return MessageDigest.getInstance(digestName); } catch (NoSuchAlgorithmException e) { throw new UnsupportedOperationException(e); } }
/** * Reads the stream until EOF. Does not close the stream. * * @return hash SHA-1 hash, NOT a SHA-256 hash */ public SHA1Hash calculateHash(InputStream in) { MessageDigest digest = SHA1.getInstance(); byte buf[] = new byte[64]; int read = 0; try { while ((read = in.read(buf)) != -1) { digest.update(buf, 0, read); } } catch (IOException ioe) { if (_log.shouldLog(Log.WARN)) _log.warn("Unable to hash the stream", ioe); return null; } return new SHA1Hash(digest.digest()); }
@Provides @Singleton SHA1 provideSHA1() { return SHA1.getInstance(); }
/** @return hash SHA-1 hash, NOT a SHA-256 hash */ public static SHA1Hash calculateHash(byte[] source, int offset, int len) { MessageDigest h = SHA1.getInstance(); h.update(source, offset, len); byte digested[] = h.digest(); return new SHA1Hash(digested); }