CRC Demo : CRC « Security « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Security » CRC 




CRC Demo
   

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.Security;
import java.util.zip.CRC32;

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;
  }
}

public class CRCTest {
  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);

    }
  }
}


           
         
    
    
  














Related examples in the same category
1.This program computes the CRC checksum of a file
2.CRC from 7 zip
3.Calculates CRC checksum for data being (un-)compressed by BZip2 algorithms.
4.Uses CRC32 algorithm for creating fingerprint.
5.16-Bit CRC checksum
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.