1

I'm wondering if there's a way to create a GUID from a MySQL record ID, so that each time a GUID is created from that ID, it will be the same.

For instance, if we have an ID of 347, the method would look something like this:

create_guid_from_id(347);

always returns: 90880842-7b30-46b0-8179-fa876d4d84bd

Any ideas?

Phll
  • 71
  • 1
  • 5
  • 1
    [What is GUID/UUID?](https://en.wikipedia.org/wiki/Universally_unique_identifier) – Dharman Dec 13 '18 at 19:49
  • 3
    Are you by any chance looking for a hash function? GUID is a random unique identifier. Being able to create exactly the same one by calling the function again would be against its purpose. – Dharman Dec 13 '18 at 19:51
  • 3
    You want a hash function? https://en.wikipedia.org/wiki/Hash_function#Determinism – Felippe Duarte Dec 13 '18 at 19:51
  • You should use any hash function like `md5()` or any hash algorithm. if you want to reconvert the data you can use any encryption/decryption algorithm. https://stackoverflow.com/questions/16600708/how-do-you-encrypt-and-decrypt-a-php-string?answertab=active#tab-top – Rohit Rasela Dec 13 '18 at 19:56

1 Answers1

3

GUIDs are probably not the right thing you are after here. As they produce different output every time and in general don't accept input values

As the commenters have posted you are better to use a hashing algorithm. Hashing algorithms are deterministic/idempotent, meaning that the algorithm will produce the same output every time as long as the input is the same.

You can use md5("347") in order to do this in PHP. Note that the md5 function takes a string as a parameter so probably best to convert your id to a string first

http://php.net/manual/en/function.md5.php

It's also worth noting the hashes can't be converted back into their original form. So you can never get your id back from the hash.

Aaron Long
  • 83
  • 6
  • 1
    Also, MD5 is no longer secure. So if used in any security-related context look into using SHA-256/512, Bcrypt or Scrypt. – J.A.P Dec 13 '18 at 20:39