0

I have need in a java application that the file created by my application should not be modified by user, if it is modified then it should get validated.

My approach: I have taken the last modified time of each file in a hashmap and validate the modified file basis on this.

Problem: It is fine for particular session and if i want to persist that information then i have to create another file containing the last modified information that also can modified by user. For now I am not using any database.

So request you to give me a alternative of this i.e. how can i validate file? and is my approach most optimized one?

RTA
  • 1,251
  • 2
  • 14
  • 33

1 Answers1

0

Use a decent hashing algorithm to take a hash of the file contents. To test if the user modified the file, conduct the same hash procedure again (at the time of the test) and compare it to the original hash. If the hashes are different, then the user clearly modified the file. I would suggest you use SHA-1 for your hashing algorithm but someone else might have a better hashing algorithm to use.

You can see this SO answer for information about how to compute a SHA-1 hash from a byte array. You can see this SO answer for information about reading a File into a byte array.

For storing the hash information of each file, I recommend using a database but you don't have to do so. You could use a normal file and within that file, create a format for storing hash related information. Your format could be the user's file name=the hash. For example:

myfile.txt=489892945720524750
otheruserfile.txt=390495940542905490

Community
  • 1
  • 1
KyleM
  • 4,445
  • 9
  • 46
  • 78
  • again I have to write a file that can also be modified by user – RTA Nov 01 '13 at 06:55
  • @RTA is the user supposed to be able to edit the file? Or does the user have the same privileges as you do on the machine, and therefore can see all files? You can always create a password protected file or something. It would be better, again, to install a DB which includes password protection... – KyleM Nov 01 '13 at 07:16
  • application would get installed at user's system then he can modified some of my file that i used to create for my application convience. – RTA Nov 01 '13 at 09:26