List of usage examples for java.security DigestInputStream getMessageDigest
public MessageDigest getMessageDigest()
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new BouncyCastleProvider()); byte[] input = "www.java2s.com".getBytes(); System.out.println("input : " + new String(input)); MessageDigest hash = MessageDigest.getInstance("SHA1"); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(input); DigestInputStream digestInputStream = new DigestInputStream(byteArrayInputStream, hash); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); int ch;/*from w ww . ja v a 2 s . c o m*/ while ((ch = digestInputStream.read()) >= 0) { byteArrayOutputStream.write(ch); } byte[] newInput = byteArrayOutputStream.toByteArray(); System.out.println("in digest : " + new String(digestInputStream.getMessageDigest().digest())); byteArrayOutputStream = new ByteArrayOutputStream(); DigestOutputStream digestOutputStream = new DigestOutputStream(byteArrayOutputStream, hash); digestOutputStream.write(newInput); digestOutputStream.close(); System.out.println("out digest: " + new String(digestOutputStream.getMessageDigest().digest())); }
From source file:org.apache.sling.distribution.util.impl.DigestUtils.java
public static String readDigestMessage(DigestInputStream input) { return readDigestMessage(input.getMessageDigest()); }
From source file:MainClass.java
static void performInputTest() throws Exception { MessageDigest md = MessageDigest.getInstance("SHA"); FileInputStream fin = new FileInputStream("sha-results.txt"); DigestInputStream in = new DigestInputStream(fin, md); byte[] b = new byte["testCase".getBytes().length]; in.read(b, 0, "testCase".getBytes().length); md = in.getMessageDigest(); String s = new String(md.digest()); System.out.println("Calculated result: " + s); }
From source file:MainClass.java
public static String md(String f) throws Exception { BufferedInputStream file = new BufferedInputStream(new FileInputStream(f)); MessageDigest md = MessageDigest.getInstance("MD5"); DigestInputStream in = new DigestInputStream(file, md); int i;/*from w ww .j a v a2 s .c o m*/ byte[] buffer = new byte[BUFFER_SIZE]; do { i = in.read(buffer, 0, BUFFER_SIZE); } while (i == BUFFER_SIZE); md = in.getMessageDigest(); in.close(); return new String(md.digest()); }
From source file:MainClass.java
public static String md(String f) throws Exception { BufferedInputStream file = new BufferedInputStream(new FileInputStream(f)); MessageDigest md = MessageDigest.getInstance("SHA-1"); DigestInputStream in = new DigestInputStream(file, md); int i;// w w w.j a v a 2s .c o m byte[] buffer = new byte[BUFFER_SIZE]; do { i = in.read(buffer, 0, BUFFER_SIZE); } while (i == BUFFER_SIZE); md = in.getMessageDigest(); in.close(); return new String(md.digest()); }
From source file:org.duracloud.common.util.ChecksumUtil.java
/** * Determines the checksum value of a DigestInputStream's underlying * stream after the stream has been read. * * @param digestStream//w w w . j av a 2s.co m * @return The checksum value of the stream's contents */ public static String getChecksum(DigestInputStream digestStream) { MessageDigest digest = digestStream.getMessageDigest(); return checksumBytesToString(digest.digest()); }
From source file:org.duracloud.common.util.ChecksumUtil.java
/** * Determines the checksum value of a DigestInputStream's underlying * stream after the stream has been read. * * @param digestStream//from ww w .ja v a2 s . com * @return The checksum value of the stream's contents */ public static byte[] getChecksumBytes(DigestInputStream digestStream) { MessageDigest digest = digestStream.getMessageDigest(); return digest.digest(); }
From source file:Main.java
public static boolean checkMd5sum(File file, String checkCode) throws IOException { DigestInputStream dInput = null; try {/* ww w .j av a2s . co m*/ FileInputStream fInput = new FileInputStream(file); dInput = new DigestInputStream(fInput, getMd5Instance()); byte[] buf = new byte[8192]; while (dInput.read(buf) > 0) { } byte[] bytes = dInput.getMessageDigest().digest(); return bytes2hex(bytes).equals(checkCode); } finally { closeQuietly(dInput); } }
From source file:Main.java
public static String md5(InputStream in) { int bufferSize = 256 * 1024; DigestInputStream digestInputStream = null; try {//from w w w .j a v a 2s .c om MessageDigest messageDigest = MessageDigest.getInstance("MD5"); digestInputStream = new DigestInputStream(in, messageDigest); byte[] buffer = new byte[bufferSize]; while (digestInputStream.read(buffer) > 0) ; messageDigest = digestInputStream.getMessageDigest(); byte[] resultByteArray = messageDigest.digest(); char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; char[] resultCharArray = new char[resultByteArray.length * 2]; int index = 0; for (byte b : resultByteArray) { resultCharArray[index++] = hexDigits[b >>> 4 & 0xf]; resultCharArray[index++] = hexDigits[b & 0xf]; } return new String(resultCharArray); } catch (NoSuchAlgorithmException e) { return null; } catch (IOException e) { e.printStackTrace(); } finally { try { if (digestInputStream != null) digestInputStream.close(); } catch (Exception e) { e.printStackTrace(); } } return null; }
From source file:Main.java
public static String encode(InputStream in) { int bufferSize = 256 * 1024; DigestInputStream digestInputStream = null; try {// w ww.j a v a 2 s . c o m MessageDigest messageDigest = MessageDigest.getInstance("MD5"); digestInputStream = new DigestInputStream(in, messageDigest); byte[] buffer = new byte[bufferSize]; while (digestInputStream.read(buffer) > 0) ; messageDigest = digestInputStream.getMessageDigest(); byte[] resultByteArray = messageDigest.digest(); char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; char[] resultCharArray = new char[resultByteArray.length * 2]; int index = 0; for (byte b : resultByteArray) { resultCharArray[index++] = hexDigits[b >>> 4 & 0xf]; resultCharArray[index++] = hexDigits[b & 0xf]; } return new String(resultCharArray); } catch (NoSuchAlgorithmException e) { return null; } catch (IOException e) { e.printStackTrace(); } finally { try { if (digestInputStream != null) digestInputStream.close(); } catch (Exception e) { e.printStackTrace(); } } return null; }