Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calculate crc checksum md5

 
Adam Teg
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Adam Teg
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, my code is using SHA, which is yet another similar thing.
 
Adam Teg
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15438
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Adam Teg
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic