I am writing a message transfer program between multiple clients and server.
I want to generate a unique message ID for every message. It should be generated by the server and returned to the client.
For message transfer, I am using a hash data structure, for example:
{
api => POST,
username => sganesh,
pass => "pass",
message => "hai",
time => "current_time",
}
I want to generate a unique ID using this hash.
I have tried several approaches, MD5 and freeze but these give unreadable IDs. I want some meaningful or readable unique IDs.
I have thought we can use microseconds to differentiate between the IDs but here the problem is multiple clients.
In any situation the IDs should be unique.
Can anyone help me out of this problem?
Thanks in advance.
I suspect you don’t want to do what you are asking, but you can do it.
Take the hash key/values and flatten them to an array
@foo = (%foo).MD5 the array to get an ID code – use
md5_base64(@foo)if you want it to be 7bit (human readable).Remember that hashes are not ordered, so you need
sort @foothe array if you want it to be repeatable.In code, something like:
To be honest, I think you are better off generating a unique random ID (a token) and giving it to the client to return to you, but then from your question, I don’t know your motivation.