Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Need and algorithm to converse characters to unique and permanent Serials

by Sombrerero_loco (Beadle)
on Jan 29, 2009 at 08:28 UTC ( [id://739819]=perlquestion: print w/replies, xml ) Need Help??

Sombrerero_loco has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, Im seeking your wisdom for the next: i need a perl module or algorithm to use in perl. I need for a value given (for example a $value) the algoritm returns me always the same value, i mean: If i type John he must return me ekfjosk-001 (for example), and the next time i run the script, give me again the same "code" based in the name. This codes or serials must be unique for the same elements between runs of the script. I was looking the module Algorith::Diff but its not seems the be always de same. I think also to use characters assigned to letter and extract it with regex and use it, but i think this could greated. Any idea? Thanks!


:::When you dream, there're no rules, ppl can fly...anything can happen!!!:::
  • Comment on Need and algorithm to converse characters to unique and permanent Serials

Replies are listed 'Best First'.
Re: Need and algorithm to converse characters to unique and permanent Serials
by Corion (Patriarch) on Jan 29, 2009 at 08:35 UTC
      The OP asked for unique serials, and those algorithms can not guaranteed that, they just make repetitions unlikely (...I know, the requirements are quite vague).

      If uniquesness is a hard requirement, I think that there are only two possible solutions:

      • Track used serials in a file or database (using DB_File or SQLite, for instance).
      • Make the serials transforming the originals in a way so that no information is lost (an injective transformation). For instance, if the data has some structure, use that knowledge to compress it, and then convert it to a human-friendly format.
        salva,
        Using the DB approach would allow the use of Data::UUID.
        • If input string exists, return key
        • If not, generate new one, store it, return it

        Cheers - L~R

Re: Need and algorithm to converse characters to unique and permanent Serials
by bart (Canon) on Jan 29, 2009 at 10:37 UTC

    The only ways I can think of getting unique serials out of a user name are:

    • pulling something out of thin air, like a random string, and stuff them in a database.

      You could use something like a CRC or a digest (like MD5), but clashes, however unlikely, are still theoretically a possibility, so you'd still have to store it. BTW you can append a secret key string to the user supplied value, before processing, to get a more unpredictable output.

    • using reversible encryption. That way you cannot get clashes, but the result will likely be at least as long as the user supplied name, bar compression. You can use something as simple as ROT13, but if people mustn't be able to reverse engineer it too easily, use one of the more elaborate encryption schemes, with a password (AKA encryption key).

      If user names are sufficiently small, you may get by with just using a block cipher algorithm, i.e. virtually any encryption algorithm available, and just use a single cipher block. In that case, you're commonly limited to string of for example 8 bytes long. I'm not sure the latter is actually worth investigating, but most likely, as a result, you'll get serials with a more predictable length.

      For example, the module Crypt::CAST5 (or Crypt::CAST5_PP, if it must be Pure Perl) looks usable to me, straight out of the box.

Re: Need and algorithm to converse characters to unique and permanent Serials
by dHarry (Abbot) on Jan 29, 2009 at 08:40 UTC

    I would use a one-way-function for that. See here for some explanation on one-way-functions. Although in your case it might be overkill, i.e. you don't state that it has to be difficult/impossible to calculate the original out of the generated code. Still a one way function does what you want. Take a look at some of the Crypt modules.

Re: Need and algorithm to converse characters to unique and permanent Serials
by sathiya.sw (Monk) on Jan 29, 2009 at 10:10 UTC
    a simple sample code, by googling.
    #!/usr/bin/perl use Digest::MD5 qw/md5_hex/; while(<>) { print md5_hex($_)," $ARGV\n"; }
    Sathiyamoorthy
Re: Need and algorithm to converse characters to unique and permanent Serials
by apl (Monsignor) on Jan 29, 2009 at 13:07 UTC
    Why not simply use john to identify the record with $value eq 'john'?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://739819]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-24 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found