I am building an application(in java) and I would like to provide myself with some mesure of its performance. There are multiple long running processes that I am timing, and I have a web server that I will use to receive the metrics, however I would like to create a number that is unique for a particular user(my program doesn’t need to be logged in) on a computer.
So if Joe logged on to computer A and ran my application, It would give him a unique number like 1234. Then if Jane logged on to the same computer it would use a different number, such as 4321. However if Joe logged on to computer B, it would create a separate number entirely, say 5678. Also (just to clarify) If Joe logged back on to computer A, it would still produce the number 1234.
Also, just to make it more challenging, If the same computer was on a different network, then I would preferably want a different ID.
These numbers are arbitrary, and will be longer than this. My code so far generates 8akb7d3n7lp1i1jo6apfhegp593kja5hg38b6o1a5godm38g97omm7bmc3jekc6apopgjhf9g as my UID from:
try {
byte[] mac = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()).getHardwareAddress();
String names = new BigInteger(mac).toString(16) + InetAddress.getLocalHost().getHostAddress() + get("user.name") + get("os.version") + get("java.vendor");
BigInteger uid = new BigInteger(names.getBytes());
thisUsersUid = uid.toString(26);
} catch (SocketException ex) {
Logger.getLogger(TimingLogger.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnknownHostException ex) {
Logger.getLogger(TimingLogger.class.getName()).log(Level.SEVERE, null, ex);
}
I don’t want this information to easily read(as you might figure out names="-f4b86eb8ee1192.168.0.20lee10.6.8Apple Inc.") so I will be adding a hash function to that.
So my question, after all this blurb is: Is this a good way of doing it? Is there a library out there that can do a better job than this? Or am I just wanting too much?
Thanks in advance(even for those comments that say: WHY?)
Your question is not entirely clear, so I’m going to state some assumptions.
If so, here are a couple of options.
First time you run the application, generate a random UUID and store it in the user’s home directory or preferences or something. Subsequent times, look for an existing UUID in the place where it would have been stored by your application.
Generate a UID as a MD5 (or similar) hash of the user’s account name and the machine’s MAC address. This should be stable over restarts … unless there is something weird about the way the user’s PC is implemented.
(If security is a primary concern then the first approach is not on – you have to assume that the user can find and remove the saved UUID. In the second you have the problem that the user can possibly spoof his account name and/or change the PC’s MAC address. For ANY scheme, you have a bigger problem in that ANY SOFTWARE that is installed or run on a user’s PC could be hacked to replace the UID with something that the user wants. In short, you’re fighting a losing battle if your users have hacker skills and motivation.)
I’m not aware of an existing library that will do this.