Hi. I'm deserializing a json file into a data clas...
# getting-started
j
Hi. I'm deserializing a json file into a data class. I'm then exporting aspects of that data class into a csv file (not using serialization). I'm using JFileChooser in swing to prompt the user to give the CSV export a file name. The idea here is that file names should be unique, so i'm taking the hashcode of that data class instance and appending it to the file name. The problem here is that hashcode() is different for the same input file on different machines. Is there a way to provide a seed to ensure that hashcode() is the same for classes which are
==?
Or do i have to specify my own implementation of
hashcode()
?
k
Nothing really about Kotlin here. What you can do is use MessageDigest / MD5 to get a digest of your string content as a shorter string. No guarantee that it's going to be unique over large enough input sets, but it's going to be stable for identical inputs.
👍 1
t
If all you need is a unique name, then I'm not sure why it matters that hashcode() varies by machine for the same input. Do you have other constraints? Do the filenames need to be unguessable? Is it important that someone who controls the data cannot trick you into suggesting two filenames that match, aka a collision? (Note that hashcode is far, far more prone to producing collisions than a cryptographic hash would be.) Is it important that the suggested names are stable over time, even if the libraries you are using vary?
MD5 of the toString() of the data class might be fine for some purposes, but for other purposes you might need canonicalization as well as stronger hash functions like SHA2.
j
HI Tim. It's important that the hashcodes be consistent across machines because these CSV output files are being uploaded to a database and we want to try our best to enforce that no duplicates are uploaded. My question really was geared toward asking if kotlin's builtin hashing provides enough functionality for me to achieve this. It seems it doesn't. Thanks for the tips
b
to be clear, hashcode in Kotlin (and in Java in general) is not supposed to be a collision-free hashing. As stated earlier, if you want unique and consistent hashcode you're better using a known hashing method like SHA256
t
OK yeah, sounds like you can pick any cryptographic hash function. Beyond that, canonicalization will probably be the most interesting part.
👍🏻 1