List of usage examples for java.security MessageDigest reset
public void reset()
From source file:com.cubeia.game.poker.bot.PokerBot.java
private String getPassword() { String password = String.valueOf(getBot().getId()); if (hashPasswd) { try {// ww w . j a v a2s. c o m MessageDigest md = MessageDigest.getInstance("MD5"); md.reset(); md.update(password.getBytes("ISO-8859-1")); byte[] bytes = md.digest(); return Hex.encodeHexString(bytes); } catch (Exception e) { throw new RuntimeException(e); } } else { return password; } }
From source file:edu.cmu.sei.ams.cloudlet.impl.AESEncrypter.java
public AESEncrypter(String password) { this.password = password; try {/*from ww w .j a v a2s .c o m*/ MessageDigest digest = MessageDigest.getInstance("SHA-256"); digest.reset(); this.skeySpec = new SecretKeySpec(digest.digest(this.password.getBytes("UTF-8")), "AES"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } }
From source file:cz.cas.lib.proarc.common.export.mets.MetsUtils.java
/** * * Generates an MD5 checksum OutputStream * * @param is/*from ww w .ja v a 2 s . c om*/ * @param os * @return * @throws NoSuchAlgorithmException * @throws IOException */ public static FileMD5Info getDigest(InputStream is) throws NoSuchAlgorithmException, IOException { MessageDigest md = MessageDigest.getInstance("MD5"); md.reset(); byte[] bytes = new byte[2048]; int numBytes; long totalBytes = 0; while ((numBytes = is.read(bytes)) > 0) { totalBytes += numBytes; md.update(bytes, 0, numBytes); } byte[] digest = md.digest(); String result = new String(Hex.encodeHex(digest)); return new FileMD5Info(result, totalBytes); }
From source file:org.phoenicis.tools.checksum.ChecksumCalculator.java
private byte[] getDigest(InputStream inputStream, MessageDigest messageDigest, long sizeInBytes, Consumer<ProgressEntity> onChange) throws IOException { messageDigest.reset(); byte[] bytes = new byte[BLOCK_SIZE]; int numBytes; int readBytes = 0; while ((numBytes = inputStream.read(bytes)) != -1) { messageDigest.update(bytes, 0, numBytes); readBytes += numBytes;//from w w w .j a va 2 s .co m if (sizeInBytes != 0L) { double percentage = (double) readBytes * 100. / (double) sizeInBytes; changeState(percentage, onChange); } } return messageDigest.digest(); }
From source file:com.titilink.common.app.EncryptDecryptUtil.java
public void testMD5() throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance("MD5"); md.reset(); byte[] retArr = md.digest("this is a security string".getBytes(Charset.forName("UTF-8"))); System.out.println(Hex.encodeHexString(retArr)); byte[] retArr1 = md.digest("this is a security string".getBytes(Charset.forName("UTF-8"))); System.out.println(Hex.encodeHexString(retArr1)); byte[] retArr2 = md.digest("this is a security string.".getBytes(Charset.forName("UTF-8"))); System.out.println(Hex.encodeHexString(retArr2)); byte[] retArr3 = md.digest("this is a security string.".getBytes(Charset.forName("UTF-8"))); System.out.println(Hex.encodeHexString(retArr3)); }
From source file:cdr.forms.DepositFile.java
private byte[] getDigest(String algorithm) throws NoSuchAlgorithmException, IOException { InputStream input = null;/*from w w w. ja v a 2s. c o m*/ try { MessageDigest messageDigest = MessageDigest.getInstance(algorithm); messageDigest.reset(); input = new FileInputStream(file); byte[] buffer = new byte[1048576]; int count; while ((count = input.read(buffer, 0, buffer.length)) != -1) { messageDigest.update(buffer, 0, count); } return messageDigest.digest(); } finally { if (input != null) { input.close(); } } }
From source file:cz.cas.lib.proarc.common.export.mets.MetsUtils.java
/** * * Generates an MD5 checksum and copies a file (image) to defined * OutputStream/*ww w .j a v a 2 s . co m*/ * * @param is * @param os * @return * @throws NoSuchAlgorithmException * @throws IOException */ public static FileMD5Info getDigestAndCopy(InputStream is, OutputStream os) throws NoSuchAlgorithmException, IOException { MessageDigest md = MessageDigest.getInstance("MD5"); md.reset(); byte[] bytes = new byte[2048]; int numBytes; long totalBytes = 0; while ((numBytes = is.read(bytes)) > 0) { totalBytes += numBytes; md.update(bytes, 0, numBytes); os.write(bytes, 0, numBytes); } byte[] digest = md.digest(); os.close(); is.close(); String result = new String(Hex.encodeHex(digest)); return new FileMD5Info(result, totalBytes); }
From source file:org.sonar.batch.scan.filesystem.FileMetadata.java
/** * Compute hash of a file ignoring line ends differences. * Maximum performance is needed./*from w w w.j a v a 2 s.co m*/ */ Metadata read(File file, Charset encoding) { Reader reader = null; int lines = 0; char c = (char) -1; try { MessageDigest md5Digest = DigestUtils.getMd5Digest(); md5Digest.reset(); reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding)); int i = reader.read(); boolean afterCR = false; while (i != -1) { c = (char) i; if (afterCR) { afterCR = false; if (c == LINE_FEED) { // Ignore i = reader.read(); continue; } } if (c == CARRIAGE_RETURN) { afterCR = true; c = LINE_FEED; } if (c == LINE_FEED) { lines++; } md5Digest.update(charToBytesUTF(c)); i = reader.read(); } if (c != (char) -1) { lines++; } String hash = Hex.encodeHexString(md5Digest.digest()); return new Metadata(lines, hash); } catch (IOException e) { throw new IllegalStateException( String.format("Fail to read file '%s' with encoding '%s'", file.getAbsolutePath(), encoding), e); } finally { IOUtils.closeQuietly(reader); } }
From source file:org.shredzone.cilla.view.ResourceView.java
/** * Sets up the internal cache for the resource's etag and content length. * * @param pack/*from ww w .j ava 2 s . c o m*/ * Resource pack * @param name * Resource name * @return Map key */ private String setup(String pack, String name) throws IOException { String key = pack + '/' + name; if (!(etagMap.containsKey(key) && sizeMap.containsKey(key))) { try (InputStream in = ResourceView.class.getResourceAsStream("/public/" + pack + '/' + name)) { MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.reset(); int counter = 0; byte[] buffer = new byte[4096]; int length; while ((length = in.read(buffer)) >= 0) { md5.update(buffer, 0, length); counter += length; } byte[] digest = md5.digest(); etagMap.put(key, IntStream.range(0, digest.length) .mapToObj(ix -> String.format("%02x", digest[ix] & 0xFF)).collect(joining("", "\"", "\""))); sizeMap.put(key, counter); } catch (NoSuchAlgorithmException ex) { // we expect no exception, since MD5 is a standard digester throw new InternalError(); } } return key; }
From source file:org.codehaus.mojo.mrm.impl.digest.MD5DigestFileEntry.java
/** * Generates the digest.//from w w w .ja v a 2 s . c o m * * @return the digest. * @throws IOException if the backing entry could not be read. * @since 1.0 */ private byte[] getContent() throws IOException { InputStream is = null; try { MessageDigest digest = MessageDigest.getInstance("MD5"); digest.reset(); byte[] buffer = new byte[8192]; int read; try { is = entry.getInputStream(); while ((read = is.read(buffer)) > 0) { digest.update(buffer, 0, read); } } catch (IOException e) { if (is != null) { throw e; } } final String md5 = StringUtils.leftPad(new BigInteger(1, digest.digest()).toString(16), 32, "0"); return md5.getBytes(); } catch (NoSuchAlgorithmException e) { IOException ioe = new IOException("Unable to calculate hash"); ioe.initCause(e); throw ioe; } finally { IOUtils.closeQuietly(is); } }