It's not a secret anymore!*
The moose likes Java in General and the fly likes Calculate crc checksum md5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calculate crc checksum md5" Watch "Calculate crc checksum md5" New topic
Author

Calculate crc checksum md5

Adam Teg
Ranch Hand

Joined: Jul 10, 2007
Posts: 58
I have three questions regarding crc checksum: For the same file

1) Is crc and checksum the same?
2) Does the value change everytime you calculate it?
3) Is there a sample code you can reffer me to?

Thanks
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
A CRC (cyclic redundancy check) and a checksum are two ways of obtaining a short "signature" of a large chunk of data, such that a change anywhere in the data is very likely to produce a change in the CRC/checksum. An MD5 message digest is another technique with a similar aim. MD5 is better, but is a fairly serious algorithm to program, whereas checksum is trivial to program. Luckily, Java gives you MD5 for free, so it's as easy to use as anything else.

In all cases, if you put the same data in, you should get the same value out, no matter how many times you calculate it. That's rather the whole idea!

Here's my use of it. It's strictly our proprietary code, but it's neither our core competency nor security nor rocket science, so no-one will mind...


[ August 09, 2007: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Adam Teg
Ranch Hand

Joined: Jul 10, 2007
Posts: 58
Thanks.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Actually, my code is using SHA, which is yet another similar thing.
Adam Teg
Ranch Hand

Joined: Jul 10, 2007
Posts: 58
Can you tell me how come this returns a different value everytime for arrayOfBytes?

MessageDigest messageDigest = MessageDigest.getInstance("MD5");
//writer is a Writer object that has the XML file
byte [] stringByte = writer.toString().getBytes("UTF-8");
messageDigest.update(stringByte);
byte[] arrayOfBytes = messageDigest.digest();
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

It should not return a different value each time you run it with the same input. Is this exactly the code that you are using or is this an example that you made just to post in the forum here?

Are you, in your actual code, re-using the MessageDigest object? If yes, then you need to call reset() on the MessageDigest object before you let it calculate a digest, otherwise it will still contain data from the previous calculation. See the API documentation of class MessageDigest.
[ August 09, 2007: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Adam Teg
Ranch Hand

Joined: Jul 10, 2007
Posts: 58
I see why it is returning a different value everytime. The file is in memory and I am trying to calculate it while it is still there.

How can I fix that. Can a file be in memory while crc ic calculated?

Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Have you looked at what "writer.toString()" actually returns? You seem to be assuming that it's going to return some XML, but generally the toString() method of a Writer returns an obscure string composed of the Writer's class name and a random-looking number. If that's what is actually happening then it wouldn't be surprising that you get different values in different runs.
Adam Teg
Ranch Hand

Joined: Jul 10, 2007
Posts: 58
Looks like Writter is not very reliable so I had to write the file thanks.

Switching topics:

I am exporting my app to an exe jar but I keep on getting "duplicate" entry for my .class files although there is only one. I also get a resource out of sync error. How can I fix this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calculate crc checksum md5
 
Similar Threads
how to start and stop tomcat 5.5 server
Info on UDP checksums
Checksum crc 16 modbus
How to unzip a file??
about actualize the CRC witn java