Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alphanumeric (sha1) Semaphore in PHP?

PHP's "shm_get" function requires an integer semaphore key, which I realise to be a restriction of the underlying OS.

I am using the "sha1" function to hash some user input and using the hash to uniquely identify a number of resulting files and and background processes.

Is there a way to convince shm_get to accept an alphanumeric key or to convert a sha1 hash to an acceptable integer?

like image 294
Richard Avatar asked Aug 11 '26 12:08

Richard


1 Answers

You can convert a hexadecimal number into a decimal number by using hexdec()

However if you have got a large number in your hash, this won't return an integer. But you need an integer. So you might want to cut it apart and only use a part of the hash.

$hash = sha1('http://www.hashcat.net/');
$hash = substr($hash, 0, 15); // ok on 64bit systems
$number = (int) hexdec($hash); // cap to PHP_INT_MAX anyway
var_dump($hash, $number);
like image 65
hakre Avatar answered Aug 13 '26 05:08

hakre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!