CRC32 Introduction
CRC Verification Utility Library in the field of data storage and data communication, in order to ensure the correct data, we have to use the means of error detection. CRC is one of the most famous in many error detection methods. CRC is the full name of the cyclic redundancy check.
CRC32 Error detection ability is very strong, small overhead, easy to use encoder and circuit implementation. Judging from its ability to detect errors, it can not find the probability of error is only less than 0.0047%. Both performance and cost considerations are far superior to parity and arithmetic and checksum. Thus, in the field of data storage and data communication, CRC is everywhere: the famous Communication protocol X.25 FCS (frame error sequence) using the Crc-ccitt,arj, LHA and other compression tools software used is CRC32, disk drives read and write using the CRC16, General image Storage Format GIF, TIFF, etc. also use CRC as a means of error detection. CRC Implementation
Package Com.jianggujin.codec;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.zip.CRC32;
/**
* CRC32 * * * @author Jianggujin * */public
class HQCRC32
{
private static HQCRC32 CRC32 = new HQCRC32 ();
public static HQCRC32 getinstance ()
{return
crc32;
}
Private HQCRC32 ()
{
}
private static final int stream_buffer_length = 1024;
Public long Encrypt (byte[] data)
{
CRC32 crc32 = new CRC32 ();
Crc32.update (data);
return Crc32.getvalue ();
}
Public long Encrypt (InputStream data) throws IOException
{
final byte[] buffer = new Byte[stream_buffer_length ];
int read = data.read (buffer, 0, stream_buffer_length);
CRC32 CRC32 = new CRC32 ();
while (Read >-1)
{
crc32.update (buffer, 0, read);
Read = data.read (buffer, 0, stream_buffer_length);
}
return Crc32.getvalue ();
}
Test code:
Import Org.junit.Test;
Import com.jianggujin.codec.HQCRC32;
public class Crc32test
{
HQCRC32 CRC32 = Hqcrc32.getinstance ();
@Test public
Void encode ()
{
byte[] data = "Jianggujin". GetBytes ();
Long result = Crc32.encrypt (data);
SYSTEM.ERR.PRINTLN (result);
}
Test results:
724585211