Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP What is this kind of key called? Similar to UUID

Tags:

php

In a web application I'm working on, i need to generate unique id's with an excessive length. Longer than typical UUID's. Another similar web app uses keys that look like this:

cb745abbc635c03f0c259b65y5da57c06e12ef51

What are these called? and how can i create unique ones in PHP? I've tried the UID method, however they are kinda short.

like image 733
ThatGuy343 Avatar asked Oct 21 '22 01:10

ThatGuy343


2 Answers

The example you posted is a 40 character hex string, which therefore looks suspiciously like a SHA1 hash. PHP's built-in sha1() function will hash an input string into just such a hash.

If you pass microtime(true) (to get the current time with microseconds as a float) as input, you'll get a value unique in time. Concatenate it with a hostname for a 40 character globally unique value.

echo sha1(microtime(true) . $hostname));

Note that while this type of value is probably satisfactory as a unique identifier for a database object, user ID, etc, it should not be considered cryptographically secure, as its sequence could be easily guessed.

like image 129
Michael Berkowski Avatar answered Nov 03 '22 02:11

Michael Berkowski


This might be a hash generated from sha1 which is widely used:

From the PHP documentation:

If the optional raw_output is set to TRUE, then the sha1 digest is instead returned in raw binary format with a length of 20, otherwise the returned value is a 40-character hexadecimal number.

echo (sha1("whatever"));

Note that is not certain since it exists multiple other hashing algorithms that will give you a 40 characters length:

echo (hash("ripemd160", "whatever"));
echo (hash("tiger160,3", "whatever"));
echo (hash("tiger160,4", "whatever"));
echo (hash("haval160,3", "whatever"));
echo (hash("haval160,4", "whatever"));
echo (hash("haval160,5", "whatever"));
like image 37
Adam Sinclair Avatar answered Nov 03 '22 03:11

Adam Sinclair