private String sha256(File file) throws IOException { return digestGenerator.calcDigestString(file); }
private String sha256(File file) throws IOException { return digestGenerator.calcDigestString(file); }
public String calcDigestString(byte[] bytes) throws IOException { try { ByteArrayInputStream bs = new ByteArrayInputStream(bytes); return calcDigestString(bs); } catch (IOException e) { throw new RuntimeException("IOException reading a byte array input stream, this should never happen", e); } }
/** * Similar to {@link #getDigest(InputStream)}, only this returns the digest as a String. * * @param is the stream whose data is to be used to calculate the digest * * @return the stream data's digest as a String * * @throws IOException if failed to read the stream for some reason */ public static String getDigestString(InputStream is) throws IOException { MessageDigestGenerator md5 = new MessageDigestGenerator(MD5); return md5.calcDigestString(is); }
/** * 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); }
/** * 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); }
/** * 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 as a String * * @throws IOException if the file could not be read or accessed */ public static String getDigestString(File file) throws IOException { MessageDigestGenerator md5 = new MessageDigestGenerator(MD5); return md5.calcDigestString(file); }
/** * Computes SHA256 for an archived content. * * @param contentFile content archive * @return SHA256 of the archive */ private String computeSHAForArchivedContent(File contentFile) { if (!contentFile.isDirectory()) { try { MessageDigestGenerator messageDigest = new MessageDigestGenerator(MessageDigestGenerator.SHA_256); return messageDigest.calcDigestString(contentFile); } catch (Exception ex) { log.error("Not able to compute SHA256 for " + contentFile.getPath() + " ."); } } return null; }
/** * 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); } }
public boolean visit(ZipEntry entry, ZipInputStream stream) throws Exception { if (entry.isDirectory()) { return true; // skip directory entries, we only care about the files } String pathname = entry.getName(); String hashcode; if (this.filesToRealizeRegex != null && this.filesToRealizeRegex.matcher(pathname).matches()) { // this entry needs to be realized, do it now, then calc the hashcode // note: tempateEngine will never be null if we got here int contentSize = (int) entry.getSize(); ByteArrayOutputStream baos = new ByteArrayOutputStream((contentSize > 0) ? contentSize : 32768); StreamUtil.copy(stream, baos, false); String content = this.templateEngine.replaceTokens(baos.toString()); baos = null; hashcode = this.hashcodeGenerator.calcDigestString(content); } else { hashcode = this.hashcodeGenerator.calcDigestString(stream); } this.fileHashcodeMap.put(pathname, hashcode); return true; } }
sha = new MessageDigestGenerator(MessageDigestGenerator.SHA_256).calcDigestString(deploymentFile);
/** * Creates a new package described by the specified details. The destination of the content in the provided input * stream will be determined by the package name. * * @param contentFile the file to write the content to * @param content content to be written for the package. NOTE this Stream will be closed by this method. * @param unzip if <code>true</code>, the content stream will be treated like a ZIP file and be unzipped as * it is written, using the package name as the base directory; if <code>false</code> the * content will be written to directly to a file using the package name as the file name */ public void createContent(File destination, File content, boolean unzip) { try { if (unzip) { ZipUtil.unzipFile(content, destination); String sha = new MessageDigestGenerator(MessageDigestGenerator.SHA_256).calcDigestString(content); writeSHAToManifest(destination, sha); } else { InputStream contentStream = new BufferedInputStream(new FileInputStream(content)); FileUtil.writeFile(contentStream, destination); } } catch (IOException e) { throw new RuntimeException("Error creating artifact for contentFile: " + destination, e); } }
realHash = new MessageDigestGenerator(MessageDigestGenerator.MD5).calcDigestString(packageFile); if (!packageVersion.getMD5().equals(realHash)) { throw new Exception("Package version [" + packageVersion + "] failed MD5 check. expected=[" realHash = new MessageDigestGenerator(MessageDigestGenerator.SHA_256).calcDigestString(packageFile); if (!packageVersion.getSHA256().equals(realHash)) { throw new Exception("Package version [" + packageVersion + "] failed SHA256 check. expected=["
private static String computeDigest(ResourceType rt, Collection<MeasurementDefinition> measurements, Collection<OperationDefinition> operations, Collection<PackageType> packageTypes, ConfigurationDefinition pluginConfigurationDefinition, ConfigurationDefinition resourceConfigurationDefinition) { StringBuilder representation = new StringBuilder(); addResourceTypeRepresentation(rt, representation); addMeasurementDefinitionsRepresentations(measurements, representation); addOperationDefinitionsRepresentations(operations, representation); addPackageTypesRepresentations(packageTypes, representation); addRepresentation(pluginConfigurationDefinition, representation); addRepresentation(resourceConfigurationDefinition, representation); return new MessageDigestGenerator(MessageDigestGenerator.SHA_256).calcDigestString(representation.toString()); }
try { contentDetails.put(UPLOAD_SHA256, new MessageDigestGenerator(MessageDigestGenerator.SHA_256).calcDigestString(bytes)); } catch (Exception e) { throw new RuntimeException("Failed to calculate SHA256 for package bits: ", e);
private void mergedStructuredIntoRaws(Configuration configuration, ResourceConfigurationFacet facet) { Set<RawConfiguration> rawConfigs = facet.loadRawConfigurations(); if (rawConfigs == null) { return; } prepareConfigForMergeIntoRaws(configuration, rawConfigs); Queue<RawConfiguration> queue = new LinkedList<RawConfiguration>(rawConfigs); while (!queue.isEmpty()) { RawConfiguration originalRaw = queue.poll(); RawConfiguration mergedRaw = facet.mergeRawConfiguration(configuration, originalRaw); if (mergedRaw != null) { //TODO bypass validation of structured config for template values String contents = mergedRaw.getContents(); String sha256 = new MessageDigestGenerator(MessageDigestGenerator.SHA_256).calcDigestString(contents); mergedRaw.setContents(contents, sha256); updateRawConfig(configuration, originalRaw, mergedRaw); } } }
public void mergeRawConfig(Configuration from, RawConfiguration existingConfig, RawConfiguration toUpdate) throws Exception { Augeas aug = null; try { String lens = configMap.get(existingConfig.getPath()); aug = createAugeas(lens, existingConfig.getContents()); String file = getFile(aug); for (String pathSuffix : nodePaths.get(existingConfig.getPath())) { String propName = ("/files" + existingConfig.getPath() + "/" + pathSuffix).substring(rootNodePath .length()); String propValue = translator.getPropertyValue(propName, from); aug.set("/files" + file + "/" + pathSuffix, propValue); } aug.save(); toUpdate.setPath(existingConfig.getPath()); String contents = FileUtils.readFileToString(new File(file)); String sha256 = new MessageDigestGenerator(MessageDigestGenerator.SHA_256).calcDigestString(contents); toUpdate.setContents(contents, sha256); } finally { if (aug != null) { aug.close(); } } }
sha256 = new MessageDigestGenerator(MessageDigestGenerator.SHA_256).calcDigestString(file); } catch (Exception e) {
public void updateBackingContent(String filename, String displayVersion) { File file = new File(filename); if (!file.exists()) { throw new IllegalArgumentException("File not found: " + file.getAbsolutePath()); } if (file.isDirectory()) { throw new IllegalArgumentException("File expected, found directory: " + file.getAbsolutePath()); } String sha = null; try { sha = new MessageDigestGenerator(MessageDigestGenerator.SHA_256).calcDigestString(file); } catch (Exception e) { //do nothing because the sha will remain null. LOG.error("Message digest for the package bits failed.", e); } String packageVersion = "[sha256=" + sha + "]"; InstalledPackage oldPackage = getBackingContent(); ContentManagerRemote contentManager = remoteClient.getProxy(ContentManagerRemote.class); ContentUploader contentUploader = new ContentUploader(contentManager); String temporaryContentHandle = contentUploader.upload(file); PackageVersion pv = contentManager.createPackageVersionWithDisplayVersion(remoteClient.getSubject(), oldPackage.getPackageVersion().getGeneralPackage().getName(), oldPackage.getPackageVersion() .getGeneralPackage().getPackageType().getId(), packageVersion, displayVersion, oldPackage .getPackageVersion().getArchitecture().getId(), temporaryContentHandle); contentManager.deployPackagesWithNote(remoteClient.getSubject(), new int[] { resourceClientProxy.getId() }, new int[] { pv.getId() }, "CLI deployment request"); }
private void mergeRawsIntoStructured(Configuration configuration, ResourceConfigurationFacet facet) { Configuration structuredConfig = facet.loadStructuredConfiguration(); if (structuredConfig != null) { prepareConfigForMergeIntoStructured(configuration, structuredConfig); for (RawConfiguration rawConfig : configuration.getRawConfigurations()) { String contents = rawConfig.getContents(); String sha256 = new MessageDigestGenerator(MessageDigestGenerator.SHA_256).calcDigestString(contents); rawConfig.setContents(contents, sha256); structuredConfig.addRawConfiguration(rawConfig); facet.mergeStructuredConfiguration(rawConfig, configuration); } } }