1

I'm trying to build a app that would identify a user by scanning a qrcode. For this, I want to use the primary key as the identifier. Since the character length of the integer is short, it wouldn't give a good look as a qrcode.

So my question is: Is it possible to convert the int to string which is longer than 10-12 chars (fixed length if possible),mix of chars and numbers which can be reversed to the original integer.

newfurniturey
  • 37,556
  • 9
  • 94
  • 102
Dester Dezzods
  • 1,417
  • 4
  • 17
  • 33

3 Answers3

1

What you can do is to make SHA256 of your user's ID and convert it to QR code. Then when user reads QR code and send you sha value you try to match it with SHA of user's IDs in the database.

So here is the way to have SHA hash from user id:

$hash = hash('sha256', $userId); // The result is long enough string for QA

The when you need to find a user based on SHA do the following:

select * from users where SHA2(id, 256) = 'SHA_PROVIDED_BY_USER';

You can in order to speed up the look up process store SHA in the DB as well then query will be much faster.

Another option is to prepend the number with some letters. It will give you random string, nice QRs and you can extract numeric ID with simple regexp.

Using function from PHP random string generator (don't forget to remove numbers from $characters) the code could be:

//encoding
$size = 12;
$str = generateRandomString($size-strlen($userId)).$userId;

//decoding
preg_match('/(\d+)$/', $str, $matching);
$userId = $matching[1];
Community
  • 1
  • 1
Stepashka
  • 2,648
  • 20
  • 23
  • I don't think this is a good solution because i don't want to match it, I want to find the exact integer (since i need to find it from the database) – Dester Dezzods Apr 21 '15 at 14:17
  • In any case you'll need to convert integer to string and then convert string to integer again. – Stepashka Apr 21 '15 at 14:21
  • looks promising. I will check it shortly. :) – Dester Dezzods Apr 21 '15 at 14:28
  • Im going to use hash('CRC32',$id) which will give a text legth of 8 good enough to type manually even if the code cannot be decoded by some reason :) – Dester Dezzods Apr 21 '15 at 14:58
  • Please note that CRC32 has higher probability of collisions than SHA. So the probability that different IDs will be matched with the same CRC is higher. – Stepashka Apr 21 '15 at 15:04
0

you can convert your integer to any base with base_convert function.

here is the documentation.

http://php.net/manual/en/function.base-convert.php

tanaydin
  • 5,171
  • 28
  • 45
0

The notion that a number, in PHP, has a "maximum size" is a little off (not wrong, just off =P)

From the manual:

If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead.

So, you could use really large numbers for your QR Codes if you want. Shouldn't be an issue. However, what would be better is to think of "what exactly do you need"?

If you need a numeric value, but want it in hex, you can use base_convert() to go back and forth between the numbers:

$val = 1234;
$hex = base_convert($val, 10, 16);

However, if strings are more for you, you could use base64_encode() to encode it:

$val = 'awesome string value';
$encoded = base64_encode($val);

UPDATE
Based on comments, it sounds like you also want to pad the string if it's too short. You can use str_pad() to accomplish this:

$val = str_pad("1", 10, "0", STR_PAD_LEFT);
echo $val;
// displays: 0000000001
$orig = intval($val);
echo $orig;
// displays: 1

Coderpad Example of str_pad()

newfurniturey
  • 37,556
  • 9
  • 94
  • 102
  • I want the converted string to be long. If I provide '1' as value i should get a string whose length is longer than 10 – Dester Dezzods Apr 21 '15 at 14:23
  • @DesterDezzods updated to show `str_pad()` info; let me know if that's what you were looking for or not, I'll try to revise if not – newfurniturey Apr 21 '15 at 14:26
  • Will the padding with same number give almost similar QR codes? – Stepashka Apr 21 '15 at 14:33
  • @Stepashka No idea, never worked with QR codes - I'm just answering based on the question asked "Is it possible to convert [an integer to a string], mix of chars and numbers, which can be reversed to the original integer.". If this is wrong, please let me know and I'll address =] – newfurniturey Apr 21 '15 at 14:44