Reputation: 1721
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.
Upvotes: 3
Views: 4139
Reputation: 132858
This sounds like a job for Data::UUID.
Furthermore, the unique ID is for a computer. You can abstract that anyway that you like for the humans. :)
Upvotes: 6
Reputation: 5087
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 @foo
the array if you want it to be repeatable.
In code, something like:
use Digest::MD5 qw(md5_base64);
my $foo = {
api => POST,
username => sganesh,
pass => "pass",
message => "hai",
time => "current_time",
};
my $id = md5_base64(sort %$foo); # in my case eRR9QzGN1n+nIl1TDmclEA
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.
Upvotes: 9