Question

I use something like this:

http://www.mkyong.com/java/how-to-generate-a-file-checksum-value-in-java/

to generate a checksum for a file and store the key in a plain txt file.

But how do I go the other way - match/validate the file checksum with the key in the txt file?

On linux you can do:

sha256sum -c myFile.txt

where the myFile.txt contains the SHA and path to the file.

I guess I could just re-compute the checksum and match the value with the value from the file. But is there a less boilerplate way to do this in Java? E.g. simply pass the checksum txt file a MessageDigest like object?

Was it helpful?

Solution

I guess I could just re-compute the checksum and match the value with the value from the file. But is there a less boilerplate way to do this in Java?

No, you have to re-compute the checksum and compare. There's no other way of doing it

You can reduce boiler-plate by writing a method to do your SHA-1 hashing. Then it's just a one-line method call to produce the checksum and verify it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top