new DigestInputStream(InputStream stream, MessageDigest digest) : DigestInputStream « java.security « Java by API

Java by API
1. com.sun.image.codec.jpeg
2. java.applet
3. java.awt
4. java.awt.datatransfer
5. java.awt.dnd
6. java.awt.event
7. java.awt.font
8. java.awt.geom
9. java.awt.image
10. java.awt.print
11. java.beans
12. java.io
13. java.lang
14. java.lang.instrument
15. java.lang.management
16. java.lang.reflect
17. java.math
18. java.net
19. java.nio
20. java.nio.channels
21. java.nio.charset
22. java.rmi.server
23. java.security
24. java.security.cert
25. java.sql
26. java.text
27. java.util
28. java.util.concurrent
29. java.util.logging
30. java.util.prefs
31. java.util.regex
32. java.util.zip
33. javax.accessibility
34. javax.comm
35. javax.naming
36. javax.net
37. javax.net.ssl
38. javax.print
39. javax.servlet
40. javax.servlet.http
41. javax.sql
42. javax.sql.rowset
43. javax.swing
44. javax.swing.border
45. javax.swing.colorchooser
46. javax.swing.event
47. javax.swing.filechooser
48. javax.swing.plaf.basic
49. javax.swing.plaf.metal
50. javax.swing.plaf.synth
51. javax.swing.table
52. javax.swing.text
53. javax.swing.text.html
54. javax.swing.text.html.parser
55. javax.swing.tree
56. javax.swing.undo
57. javax.xml
58. javax.xml.parsers
59. javax.xml.transform
60. javax.xml.transform.dom
61. javax.xml.transform.stream
62. javax.xml.validation
63. javax.xml.xpath
64. org.apache.commons.lang
65. org.apache.commons.lang.builder
66. org.apache.commons.lang.exception
67. org.apache.commons.lang.time
68. org.apache.commons.logging
69. org.apache.commons.math
70. org.eclipse.jface.action
71. org.eclipse.jface.dialogs
72. org.eclipse.jface.operation
73. org.eclipse.jface.viewers
74. org.eclipse.jface.window
75. org.eclipse.jface.wizard
76. org.eclipse.swt
77. org.eclipse.swt.browser
78. org.eclipse.swt.custom
79. org.eclipse.swt.dnd
80. org.eclipse.swt.events
81. org.eclipse.swt.graphics
82. org.eclipse.swt.layout
83. org.eclipse.swt.ole.win32
84. org.eclipse.swt.printing
85. org.eclipse.swt.program
86. org.eclipse.swt.widgets
87. org.w3c.dom
88. org.xml.sax
89. org.xml.sax.helpers
90. sun.audio
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java by API » java.security » DigestInputStream 
new DigestInputStream(InputStream stream, MessageDigest digest)

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.Security;
import java.util.zip.CRC32;

public class MainClass {
  static private String hexDigit(byte x) {
    StringBuffer sb = new StringBuffer();
    char c;
    // First nibble
    c = (char) ((x >> 40xf);
    if (c > 9) {
      c = (char) ((c - 10'a');
    else {
      c = (char) (c + '0');
    }
    sb.append(c);
    // Second nibble
    c = (char) (x & 0xf);
    if (c > 9) {
      c = (char) ((c - 10'a');
    else {
      c = (char) (c + '0');
    }
    sb.append(c);
    return sb.toString();
  }

  static private String computeDigest(MessageDigest algorithm, String filename) {
    String returnValue = "";
    try {
      algorithm.reset();
      FileInputStream fis = new FileInputStream(filename);
      BufferedInputStream bis = new BufferedInputStream(fis);
      DigestInputStream dis = new DigestInputStream(bis, algorithm);
      int ch;
      while ((ch = dis.read()) != -1)
        ;
      StringBuffer hexString = new StringBuffer();
      byte digest[] = algorithm.digest();
      int digestLength = digest.length;
      for (int i = 0; i < digestLength; i++) {
        hexString.append(hexDigit(digest[i]));
        hexString.append(" ");
      }
      returnValue = hexString.toString();
    catch (IOException e) {
      System.err.println("Error generating digest for: " + filename);
    }
    return returnValue;
  }

  public static void main(String args[]) {
    MessageDigest algorithm = null;
    Security.addProvider(new MyProvider());
    try {
      algorithm = MessageDigest.getInstance("CRC32");
    catch (NoSuchAlgorithmException e) {
      System.err.println("Invalid algorithm");
      System.exit(-1);
    }
    int argsLength = args.length;
    for (int i = 0; i < argsLength; i++) {
      String digest = computeDigest(algorithm, args[i]);
      System.out.println(args[i" : " + digest);

    }
  }
}

class MyProvider extends Provider {
  public MyProvider() {
    super("MyProvider"1.0"Java2s.com Collections Example");
    put("CRC32""CRC");
  }
}

class CRC extends MessageDigest {
  CRC32 crc;

  public CRC() {
    super("CRC");
    crc = new CRC32();
  }

  protected void engineReset() {
    crc.reset();
  }

  protected void engineUpdate(byte input) {
    crc.update(input);
  }

  protected void engineUpdate(byte[] input, int offset, int len) {
    crc.update(input, offset, len);
  }

  protected byte[] engineDigest() {
    long l = crc.getValue();
    byte[] bytes = new byte[4];
    bytes[3(byte) ((l & 0xFF000000>> 24);
    bytes[2(byte) ((l & 0x00FF0000>> 16);
    bytes[1(byte) ((l & 0x0000FF00>> 8);
    bytes[0(byte) ((l & 0x000000FF>> 0);
    return bytes;
  }
}

           
       
Related examples in the same category
w__w__w___.___j__a_v_a___2__s___.c_o__m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.