List of usage examples for java.security MessageDigest reset
public void reset()
From source file:org.dcm4chex.archive.hsm.TarRetrieverService.java
private void extractTar(File tarFile, File cacheDir) throws IOException, VerifyTarException { int count = 0; long totalSize = 0; long free = FileSystemUtils.freeSpace(journal.getDataRootDir().getPath()); long fsize = tarFile.length(); long toDelete = fsize + minFreeDiskSpace - free; if (toDelete > 0) free += free(toDelete);//from w ww . ja v a 2 s .c o m byte[] buf = new byte[bufferSize]; TarInputStream tar = new TarInputStream(new FileInputStream(tarFile)); InputStream in = tar; try { TarEntry entry = skipDirectoryEntries(tar); if (entry == null) throw new IOException("No entries in " + tarFile); String entryName = entry.getName(); Map<String, byte[]> md5sums = null; MessageDigest digest = null; if ("MD5SUM".equals(entryName)) { if (checkMD5) { try { digest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } md5sums = new HashMap<String, byte[]>(); BufferedReader lineReader = new BufferedReader(new InputStreamReader(tar)); String line; while ((line = lineReader.readLine()) != null) { md5sums.put(line.substring(34), MD5Utils.toBytes(line.substring(0, 32))); } } entry = skipDirectoryEntries(tar); } else if (checkMD5) { getLog().warn("Missing MD5SUM entry in " + tarFile); } for (; entry != null; entry = skipDirectoryEntries(tar)) { entryName = entry.getName(); // Retrieve saved MD5 checksum byte[] md5sum = null; if (md5sums != null && digest != null) { md5sum = md5sums.remove(entryName); if (md5sum == null) throw new VerifyTarException("Unexpected TAR entry: " + entryName + " in " + tarFile); digest.reset(); in = new DigestInputStream(tar, digest); } File fOri = new File(cacheDir, entryName.replace('/', File.separatorChar)); File f = new File(fOri.getAbsolutePath() + ".tmp"); File dir = f.getParentFile(); if (dir.mkdirs()) { log.info("M-WRITE " + dir); } log.info("M-WRITE " + f); // Write the stream to file FileOutputStream out = new FileOutputStream(f); boolean cleanup = true; try { int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } cleanup = false; } finally { try { out.close(); } catch (Exception ignore) { } if (cleanup) { log.info("M-DELETE " + f); f.delete(); } } // Verify MD5 if (md5sums != null && digest != null) { if (!Arrays.equals(digest.digest(), md5sum)) { log.info("M-DELETE " + f); f.delete(); throw new VerifyTarException( "Failed MD5 check of TAR entry: " + entryName + " in " + tarFile); } else log.info("MD5 check is successful for " + entryName + " in " + tarFile); } free -= f.length(); count++; totalSize += f.length(); if (f.exists()) f.renameTo(fOri); } } finally { tar.close(); } toDelete = prefFreeDiskSpace - free; if (toDelete > 0) { freeNonBlocking(toDelete); } }
From source file:edu.mayo.informatics.lexgrid.convert.directConversions.MetaThesaurusToSQL.java
/** * Adds qualification to concepts and associations in the LexGrid * repository.//from w ww . j ava2 s. c o m * * @param aq * Qualification information from the UMLS source. * @param constructHCD * Indicates whether artificial context values should be * constructed if not provided in the UMLS information. * @param rela * The relationship attribute defined by UMLS (can be empty or * null). * @param totalCount * The total number of context links qualified previously. * @return The number of contextual links qualified in the repository for * the given UMLS info. * @throws SQLException */ protected int loadContext(AssociationQualification aq, boolean constructHCD, String rela, int totalCount) throws SQLException { // If a context identifier was assigned, use it. // If a context identifier is not assigned and the option to construct // is enabled, // derive one based on the root concept code and path to root AUI // values. int contextLinks = 0; String hcd = aq.qualifierValue; if (constructHCD && StringUtils.isBlank(hcd) && StringUtils.isNotBlank(aq.pathToRoot) && StringUtils.isNotBlank(aq.sourceConceptCode)) { MessageDigest md = getSHA1(); md.reset(); md.update(aq.pathToRoot.getBytes()); hcd = String.valueOf(md.digest(aq.sourceConceptCode.getBytes())); } if (StringUtils.isBlank(hcd)) return 0; // Iterate through the path to root and determine the codes for // participating AUIs. We maintain a LRU cache of AUIs to codes to // assist. // If the associated code is not in the cache, find and cache it here. ListOrderedMap orderedPtrAUIToCode = new ListOrderedMap(); // Break up the path to root into AUIs ... String[] auis = aq.pathToRoot.split("\\."); if (auis.length > 0) { // Check the cache for each. If not found, perform and cache the // AUI to code mapping. PreparedStatement getPTRCode = umlsConnection2_ .prepareStatement("SELECT CUI FROM MRCONSO WHERE AUI = ?"); try { String nextCode, nextAUI; for (int i = 0; i < auis.length; i++) { // Check for registered code in the cache. nextAUI = auis[i]; nextCode = (String) auiToCodeCache_.get(nextAUI); // If not cached, perform lookup ... if (nextCode == null) { getPTRCode.setString(1, nextAUI); ResultSet ptrCodes = getPTRCode.executeQuery(); int count = 0; try { while (ptrCodes.next()) { count++; nextCode = ptrCodes.getString(1); } } finally { ptrCodes.close(); } // If one to one mapping (probably should always be, but // doesn't // hurt to check), add to the cache for quick lookup // later... if (count == 1) auiToCodeCache_.put(nextAUI, nextCode); } // Was it resolved? if (nextCode != null) orderedPtrAUIToCode.put(nextAUI, nextCode); } } finally { getPTRCode.close(); } } // Ensure we have included the original AUI to code mapping from the // provided UMLS qualification info; inserted last as the root // of the path. orderedPtrAUIToCode.put(aq.sourceConceptAUI, aq.sourceConceptCode); // ///////////////////////////////////////////////////////////////////// // We have all the participating codes and AUIs. // Add context qualifiers to the text presentation of each concept // based on code/AUI pairs. // ///////////////////////////////////////////////////////////////////// for (OrderedMapIterator omi = orderedPtrAUIToCode.orderedMapIterator(); omi.hasNext();) { omi.next(); String aui = (String) omi.getKey(); String code = (String) omi.getValue(); if (code != null) qualifyConceptPresentation(code, aui, aq.codingSchemeName, aq.qualifierName, hcd); } // ///////////////////////////////////////////////////////////////////// // At this point we have taken care of all the concept qualifiers. // Now find and similarly tag each participating association link // between AUIs in the path to root chain. // ///////////////////////////////////////////////////////////////////// // Statements to find LexGrid association to concept mappings. // Check source to target (parent association as processed) // or target to source (child association as processed). // Honor the association specified in the MRHIER entry, if provided. // For example, the UMLS 'inverse_isa' is mapped on load to 'hasSubtype' // association name; account for that here. String assoc = mapRela(rela); // If a specific relation attribute (rela) was not provided, consider // all relevant // hierarchical associations (including UMLS standard or source-specific // names). String assocParam = StringUtils.isNotBlank(assoc) ? '\'' + assoc + '\'' : toCommaDelimitedWithQuotes(getHierAssocNames(aq.codingSchemeName)); // Create statements to navigate both directions (up & down the // contextual chain). PreparedStatement getRelationship_1 = sqlConnection_.prepareStatement(new StringBuffer( "SELECT " + SQLTableConstants.TBLCOL_MULTIATTRIBUTESKEY + ", " + stc_.targetEntityCodeOrId + ", " + stc_.entityCodeOrAssociationId + ", " + stc_.sourceEntityCodeOrId + " FROM ") .append(stc_.getTableName(SQLTableConstants.ENTITY_ASSOCIATION_TO_ENTITY)) .append(" WHERE " + stc_.sourceEntityCodeOrId + " = ? AND " + stc_.targetEntityCodeOrId + " = ? AND ") .append(stc_.codingSchemeNameOrId + " = ? AND " + stc_.entityCodeOrAssociationId + " IN (") .append(assocParam).append(")").toString()); PreparedStatement getRelationship_2 = sqlConnection_.prepareStatement(new StringBuffer( "SELECT " + SQLTableConstants.TBLCOL_MULTIATTRIBUTESKEY + ", " + stc_.targetEntityCodeOrId + ", " + stc_.entityCodeOrAssociationId + ", " + stc_.sourceEntityCodeOrId + " FROM ") .append(stc_.getTableName(SQLTableConstants.ENTITY_ASSOCIATION_TO_ENTITY)) .append(" WHERE " + stc_.targetEntityCodeOrId + " = ? AND " + stc_.sourceEntityCodeOrId + " = ? AND ") .append(stc_.codingSchemeNameOrId + " = ? AND " + stc_.entityCodeOrAssociationId + " IN (") .append(assocParam).append(")").toString()); // Statement to update a multi-attributes key for an association // mapping. PreparedStatement updateMAK = sqlConnection_.prepareStatement(new StringBuffer("UPDATE ") .append(stc_.getTableName(SQLTableConstants.ENTITY_ASSOCIATION_TO_ENTITY)) .append(" SET " + SQLTableConstants.TBLCOL_MULTIATTRIBUTESKEY + " = ? " + " WHERE " + stc_.codingSchemeNameOrId + " = ?") .append(" AND " + stc_.sourceEntityCodeOrId + " = ? AND " + stc_.targetEntityCodeOrId + " = ?") .append(" AND " + stc_.entityCodeOrAssociationId + " = ?").toString()); // Locate and qualify each affected association link with the context ID // ... try { PreparedStatement[] stmts = new PreparedStatement[] { getRelationship_1, getRelationship_2 }; for (int s = 0; s < stmts.length; s++) { PreparedStatement stmt = stmts[s]; for (int i = orderedPtrAUIToCode.size() - 1; i > 0; i--) { String code = (String) orderedPtrAUIToCode.getValue(i); String codePrev = (String) orderedPtrAUIToCode.getValue(i - 1); stmt.setString(1, code); stmt.setString(2, codePrev); stmt.setString(3, aq.codingSchemeName); ResultSet results = stmt.executeQuery(); try { // Iterate through all relevant association links ... while (results.next()) { String multiAttributesKey = results .getString(SQLTableConstants.TBLCOL_MULTIATTRIBUTESKEY); String targetConceptCode = results.getString(stc_.targetEntityCodeOrId); String sourceConceptCode = results.getString(stc_.sourceEntityCodeOrId); String association = results.getString(stc_.entityCodeOrAssociationId); // If there is no key to correlate to the // multi-attributes table, // construct and add now. if (multiAttributesKey == null) { StringBuffer key = new StringBuffer().append(System.currentTimeMillis()) .append((int) Math.floor((Math.random() * 100000))).append(totalCount); multiAttributesKey = key.substring(0, Math.min(50, key.length())); updateMAK.setString(1, multiAttributesKey); updateMAK.setString(2, aq.codingSchemeName); updateMAK.setString(3, sourceConceptCode); updateMAK.setString(4, targetConceptCode); updateMAK.setString(5, association); updateMAK.execute(); } // Add a context qualifier to the multi-attributes // table. try { addEntityAssociationQualifierToEntityAssociation(aq.codingSchemeName, multiAttributesKey, aq.qualifierName, hcd); contextLinks++; } catch (SQLException e) { // Because we qualify all relationships along // the PTR and // the HCD is identical for siblings at the same // PTR some // exceptions with regards to identical keys // will come up. // We try to bypass altogether if the message // indicates duplication. // However, message text can vary based on the // database engine. // Rather than exit in error, log the message // and continue. if (!e.getMessage().contains("Duplicate")) { messages_.warn("Unable to add context qualifier to association.", e); } } } } finally { results.close(); } } } } finally { updateMAK.close(); getRelationship_1.close(); getRelationship_2.close(); } return contextLinks; }
From source file:org.vafer.jdeb.DataBuilder.java
/** * Build the data archive of the deb from the provided DataProducers * * @param producers//from w w w .j a v a 2s. c om * @param output * @param checksums * @param compression the compression method used for the data file * @return * @throws java.security.NoSuchAlgorithmException * @throws java.io.IOException * @throws org.apache.commons.compress.compressors.CompressorException */ BigInteger buildData(Collection<DataProducer> producers, File output, final StringBuilder checksums, Compression compression) throws NoSuchAlgorithmException, IOException, CompressorException { final File dir = output.getParentFile(); if (dir != null && (!dir.exists() || !dir.isDirectory())) { throw new IOException("Cannot write data file at '" + output.getAbsolutePath() + "'"); } final TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream( compression.toCompressedOutputStream(new FileOutputStream(output))); tarOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); final MessageDigest digest = MessageDigest.getInstance("MD5"); final Total dataSize = new Total(); final List<String> addedDirectories = new ArrayList<String>(); final DataConsumer receiver = new DataConsumer() { public void onEachDir(String dirname, String linkname, String user, int uid, String group, int gid, int mode, long size) throws IOException { dirname = fixPath(dirname); createParentDirectories(dirname, user, uid, group, gid); // The directory passed in explicitly by the caller also gets the passed-in mode. (Unlike // the parent directories for now. See related comments at "int mode =" in // createParentDirectories, including about a possible bug.) createDirectory(dirname, user, uid, group, gid, mode, 0); console.info("dir: " + dirname); } public void onEachFile(InputStream inputStream, String filename, String linkname, String user, int uid, String group, int gid, int mode, long size) throws IOException { filename = fixPath(filename); createParentDirectories(filename, user, uid, group, gid); final TarArchiveEntry entry = new TarArchiveEntry(filename, true); entry.setUserName(user); entry.setUserId(uid); entry.setGroupName(group); entry.setGroupId(gid); entry.setMode(mode); entry.setSize(size); tarOutputStream.putArchiveEntry(entry); dataSize.add(size); digest.reset(); Utils.copy(inputStream, new DigestOutputStream(tarOutputStream, digest)); final String md5 = Utils.toHex(digest.digest()); tarOutputStream.closeArchiveEntry(); console.info("file:" + entry.getName() + " size:" + entry.getSize() + " mode:" + entry.getMode() + " linkname:" + entry.getLinkName() + " username:" + entry.getUserName() + " userid:" + entry.getUserId() + " groupname:" + entry.getGroupName() + " groupid:" + entry.getGroupId() + " modtime:" + entry.getModTime() + " md5: " + md5); // append to file md5 list checksums.append(md5).append(" ").append(entry.getName()).append('\n'); } public void onEachLink(String path, String linkName, boolean symlink, String user, int uid, String group, int gid, int mode) throws IOException { path = fixPath(path); createParentDirectories(path, user, uid, group, gid); final TarArchiveEntry entry = new TarArchiveEntry(path, symlink ? TarArchiveEntry.LF_SYMLINK : TarArchiveEntry.LF_LINK); entry.setLinkName(linkName); entry.setUserName(user); entry.setUserId(uid); entry.setGroupName(group); entry.setGroupId(gid); entry.setMode(mode); tarOutputStream.putArchiveEntry(entry); tarOutputStream.closeArchiveEntry(); console.info("link:" + entry.getName() + " mode:" + entry.getMode() + " linkname:" + entry.getLinkName() + " username:" + entry.getUserName() + " userid:" + entry.getUserId() + " groupname:" + entry.getGroupName() + " groupid:" + entry.getGroupId()); } private void createDirectory(String directory, String user, int uid, String group, int gid, int mode, long size) throws IOException { // All dirs should end with "/" when created, or the test DebAndTaskTestCase.testTarFileSet() thinks its a file // and so thinks it has the wrong permission. // This consistency also helps when checking if a directory already exists in addedDirectories. if (!directory.endsWith("/")) { directory += "/"; } if (!addedDirectories.contains(directory)) { TarArchiveEntry entry = new TarArchiveEntry(directory, true); entry.setUserName(user); entry.setUserId(uid); entry.setGroupName(group); entry.setGroupId(gid); entry.setMode(mode); entry.setSize(size); tarOutputStream.putArchiveEntry(entry); tarOutputStream.closeArchiveEntry(); addedDirectories.add(directory); // so addedDirectories consistently have "/" for finding duplicates. } } private void createParentDirectories(String filename, String user, int uid, String group, int gid) throws IOException { String dirname = fixPath(new File(filename).getParent()); // Debian packages must have parent directories created // before sub-directories or files can be installed. // For example, if an entry of ./usr/lib/foo/bar existed // in a .deb package, but the ./usr/lib/foo directory didn't // exist, the package installation would fail. The .deb must // then have an entry for ./usr/lib/foo and then ./usr/lib/foo/bar if (dirname == null) { return; } // The loop below will create entries for all parent directories // to ensure that .deb packages will install correctly. String[] pathParts = dirname.split("/"); String parentDir = "./"; for (int i = 1; i < pathParts.length; i++) { parentDir += pathParts[i] + "/"; // Make it so the dirs can be traversed by users. // We could instead try something more granular, like setting the directory // permission to 'rx' for each of the 3 user/group/other read permissions // found on the file being added (ie, only if "other" has read // permission on the main node, then add o+rx permission on all the containing // directories, same w/ user & group), and then also we'd have to // check the parentDirs collection of those already added to // see if those permissions need to be similarly updated. (Note, it hasn't // been demonstrated, but there might be a bug if a user specifically // requests a directory with certain permissions, // that has already been auto-created because it was a parent, and if so, go set // the user-requested mode on that directory instead of this automatic one.) // But for now, keeping it simple by making every dir a+rx. Examples are: // drw-r----- fs/fs # what you get with setMode(mode) // drwxr-xr-x fs/fs # Usable. Too loose? int mode = TarArchiveEntry.DEFAULT_DIR_MODE; createDirectory(parentDir, user, uid, group, gid, mode, 0); } } }; try { for (DataProducer data : producers) { data.produce(receiver); } } finally { tarOutputStream.close(); } console.info("Total size: " + dataSize); return dataSize.count; }
From source file:ldap.ActiveLoginImpl.java
/** * Calculates the pwd hash to be stored in the userPassword field. * * @param s The password in plaintext that should be hashed. * @param type The encryption scheme (The {CRYPT} scheme is currently unsupported): t == 2 means MD5 (salt needs to * be null) t == 3 means SMD5 (needs a salt != null) t == 4 means SHA (salt needs to be null) t == 5 means SSHA * (needs a salt != null)//from w ww . ja v a 2s. c o m * @param salt The salt that is to be used together with the schemes {SMD5} and {SSHA}. Should be between 8 and 16 * Bytes. salt should be null for any other scheme. * @return The base64-encoded hashed pwd with the following format: - {MD5}base64(MD5-hash) for MD5 hashes - * {SHA}base64(SHA-hash) for SHA hashes - {SMD5}base64(MD5-hash+salt bytes) for SMD5 hashes - * {SSHA}base64(SHA-hash+salt bytes) for SSHA hashes Or * {CRYPT}salt + base64(hash) * null if t is not one of 1, 2, 3, 4, 5. */ public byte[] hashPwd(String s, String type, byte[] salt) { try { MessageDigest md; StringBuffer hexString = new StringBuffer(); if (type.equals(LdapConstants.SSHA)) { if (salt == null) salt = getRandomSalt(); md = MessageDigest.getInstance(LdapConstants.SSHA); hexString.append("{" + LdapConstants.SSHA + "}"); } else if (type.equals(LdapConstants.SHA)) { md = MessageDigest.getInstance(LdapConstants.SHA); hexString.append("{" + LdapConstants.SHA + "}"); } else if (type.equals(LdapConstants.SMD5)) { if (salt == null) salt = getRandomSalt(); md = MessageDigest.getInstance(LdapConstants.SMD5); hexString.append("{" + LdapConstants.SMD5 + "}"); } else if (type.equals(LdapConstants.MD5)) { md = MessageDigest.getInstance(LdapConstants.MD5); //hexString.append("{" + MD5 + "}"); hexString.append("{" + LdapConstants.MD5 + "}"); } else if (type.equals(LdapConstants.crypt)) { return hashCrypt(s, salt); } else { logger.warn( "skipping hashing for password with type = " + type + " and has salt " + (salt == null)); return (null); } md.reset(); //md.update(s.getBytes("UTF-8")); md.update(s.getBytes(LdapConstants.UTF8)); if (salt != null) { // The way the salted hashes work is the following: // h=HASH(pwd+salt) // // To be able to restore the salt it needs to be appended // to the resulting hash. // {SMD5|SSHA}base64(h+salt) // So, lets append the salt to the pwd-buffer in md. md.update(salt); // Calculate the hash-value of s+salt byte[] buff = md.digest(); // And append the salt to the hashed pwd. byte[] new_buf = new byte[buff.length + salt.length]; for (int x = 0; x < buff.length; x++) new_buf[x] = buff[x]; for (int x = buff.length; x < new_buf.length; x++) new_buf[x] = salt[x - buff.length]; // New_buf now contains the 16(MD5), resp. 20(SHA1) hash // bytes and the salt. hexString.append(Base64.binaryToString(new_buf)); } else { byte[] buff = md.digest(); hexString.append(Base64.binaryToString(buff)); } //return hexString.toString().getBytes("UTF-8"); return hexString.toString().getBytes(LdapConstants.UTF8); } catch (UnsupportedEncodingException e) { logger.warn("Unexpected error encoding password ", e); e.printStackTrace(); return new byte[0]; } catch (java.security.NoSuchAlgorithmException e) { logger.warn("Unexpected error encoding password ", e); e.printStackTrace(); return new byte[0]; } }
From source file:de.innovationgate.utils.WGUtils.java
/** * Converts a byte array to a MD5 hexadecimal string * @param input The byte input//from w ww . ja v a2 s . co m * @return The MD5 string * @throws NoSuchAlgorithmException */ public static String createMD5HEX(byte[] input) throws NoSuchAlgorithmException { MessageDigest algorithm = MessageDigest.getInstance("MD5"); algorithm.reset(); algorithm.update(input); byte messageDigest[] = algorithm.digest(); StringBuffer hexString = new StringBuffer(); for (int i = 0; i < messageDigest.length; i++) { hexString.append(Integer.toHexString(0xFF & messageDigest[i])); } return hexString.toString(); }
From source file:de.innovationgate.utils.WGUtils.java
/** * Converts an inputstream to a MD5 hex string * the inputstream is implicit closed after reading * @param input/*from w w w . java 2 s . c om*/ * @return MD5 checksum * @throws NoSuchAlgorithmException * @throws IOException */ public static String createMD5HEX(InputStream input) throws NoSuchAlgorithmException, IOException { MessageDigest algorithm = MessageDigest.getInstance("MD5"); algorithm.reset(); InputStream in = null; try { in = new BufferedInputStream(input); byte[] buffer = new byte[1024]; int len = in.read(buffer); while (len > 0) { algorithm.update(buffer, 0, len); len = in.read(buffer); } byte messageDigest[] = algorithm.digest(); StringBuffer hexString = new StringBuffer(); for (int i = 0; i < messageDigest.length; i++) { hexString.append(Integer.toHexString(0xFF & messageDigest[i])); } return hexString.toString(); } finally { if (in != null) { in.close(); } } }
From source file:cz.cas.lib.proarc.common.export.mets.structure.MetsElementVisitor.java
/** * Saves the mets document into a file// w ww .ja v a 2s . com * * @param mets * @param outputFile * @throws MetsExportException */ private void saveMets(Mets mets, File outputFile, IMetsElement metsElement) throws MetsExportException { String fileMd5Name; try { addFileGrpToMets(fileGrpMap); addStructLink(); try { JAXBContext jaxbContext = JAXBContext.newInstance(Mets.class, OaiDcType.class, ModsDefinition.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8"); // marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, // "http://www.w3.org/2001/XMLSchema-instance http://www.w3.org/2001/XMLSchema.xsd http://www.loc.gov/METS/ http://www.loc.gov/standards/mets/mets.xsd http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/mods.xsd http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd"); marshaller.marshal(mets, outputFile); MessageDigest md; try { md = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new MetsExportException("Unable to create MD5 hash", false, e); } md.reset(); InputStream is; try { is = new FileInputStream(outputFile); } catch (FileNotFoundException e) { throw new MetsExportException("Unable to open file:" + outputFile.getAbsolutePath(), false, e); } byte[] bytes = new byte[2048]; int numBytes; long totalBytes = 0; try { while ((numBytes = is.read(bytes)) != -1) { totalBytes = totalBytes + numBytes; md.update(bytes, 0, numBytes); } } catch (IOException e) { throw new MetsExportException("Unable to generate MD5 hash", false, e); } byte[] digest = md.digest(); String result = new String(Hex.encodeHex(digest)); metsElement.getMetsContext().getFileList() .add(new FileMD5Info("." + File.separator + outputFile.getName(), result, totalBytes)); fileMd5Name = "MD5_" + MetsUtils.removeNonAlpabetChars(metsElement.getMetsContext().getPackageID()) + ".md5"; File fileMd5 = new File(metsElement.getMetsContext().getOutputPath() + File.separator + metsElement.getMetsContext().getPackageID() + File.separator + fileMd5Name); OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(fileMd5)); for (FileMD5Info info : metsElement.getMetsContext().getFileList()) { osw.write(info.getMd5() + " " + info.getFileName() + "\n"); } osw.close(); is.close(); // calculate md5 for md5file - it's inserted into info.xml is = new FileInputStream(fileMd5); FileMD5Info md5InfoMd5File = MetsUtils.getDigest(is); is.close(); metsElement.getMetsContext().getFileList() .add(new FileMD5Info("." + File.separator + fileMd5Name, null, fileMd5.length())); MetsUtils.saveInfoFile(metsElement.getMetsContext().getOutputPath(), metsElement.getMetsContext(), md5InfoMd5File.getMd5(), fileMd5Name, outputFile); } catch (Exception ex) { throw new MetsExportException(metsElement.getOriginalPid(), "Unable to save mets file:" + outputFile.getAbsolutePath(), false, ex); } List<String> validationErrors; try { validationErrors = MetsUtils.validateAgainstXSD(outputFile, Mets.class.getResourceAsStream("mets.xsd")); } catch (Exception ex) { throw new MetsExportException("Error while validation document:" + outputFile, false, ex); } if (validationErrors.size() > 0) { MetsExportException metsException = new MetsExportException("Invalid mets file:" + outputFile, false, null); metsException.getExceptions().get(0).setValidationErrors(validationErrors); for (String error : validationErrors) { LOG.fine(error); } throw metsException; } LOG.log(Level.FINE, "Element validated:" + metsElement.getOriginalPid() + "(" + metsElement.getElementType() + ")"); } finally { JhoveUtility.destroyConfigFiles(metsElement.getMetsContext().getJhoveContext()); } metsElement.getMetsContext().getGeneratedPSP().add(metsElement.getMetsContext().getPackageID()); }
From source file:org.vafer.jdeb.Processor.java
/** * Build the data archive of the deb from the provided DataProducers * @param pData//from w ww . j a v a 2 s. com * @param pOutput * @param pChecksums * @param pCompression the compression method used for the data file (gzip, bzip2 or anything else for no compression) * @return * @throws NoSuchAlgorithmException * @throws IOException */ BigInteger buildData(final DataProducer[] pData, final File pOutput, final StringBuffer pChecksums, String pCompression) throws NoSuchAlgorithmException, IOException { OutputStream out = new FileOutputStream(pOutput); if ("gzip".equals(pCompression)) { out = new GZIPOutputStream(out); } else if ("bzip2".equals(pCompression)) { out.write("BZ".getBytes()); out = new CBZip2OutputStream(out); } final TarOutputStream outputStream = new TarOutputStream(out); outputStream.setLongFileMode(TarOutputStream.LONGFILE_GNU); final MessageDigest digest = MessageDigest.getInstance("MD5"); final Total dataSize = new Total(); final List addedDirectories = new ArrayList(); final DataConsumer receiver = new DataConsumer() { public void onEachDir(String dirname, String linkname, String user, int uid, String group, int gid, int mode, long size) throws IOException { dirname = fixPath(dirname); createParentDirectories((new File(dirname)).getParent(), user, uid, group, gid); // The directory passed in explicitly by the caller also gets the passed-in mode. (Unlike // the parent directories for now. See related comments at "int mode =" in // createParentDirectories, including about a possible bug.) createDirectory(dirname, user, uid, group, gid, mode, 0); console.println("dir: " + dirname); } public void onEachFile(InputStream inputStream, String filename, String linkname, String user, int uid, String group, int gid, int mode, long size) throws IOException { filename = fixPath(filename); createParentDirectories((new File(filename)).getParent(), user, uid, group, gid); TarEntry entry = new TarEntry(filename); // FIXME: link is in the constructor entry.setUserName(user); entry.setUserId(uid); entry.setGroupName(group); entry.setGroupId(gid); entry.setMode(mode); entry.setSize(size); outputStream.putNextEntry(entry); dataSize.add(size); digest.reset(); Utils.copy(inputStream, new DigestOutputStream(outputStream, digest)); final String md5 = Utils.toHex(digest.digest()); outputStream.closeEntry(); console.println("file:" + entry.getName() + " size:" + entry.getSize() + " mode:" + entry.getMode() + " linkname:" + entry.getLinkName() + " username:" + entry.getUserName() + " userid:" + entry.getUserId() + " groupname:" + entry.getGroupName() + " groupid:" + entry.getGroupId() + " modtime:" + entry.getModTime() + " md5: " + md5); pChecksums.append(md5).append(" ").append(entry.getName()).append('\n'); } private String fixPath(String path) { // If we're receiving directory names from Windows, then we'll convert to use slash // This does eliminate the ability to use of a backslash in a directory name on *NIX, but in practice, this is a non-issue if (path.indexOf('\\') > -1) { path = path.replace('\\', '/'); } // ensure the path is like : ./foo/bar if (path.startsWith("/")) { path = "." + path; } else if (!path.startsWith("./")) { path = "./" + path; } return path; } private void createDirectory(String directory, String user, int uid, String group, int gid, int mode, long size) throws IOException { // All dirs should end with "/" when created, or the test DebAndTaskTestCase.testTarFileSet() thinks its a file // and so thinks it has the wrong permission. // This consistency also helps when checking if a directory already exists in addedDirectories. if (!directory.endsWith("/")) { directory += "/"; } if (!addedDirectories.contains(directory)) { TarEntry entry = new TarEntry(directory); // FIXME: link is in the constructor entry.setUserName(user); entry.setUserId(uid); entry.setGroupName(group); entry.setGroupId(gid); entry.setMode(mode); entry.setSize(size); outputStream.putNextEntry(entry); outputStream.closeEntry(); addedDirectories.add(directory); // so addedDirectories consistently have "/" for finding duplicates. } } private void createParentDirectories(String dirname, String user, int uid, String group, int gid) throws IOException { // Debian packages must have parent directories created // before sub-directories or files can be installed. // For example, if an entry of ./usr/lib/foo/bar existed // in a .deb package, but the ./usr/lib/foo directory didn't // exist, the package installation would fail. The .deb must // then have an entry for ./usr/lib/foo and then ./usr/lib/foo/bar if (dirname == null) { return; } // The loop below will create entries for all parent directories // to ensure that .deb packages will install correctly. String[] pathParts = dirname.split("\\/"); String parentDir = "./"; for (int i = 1; i < pathParts.length; i++) { parentDir += pathParts[i] + "/"; // Make it so the dirs can be traversed by users. // We could instead try something more granular, like setting the directory // permission to 'rx' for each of the 3 user/group/other read permissions // found on the file being added (ie, only if "other" has read // permission on the main node, then add o+rx permission on all the containing // directories, same w/ user & group), and then also we'd have to // check the parentDirs collection of those already added to // see if those permissions need to be similarly updated. (Note, it hasn't // been demonstrated, but there might be a bug if a user specifically // requests a directory with certain permissions, // that has already been auto-created because it was a parent, and if so, go set // the user-requested mode on that directory instead of this automatic one.) // But for now, keeping it simple by making every dir a+rx. Examples are: // drw-r----- fs/fs # what you get with setMode(mode) // drwxr-xr-x fs/fs # Usable. Too loose? int mode = TarEntry.DEFAULT_DIR_MODE; createDirectory(parentDir, user, uid, group, gid, mode, 0); } } }; for (int i = 0; i < pData.length; i++) { final DataProducer data = pData[i]; data.produce(receiver); } outputStream.close(); console.println("Total size: " + dataSize); return dataSize.count; }
From source file:edu.mayo.informatics.lexgrid.convert.directConversions.UmlsCommon.UMLSBaseCode.java
/** * Generates a unique but reproducible key based on the given base of string * values./*from ww w . j av a2 s .c om*/ * * @param components * @return A unique string based on 20-byte output from a SHA-1 message * digest. * @throws SQLException */ protected String generateUniqueKey(String[] basis) { MessageDigest md = getSHA1(); md.reset(); for (int i = 0; i < basis.length; i++) if (basis[i] != null) md.update(basis[i].getBytes()); byte[] bytes = md.digest(); return String.valueOf(Hex.encodeHex(bytes)); }
From source file:com.evolveum.polygon.connector.ldap.schema.AbstractSchemaTranslator.java
private String hashBytes(byte[] clear, String alg, long seed) { MessageDigest md = null; try {//from w w w . ja v a2s.com if (alg.equalsIgnoreCase("SSHA") || alg.equalsIgnoreCase("SHA")) { md = MessageDigest.getInstance("SHA-1"); } else if (alg.equalsIgnoreCase("SMD5") || alg.equalsIgnoreCase("MD5")) { md = MessageDigest.getInstance("MD5"); } } catch (NoSuchAlgorithmException e) { throw new ConnectorException("Could not find MessageDigest algorithm: " + alg); } if (md == null) { throw new ConnectorException("Unsupported MessageDigest algorithm: " + alg); } byte[] salt = {}; if (alg.equalsIgnoreCase("SSHA") || alg.equalsIgnoreCase("SMD5")) { Random rnd = new Random(); rnd.setSeed(System.currentTimeMillis() + seed); salt = new byte[8]; rnd.nextBytes(salt); } md.reset(); md.update(clear); md.update(salt); byte[] hash = md.digest(); byte[] hashAndSalt = new byte[hash.length + salt.length]; System.arraycopy(hash, 0, hashAndSalt, 0, hash.length); System.arraycopy(salt, 0, hashAndSalt, hash.length, salt.length); StringBuilder resSb = new StringBuilder(alg.length() + hashAndSalt.length); resSb.append('{'); resSb.append(alg); resSb.append('}'); resSb.append(Base64.encode(hashAndSalt)); return resSb.toString(); }