How can I create a unique identifier using a hash in Perl?

I am writing a messaging program between several clients and a server.

I want to create a unique message identifier for each message. It must be generated by the server and returned to the client.

To send messages, I use a hash data structure, for example:

{ api => POST, username => sganesh, pass => "pass", message => "hai", time => "current_time", } 

I want to create a unique identifier using this hash.

I tried several approaches, MD5 and freeze, but they give unreadable identifiers. I want some meaningful or readable unique identifiers.

I thought we could use microseconds to distinguish identifiers, but here the problem is with a few clients.

In any situation, identifiers must be unique.

Can someone help me solve this problem?

Thanks in advance.

+4
source share
2 answers

I suspect that you do not want to do what you ask, but you can do it.

  • Take the hash key / values โ€‹โ€‹and smooth them into the @foo = (%foo) array.

  • MD5 to get the identification code - use md5_base64(@foo) if you want it to be 7-bit (human readable).

  • Remember that the hashes are not ordered, so you need a sort @foo array if you want it to be repeated.

In the 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 

Honestly, I think that you better create a unique random identifier (token) and give it to the client to return to you, but then from your question I do not know your motivation.

+7
source

This sounds like a job for Data :: UUID .

In addition, a unique identifier is for the computer. You can abstract, one way or another, that you love people. :)

+6
source

Source: https://habr.com/ru/post/1304436/


All Articles