List of usage examples for java.security MessageDigest reset
public void reset()
From source file:fedorax.server.module.storage.lowlevel.irods.IrodsIFileSystem.java
private static final CopyResult stream2streamCopy(InputStream in, OutputStream out) throws IOException { int BUFFER_SIZE = 8192; LOG.debug("IrodsIFileSystem.stream2streamCopy() start"); CopyResult result = new CopyResult(); byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = 0; try {//from w ww. jav a2s.c o m MessageDigest messageDigest; try { messageDigest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new IOException("Cannot compare checksums without MD5 algorithm.", e); } messageDigest.reset(); while ((bytesRead = in.read(buffer, 0, BUFFER_SIZE)) != -1) { messageDigest.update(buffer, 0, bytesRead); result.size = result.size + bytesRead; // for transport failure test add the following line: // buffer[0] = '!'; out.write(buffer, 0, bytesRead); } out.flush(); Hex hex = new Hex(); result.md5 = new String(hex.encode(messageDigest.digest())); } catch (IOException e) { LOG.error("Unexpected", e); throw e; } finally { try { out.close(); } catch (IOException e) { LOG.warn("Exception while trying to close jargon output stream", e); } } LOG.debug("IrodsIFileSystem.stream2streamCopy() end"); return result; }
From source file:staging.plugin.StagingUtils.java
/** * @param stageFileStore/*from w w w . j av a 2 s. co m*/ * @return */ public static String fetchMD5Digest(IFileStore fileStore, IProgressMonitor monitor) throws CoreException { String result = null; IFileInfo info = null; monitor.beginTask("Retreiving staged file and calculating checksum", 100); info = fileStore.fetchInfo(); if (info.getLength() == 0) { return "d41d8cd98f00b204e9800998ecf8427e"; } MessageDigest messageDigest; try { messageDigest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new CoreException(new Status(Status.ERROR, StagingPlugin.PLUGIN_ID, "Cannot create checksum without MD5 algorithm.", e)); } messageDigest.reset(); byte[] buffer = new byte[chunkSize]; int bytesRead = 0; int totalBytesRead = 0; int progressTickBytes = (int) info.getLength() / 100; if (progressTickBytes == 0) { progressTickBytes = 1; // prevents divide by zero on files less // than 100 bytes } BufferedInputStream in = new BufferedInputStream(fileStore.openInputStream(EFS.NONE, null)); try { while ((bytesRead = in.read(buffer, 0, chunkSize)) != -1) { messageDigest.update(buffer, 0, bytesRead); totalBytesRead = totalBytesRead + bytesRead; if ((totalBytesRead % progressTickBytes) < bytesRead) { monitor.worked(1); } } } catch (IOException e) { throw new CoreException(new Status(Status.ERROR, StagingPlugin.PLUGIN_ID, "Cannot read file store to calculate MD5 digest.", e)); } Hex hex = new Hex(); result = new String(hex.encode(messageDigest.digest())); monitor.done(); return result; }
From source file:tvhchgen.Service.java
/** * Calcula el md5 de una cadena de entrada * @param in : la cadena para calcular el md5 * @return el md5 de la cadena pasada como parmetro */// w w w.j ava2s . c om private static String md5ToStr(String in) throws Exception { MessageDigest md = MessageDigest.getInstance("MD5"); md.reset(); md.update(in.getBytes()); byte[] md5bytes = md.digest(); StringBuilder result = new StringBuilder(); for (int index = 0; index < md5bytes.length; index++) { result.append(String.format("%02x", 0xFF & md5bytes[index])); } return result.toString(); }
From source file:org.tdod.ether.util.GameUtil.java
/** * Used to encrypt a String./*from w w w. j a v a2 s . co m*/ * @param x The String to compute the hash with. * @return a hash stored as an array of bytes. */ public static byte[] computeHash(String x) { if (x == null) { return null; } try { java.security.MessageDigest d = null; d = java.security.MessageDigest.getInstance("SHA-1"); d.reset(); d.update(x.getBytes()); return d.digest(); } catch (Exception e) { _log.fatal(e); } return null; }
From source file:staging.plugin.StagingUtils.java
/** * @param sourceFileStore//from www . ja va 2 s .c o m * @param sourceFileInfo * @param checksumMonitor * @return * @throws CoreException */ private static String checksumWithMD5Digest(IFileStore source, IFileInfo sourceInfo, IProgressMonitor monitor) throws CoreException { // TODO honor cancellation requests during copy // TODO report progress log.info("source: {}", source); String result = null; byte[] buffer = new byte[chunkSize]; int length = (int) sourceInfo.getLength(); int progressTickBytes = length / 100; int bytesRead = 0; int totalBytesCopied = 0; InputStream in = null; try { MessageDigest messageDigest; try { messageDigest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new CoreException(new Status(Status.ERROR, StagingPlugin.PLUGIN_ID, "Cannot compare checksums without MD5 algorithm.", e)); } messageDigest.reset(); in = new BufferedInputStream(source.openInputStream(EFS.NONE, null), 1024 * 64); while ((bytesRead = in.read(buffer, 0, chunkSize)) != -1) { messageDigest.update(buffer, 0, bytesRead); totalBytesCopied = totalBytesCopied + bytesRead; if (totalBytesCopied > 0 && progressTickBytes > 0) { if ((totalBytesCopied % progressTickBytes) < bytesRead) { monitor.worked(1); // if (length > 0) { // int percent = (int) (100.0 * ((float) // totalBytesCopied / length)); // monitor.subTask(percent + "% (" + totalBytesCopied / // 1024 + "/" + length / 1024 + "K)"); // } } } } Hex hex = new Hex(); result = new String(hex.encode(messageDigest.digest())); } catch (IOException e) { throw new CoreException(new Status(Status.ERROR, StagingPlugin.PLUGIN_ID, e.getLocalizedMessage(), e)); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { log.error("Trouble closing i/o resources", e); } } return result; }
From source file:com.turn.ttorrent.common.Torrent.java
public static byte[] hash(byte[] data) throws NoSuchAlgorithmException { MessageDigest crypt; crypt = MessageDigest.getInstance("SHA-1"); crypt.reset(); crypt.update(data);//from www . j a v a 2 s . co m return crypt.digest(); }
From source file:staging.plugin.StagingUtils.java
public static final String copyWithMD5Digest(IFileStore source, IFileStore destination, IFileInfo sourceInfo, IProgressMonitor monitor) throws CoreException { // TODO honor cancellation requests during copy // TODO report progress log.debug("source: {}", source); log.debug("destination: {}", destination); // monitor.subTask("Copying file " + source.getName() + "..."); String result = null;/*from ww w . j av a 2 s. c o m*/ byte[] buffer = new byte[chunkSize]; int length = (int) sourceInfo.getLength(); int progressTickBytes = length / 100; int bytesRead = 0; int totalBytesCopied = 0; InputStream in = null; OutputStream out = null; try { MessageDigest messageDigest; try { messageDigest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new CoreException(new Status(Status.ERROR, StagingPlugin.PLUGIN_ID, "Cannot compare checksums without MD5 algorithm.", e)); } messageDigest.reset(); in = new BufferedInputStream(source.openInputStream(EFS.NONE, null), 1024 * 64); destination.getParent().mkdir(EFS.NONE, null); out = new BufferedOutputStream(destination.openOutputStream(EFS.NONE, null), 1024 * 64); while ((bytesRead = in.read(buffer, 0, chunkSize)) != -1) { if (monitor.isCanceled()) { throw new CoreException( new Status(IStatus.CANCEL, StagingPlugin.PLUGIN_ID, "Staging cancelled")); } out.write(buffer, 0, bytesRead); messageDigest.update(buffer, 0, bytesRead); totalBytesCopied = totalBytesCopied + bytesRead; if (totalBytesCopied > 0 && progressTickBytes > 0) { if ((totalBytesCopied % progressTickBytes) < bytesRead) { monitor.worked(1); // if (length > 0) { // int percent = (int) (100.0 * ((float) // totalBytesCopied / length)); // monitor.subTask(percent + "% (" + totalBytesCopied / // 1024 + "/" + length / 1024 + "K)"); // } } } } Hex hex = new Hex(); result = new String(hex.encode(messageDigest.digest())); } catch (IOException e) { throw new CoreException(new Status(Status.ERROR, StagingPlugin.PLUGIN_ID, e.getLocalizedMessage(), e)); } finally { try { if (out != null) { out.flush(); out.close(); } if (in != null) { in.close(); } } catch (IOException e) { log.error("Trouble closing i/o resources", e); } } return result; }
From source file:org.apache.ws.security.message.token.UsernameToken.java
public static String doPasswordDigest(String nonce, String created, byte[] password) { String passwdDigest = null;/*ww w . j av a 2 s . c o m*/ try { byte[] b1 = nonce != null ? Base64.decode(nonce) : new byte[0]; byte[] b2 = created != null ? created.getBytes("UTF-8") : new byte[0]; byte[] b3 = password; byte[] b4 = new byte[b1.length + b2.length + b3.length]; int offset = 0; System.arraycopy(b1, 0, b4, offset, b1.length); offset += b1.length; System.arraycopy(b2, 0, b4, offset, b2.length); offset += b2.length; System.arraycopy(b3, 0, b4, offset, b3.length); MessageDigest sha = MessageDigest.getInstance("SHA-1"); sha.reset(); sha.update(b4); passwdDigest = Base64.encode(sha.digest()); } catch (Exception e) { if (DO_DEBUG) { LOG.debug(e.getMessage(), e); } } return passwdDigest; }
From source file:org.jenkinsci.remoting.engine.HandlerLoopbackLoadStress.java
private static String secretFor(@Nonnull String clientName) { try {//from w w w.ja v a 2 s . c o m MessageDigest digest = MessageDigest.getInstance("MD5"); digest.reset(); byte[] bytes = digest .digest((HandlerLoopbackLoadStress.class.getName() + clientName).getBytes(Charsets.UTF_8)); StringBuilder result = new StringBuilder(Math.max(0, bytes.length * 3 - 1)); for (int i = 0; i < bytes.length; i++) { if (i > 0) { result.append(':'); } result.append(Character.forDigit((bytes[i] >> 4) & 0x0f, 16)); result.append(Character.forDigit(bytes[i] & 0x0f, 16)); } return result.toString(); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("JLS mandates MD5 support"); } }
From source file:org.ejbca.core.protocol.cmp.CmpMessageHelper.java
public static byte[] protectPKIMessageWithPBE(PKIMessage msg, String keyId, String raSecret, String digestAlgId, String macAlgId, int iterationCount) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException { if (LOG.isTraceEnabled()) { LOG.trace(">protectPKIMessageWithPBE()"); }/*w w w . java2 s .com*/ // Create the PasswordBased protection of the message PKIHeaderBuilder head = getHeaderBuilder(msg.getHeader()); byte[] keyIdBytes; try { keyIdBytes = keyId.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { keyIdBytes = keyId.getBytes(); LOG.info("UTF-8 not available, using platform default encoding for keyIdBytes."); } head.setSenderKID(new DEROctetString(keyIdBytes)); // SHA1 AlgorithmIdentifier owfAlg = new AlgorithmIdentifier(digestAlgId); // iterations, usually something like 1024 ASN1Integer iteration = new ASN1Integer(iterationCount); // HMAC/SHA1 AlgorithmIdentifier macAlg = new AlgorithmIdentifier(macAlgId); // We need some random bytes for the nonce byte[] saltbytes = createSenderNonce(); DEROctetString derSalt = new DEROctetString(saltbytes); // Create the new protected return message //String objectId = "1.2.840.113533.7.66.13" = passwordBasedMac; String objectId = CMPObjectIdentifiers.passwordBasedMac.getId(); PBMParameter pp = new PBMParameter(derSalt, owfAlg, iteration, macAlg); AlgorithmIdentifier pAlg = new AlgorithmIdentifier(new ASN1ObjectIdentifier(objectId), pp); head.setProtectionAlg(pAlg); // Calculate the protection bits byte[] rasecret = raSecret.getBytes(); byte[] basekey = new byte[rasecret.length + saltbytes.length]; System.arraycopy(rasecret, 0, basekey, 0, rasecret.length); System.arraycopy(saltbytes, 0, basekey, rasecret.length, saltbytes.length); // Construct the base key according to rfc4210, section 5.1.3.1 MessageDigest dig = MessageDigest.getInstance(owfAlg.getAlgorithm().getId(), "BC"); for (int i = 0; i < iterationCount; i++) { basekey = dig.digest(basekey); dig.reset(); } PKIHeader pkiHeader = head.build(); // Do the mac String macOid = macAlg.getAlgorithm().getId(); byte[] protectedBytes = CmpMessageHelper.getProtectedBytes(pkiHeader, msg.getBody()); //ret.getProtectedBytes(); Mac mac = Mac.getInstance(macOid, "BC"); SecretKey key = new SecretKeySpec(basekey, macOid); mac.init(key); mac.reset(); mac.update(protectedBytes, 0, protectedBytes.length); byte[] out = mac.doFinal(); DERBitString bs = new DERBitString(out); if (LOG.isTraceEnabled()) { LOG.trace("<protectPKIMessageWithPBE()"); } // Return response as byte array return CmpMessageHelper .pkiMessageToByteArray(new PKIMessage(pkiHeader, msg.getBody(), bs, msg.getExtraCerts())); }