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?

4
  • 1
    What is GUID/UUID? Commented Dec 13, 2018 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. Commented Dec 13, 2018 at 19:51
  • 3
    You want a hash function? en.wikipedia.org/wiki/Hash_function#Determinism Commented Dec 13, 2018 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. stackoverflow.com/questions/16600708/… Commented Dec 13, 2018 at 19:56

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Also, MD5 is no longer secure. So if used in any security-related context look into using SHA-256/512, Bcrypt or Scrypt.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.