/** * Calculates a digest for a given string and returns the digest's String representation. * * @param source_str the string whose contents will be used as the data to calculate the digest * * @return the string's digest or hash as a String */ public static String getDigestString(String source_str) { MessageDigestGenerator md5 = new MessageDigestGenerator(MD5); return md5.calcDigestString(source_str); }
@SuppressWarnings("unchecked") public String getResourceSubscriptionMD5(int resourceId) { MessageDigestGenerator md5Generator = new MessageDigestGenerator(); Query q = entityManager.createNamedQuery(Repo.QUERY_FIND_REPOS_BY_RESOURCE_ID); q.setParameter("resourceId", resourceId); List<Repo> repos = q.getResultList(); for (Repo repo : repos) { long modifiedTimestamp = repo.getLastModifiedDate(); Date modifiedDate = new Date(modifiedTimestamp); md5Generator.add(Integer.toString(modifiedDate.hashCode()).getBytes()); } String digestString = md5Generator.getDigestString(); return digestString; }
/** * Calculates the digest for a given file. The file's contents will be used as the source data for the digest calculation. * * @param file the file whose contents are to be used to calculate the digest. * * @return the file content's digest * * @throws IOException if the file could not be read or accessed */ public static byte[] getDigest(File file) throws IOException { MessageDigestGenerator md5 = new MessageDigestGenerator(MD5); return md5.calcDigest(file); }
MessageDigestGenerator generator = new MessageDigestGenerator(); generator.add(content.getBytes()); hashcode = generator.getDigestString(); } else { debug("Streaming file [", currentLocationFile, "] in-memory to determine its hashcode"); hashcode = generator.calcDigestString(in); } finally { in.close();
/** * This can be used to generate the digest hash from the command line. * * @param args one and only one filename - may or may not be a .jar file. * * @throws Exception if failed to compute the digest for some reason */ public static void main(String[] args) throws Exception { String file = args[0]; String digest = MessageDigestGenerator.getDigestString(new File(file)); System.out.println("MD5=" + digest); digest = new MessageDigestGenerator(SHA_256).calcDigestString(new File(file)); System.out.println("SHA-256=" + digest); } }
private String getMd5(URL pluginURL) throws IOException { return MessageDigestGenerator.getDigestString(pluginURL); }
public String calcDigestString(InputStream is) throws IOException { add(is); return getDigestString(); }
private String sha256(File file) throws IOException { return digestGenerator.calcDigestString(file); }
public StreamCopyDigest() { this.generator = new MessageDigestGenerator(); }
public String calcDigestString(String source_str) { return calculateDigestStringFromBytes(calcDigest(source_str)); }
public byte[] calcDigest(URL url) throws IOException { InputStream is = null; try { is = url.openStream(); return calcDigest(new BufferedInputStream(is, 1024 * 32)); } finally { if (is != null) { is.close(); } } }
MessageDigestGenerator hashcodeGenerator = new MessageDigestGenerator(); String compressedFileHashcode; if (!dryRun) { compressedFileHashcode = hashcodeGenerator.calcDigestString(compressedFile); } else { compressedFileHashcode = hashcodeGenerator.calcDigestString(zipFile); hashcodeGenerator.add(bytes); hashcode = hashcodeGenerator.getDigestString(); } else { debug("Copying raw file [", currentLocationFile, "] to [", newLocationFile, "]. dryRun=", dryRun); hashcode = MessageDigestGenerator.getDigestString(in);
private boolean isNewOrUpdated(String pluginName) { DeploymentInfo deploymentInfo = this.deploymentInfos.get(pluginName); if (deploymentInfo == null) { throw new IllegalStateException("DeploymentInfo was not found for plugin [" + pluginName + " ] - it should have been initialized by preprocessPlugin()."); } PluginManagerLocal pluginMgr = LookupUtil.getPluginManager(); Plugin plugin = pluginMgr.getPlugin(pluginName); if (null == plugin) { log.debug("New plugin [" + pluginName + "] detected."); return true; } String md5 = null; try { md5 = MessageDigestGenerator.getDigestString(new File(deploymentInfo.url.toURI())); } catch (Exception e) { log.error("Error generating MD5 for plugin [" + pluginName + "]. Cause: " + e); } if (!plugin.getMd5().equals(md5)) { log.debug("Updated plugin [" + pluginName + "] detected."); return true; } return false; }
/** * Copies the input stream data to the output stream and returns the * copied data's digest string. * * Note: the streams are never closed - the caller is responsible for that. * * @param in input content * @param out where to write the input content * @return the copied content's digest string (aka hashcode) */ public String copyAndCalculateHashcode(InputStream in, OutputStream out) { try { byte[] buffer = new byte[32768]; BufferedInputStream bufferedStream = new BufferedInputStream(in, buffer.length); for (int bytesRead = bufferedStream.read(buffer); bytesRead != -1; bytesRead = bufferedStream.read(buffer)) { out.write(buffer, 0, bytesRead); this.generator.add(buffer, 0, bytesRead); } out.flush(); } catch (IOException ioe) { throw new RuntimeException("Stream data cannot be copied", ioe); } return this.generator.getDigestString(); } }
private String sha256(File file) throws IOException { return digestGenerator.calcDigestString(file); }
/** * Creates the visitor. When the visitor hits a zip entry whose name matches * filesToRealizeRegex, that zip entry will be realized via the template engine prior * to its hashcode being computed. In other words the file's hashcode will be computed * on the content after its replacement variables have been replaced. * If you just want this visitor to walk a zip file without realizing any files, pass in * a null pattern or pass in a null template engine. This will, in effect, * have this visitor collect all zip file entry names and calculate their hashcodes based on * all content within the zip file. * * @param filesToRealizeRegex pattern of files that are to be realized prior to hashcodes being computed * @param templateEngine the template engine that replaces replacement variables in files to be realized */ public InMemoryZipFileVisitor(Pattern filesToRealizeRegex, TemplateEngine templateEngine) { if (filesToRealizeRegex == null || templateEngine == null) { filesToRealizeRegex = null; templateEngine = null; } this.filesToRealizeRegex = filesToRealizeRegex; this.templateEngine = templateEngine; this.hashcodeGenerator = new MessageDigestGenerator(); }
public String calcDigestString(File file) throws IOException { return calculateDigestStringFromBytes(calcDigest(file)); }
public byte[] calcDigest(String source_str) { try { ByteArrayInputStream bs = new ByteArrayInputStream(source_str.getBytes()); return calcDigest(bs); } catch (IOException e) { throw new RuntimeException("IOException reading a byte array input stream, this should never happen", e); } }
/** * Calculates the digest for a given file. The file's contents will be used as the source data for the digest calculation. * * @param url the URL whose contents are to be used to calculate the digest. * * @return the URL content's digest as a String * * @throws IOException if the URL could not be read or accessed */ public static String getDigestString(URL url) throws IOException { MessageDigestGenerator md5 = new MessageDigestGenerator(MD5); return md5.calcDigestString(url); }