Domanda

Hi i want to create game launcher that check md5 so if user modified game files the game wouldn't start and display message to reinstall game and if user add more files to the game folder game start but it's will delete a file that user add. How the code look like?

Thank you

È stato utile?

Soluzione

First of all just keep a hidden file in system directory for security measures. Then in that hidden file keep data such as your files and subdirectory names along with md5 hash of each file/subfolders and finally at the end md5 hash of all the combined hash calculted.

Next at the time of booting your game just calcuulte all the directory and files from the root directory of your game

public void listf(String directoryName, ArrayList<File> files) { File directory = new File(directoryName);

// get all the files from a directory
File[] fList = directory.listFiles();
for (File file : fList) {
    if (file.isFile()) {
        files.add(file);
    } else if (file.isDirectory()) {
        listf(file.getAbsolutePath(), files);
    }
}

}

now create a temp file having all the values of it then create md5 hash of each one of the file contents and update the temp file from the md5 hash of the contents of file

MessageDigest md = MessageDigest.getInstance("MD5"); try (InputStream is = Files.newInputStream(Paths.get("file.txt"))) { DigestInputStream dis = new DigestInputStream(is, md); } byte[] digest = md.digest();

cross check the temp file and the original md5 hash of your system at the end again check hash of hash files. (For checking if user hasnt changed the system file)

Since you may have a long list of file so calculating md5 of contents of all files may behave slow. For optimization use Java NIO

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top