How does python converts mysql binary(16) - php

I have two apps one written in php and one in python and both of them use the same mysql database.
For the public id of the entries in some of the tables I use binary(16) fields(I can't change this, it must remain this way).
The question is how does python does the conversion of this binary field?
Let's take one of the entries as an example.
When I get it in php(from the db) the value of the public id is °•WiCÄ‘õ0Iò|–g, the same value is shown in SequelPro. But php myAdmin does a hex function over binary fields and shows 0bb09557691443c491f53049f27c9667. Now I managed in php to convert the binary to the value showed in php myAdmin and it works for all the entries but I've just noticed that python does another conversion. When I get the entry used in this example via python the public id is owwweye1rjnvt3i1d0ib18x3.
What I need to achieve is to convert in php what I get from MySql: °•WiCÄ‘õ0Iò|–g to what python sees: owwweye1rjnvt3i1d0ib18x3. The php app makes calls on the python one(not developed by me) and thus the id needs to be in the same format for a successfull call.
Any suggestions are welcomed. Thanks.
EDIT: If i send °•WiCÄ‘õ0Iò|–g from php to python and print it rigth away I get: °•WiCÄ‘õ0Iò|–g

Finally I've sorted this out.
Seems that python converts to base36 not hex as I've wrongly supposed.
I've tried to simply base_convert 0bb09557691443c491f53049f27c9667 from 16 to 36 but I've got owwweye1rk04k4cskkw4s08s. Not really what I needed but still a great step further as it started to look like owwweye1rjnvt3i1d0ib18x3.
This difference I supposed to appear because of the large values to be converted(loss of precision), so I've further researched and found the bellow function, written by Clifford dot ct at gmail dot com on the php.net website:
<?php
function str_baseconvert($str, $frombase=10, $tobase=36) {
$str = trim($str);
if (intval($frombase) != 10) {
$len = strlen($str);
$q = 0;
for ($i=0; $i<$len; $i++) {
$r = base_convert($str[$i], $frombase, 10);
$q = bcadd(bcmul($q, $frombase), $r);
}
}
else $q = $str;
if (intval($tobase) != 10) {
$s = '';
while (bccomp($q, '0', 0) > 0) {
$r = intval(bcmod($q, $tobase));
$s = base_convert($r, 10, $tobase) . $s;
$q = bcdiv($q, $tobase, 0);
}
}
else $s = $q;
return $s;
}
?>
I don't think others will come across this issue very often, but still if it happens hope they'll find this instead of burning their brains out like I did :))))

Related

Converting base10 to base62 and back in PHP on a 32bit system

I found a previously asked question here, and am trying to implement Eineki's solution, however I'm on a 32bit system (I've installed Ubuntu 20.04 on an old laptop for testing). It was noted in the comments that the code wouldn't work on a 32bit system but there are workarounds, but they are not spelled out clearly enough for me. Unfortunately I'm pretty new with PHP and also with this website. I tried to comment there to ask for help and was told I couldn't because my account is new.
So here's how I modified that code, based on the instructions given in those comments. The instructions were:
Try to change the line occurence of $num % $b with fmod($num, $b) and
$num/$b (and similar ones) with intdiv($nub, $b).
function base10to62($num, $b=62) {
$base='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$r = fmod($num % $b) ;
$res = $base[$r];
$q = floor(intdiv($num/$b));
while ($q) {
$r = fmod($q % $b);
$q = floor(intdiv($q/$b));
$res = $base[$r].$res;
}
return $res;
}
function base62to10( $num, $b=62) {
$base='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$limit = strlen($num);
$res=strpos($base,$num[0]);
for($i=1;$i<$limit;$i++) {
$res = $b * $res + strpos($base,$num[$i]);
}
return $res;
$base3_string1 = "212012222102200121211101010110220202222222211121102101011101111110021111210000102211101010101110101010101010220110000110111010101010000010002020001010200000022111111111110011120201111110000002002002002001011011101010000110010000000000";
$base10test = base_convert($base3_string1, 3, 10);
$base62test = base10to62($base10test);
$base62returntest = base62to10($base62test);
echo ("My base3_string1 is: ".$base3_string1."<br>");
echo ("My base10 number is: ".$base10test."<br>");
echo ("My base62 number is: ".$base62test."<br>");
echo ("My base62return hash is: ".$base62returntest."<br>");
As you can see, I have a base3 number I'm converting to base10. I'm doing this because it looks like Eineki's function wants base10 input. The base10 number I'm trying to convert is 8080064204040204462880280000286008662604880682228402402248426842, but when I attempt this the return is 0 and when I try to change it back of course the result is 0 because the input is 0.
I'm guessing I've misinterpreted Eineki's instructions on how to convert his code to work on a 32bit system. If anyone can help me correct this it would be appreciated.

Convert FoxPro function to php

I have been asked/told to convert a foxpro function to PHP, however I know nothing about foxpro.
PARAMETERS cCkey
LOCAL cKey
cKey = SUBSTR(SYS(2015),2)+PADL(LTRIM(STR(INT(IIF(INT(RAND()*1000000000) = 851390329,RAND(-1),RAND())*1000000),6)),6,"0")
RETURN cKey
Above is the function they are wanting to use in a system that is being built in php to integrate with the foxpro databases.
Some of the functions are familiar from PHP, but others like the "SYS", and "IIF" are not, and being that I know someone on here will be able to take one look at it, and know exactly what it is doing.
Mind helping me out? Thanks in advance.
Sys(2015) is a handy VFP function which returns a value unique to that session of VFP. You can read it here
Iif is inline if-else-endif statement .. like Excel does
Updated
SYS(2015) in PHP ? I don't know .. but if we talking about random string in PHP, you can use this
function rand_string( $length ) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$size = strlen( $chars );
for( $i = 0; $i < $length; $i++ ) {
$str .= $chars[ rand( 0, $size - 1 ) ];
}
return $str;
}
I got that from this link and got the basic idea from this link
About the other part MAYBE like this :
$randomresult = 0
$srandom = ""
If (INT(RAND()*1000000000) = 851390329)
{
$randomresult = int(rand(-1)) * 1000000
}
else
{
$randomresult = int(rand()) * 1000000
}
$srandom=str_pad(ltrim(strval($randomresult),"0")),6,"0",STR_PAD_LEFT)
So, MAYBE we can make the foxpro code like this in PHP :
$cKey = rand_string(10) . str_pad(ltrim(strval($randomresult),"0")),6,"0",STR_PAD_LEFT)
At least you can the idea .....
SYS(2015) Returns a unique 10-character procedure name that begins with an underscore followed by a combination of letters and numbers.
http://msdn.microsoft.com/en-us/library/684by7c1(v=vs.80).aspx
IIF Returns one of two values depending on the value of a logical expression.
http://msdn.microsoft.com/en-us/library/7ttt15k6(v=vs.80).aspx
With this information I think you can at least take a stab at creating a PHP function and then showing some PHP and asking for help if needed.

Generating unique 6 digit code

I'm generating a 6 digit code from the following characters. These will be used to stamp on stickers.
They will be generated in batches of 10k or less (before printing) and I don't envisage there will ever be more than 1-2 million total (probably much less).
After I generate the batches of codes, I'll check the MySQL database of existing codes to ensure there are no duplicates.
// exclude problem chars: B8G6I1l0OQDS5Z2
$characters = 'ACEFHJKMNPRTUVWXY4937';
$string = '';
for ($i = 0; $i < 6; $i++) {
$string .= $characters[rand(0, strlen($characters) - 1)];
}
return $string;
Is this a solid approach to generating the code?
How many possible permutations would there be? (6 Digit code from pool of 21 characters). Sorry math isn't my strong point
21^6 = 85766121 possibilities.
Using a DB and storing used values is bad. If you want to fake randomness you can use the following:
Reduce to 19 possible numbers and make use of the fact that groups of order p^k where p is an odd prime are always cyclic.
Take the group of order 7^19, using a generator co-prime to 7^19 (I'll pick 13^11, you can choose anything not divisible by 7).
Then the following works:
$previous = 0;
function generator($previous)
{
$generator = pow(13,11);
$modulus = pow(7,19); //int might be too small
$possibleChars = "ACEFHJKMNPRTUVWXY49";
$previous = ($previous + $generator) % $modulus;
$output='';
$temp = $previous;
for($i = 0; $i < 6; $i++) {
$output += $possibleChars[$temp % 19];
$temp = $temp / 19;
}
return $output;
}
It will cycle through all possible values and look a little random unless they go digging. An even safer alternative would be multiplicative groups but I forget my math already :(
There is a lot of possible combination with or without repetition so your logic would be sufficient
Collision would be frequent because you are using rand see str_shuffle and randomness.
Change rand to mt_rand
Use fast storage like memcached or redis not MySQL when checking
Total Possibility
21 ^ 6 = 85,766,121
85,766,121 should be ok , To add database to this generation try:
Example
$prifix = "stamp.";
$cache = new Memcache();
$cache->addserver("127.0.0.1");
$stamp = myRand(6);
while($cache->get($prifix . $stamp)) {
$stamp = myRand(6);
}
echo $stamp;
Function Used
function myRand($no, $str = "", $chr = 'ACEFHJKMNPRTUVWXY4937') {
$length = strlen($chr);
while($no --) {
$str .= $chr{mt_rand(0, $length- 1)};
}
return $str;
}
as Baba said generating a string on the fly will result in tons of collisions. the closer you will go to 80 millions already generated ones the harder it will became to get an available string
another solution could be to generate all possible combinations once, and store each of them in the database already, with some boolean column field that marks if a row/token is already used or not
then to get one of them
SELECT * FROM tokens WHERE tokenIsUsed = 0 ORDER BY RAND() LIMIT 0,1
and then mark it as already used
UPDATE tokens SET tokenIsUsed = 1 WHERE token = ...
You would have 21 ^ 6 codes = 85 766 121 ~ 85.8 million codes!
To generate them all (which would take some time), look at the selected answer to this question: algorithm that will take numbers or words and find all possible combinations.
I had the same problem, and I found very impressive open source solution:
http://www.hashids.org/php/
You can take and use it, also it's worth it to look in it's source code to understand what's happening under the hood.
Or... you can encode username+datetime in md5 and save to database, this for sure will generate an unique code ;)

Why is PHP base_convert giving a different result to the same MYSQL function?

I've put together a PHP function as follows:
function keyword_hash($keyword) {
return base_convert(substr(md5($keyword), -16), 16, 10);
}
The aim of this function is to generate a numeric hash value that I can store in a database, and use that for a look up (rather than trying to index a keyword column).
The equivalent of this function in MySQL is as follows:
SELECT CONV(RIGHT(MD5('some keyword'), 16), 16, 10);
I've verified that the MD5 string is the same, and the substr() matches the value I get back from RIGHT() in the MySQL query. However, when I run CONV(), I'm getting a different value to what's generated from base_convert().
For example, using keyword_hash("some keyword") generates a value of 10923672322315740844. However, using SELECT CONV(RIGHT(MD5('some keyword'), 16), 16, 10) generates 10923672322315740475, which shows the last three numbers as being different.
What am I missing here? Shouldn't they produce the same value?
I took a look at the PHP Manual page for base_convert(). There is the following warning:
base_convert() may lose precision on large numbers due to properties related
to the internal "double" or "float" type used. Please see the
Floating point numbers section in the manual for more
specific information and limitations.
Later in the comments someone already found a solution for this problem (thanks #CraigSefton):
function str_baseconvert($str, $frombase=10, $tobase=36) {
$str = trim($str);
if (intval($frombase) != 10) {
$len = strlen($str);
$q = 0;
for ($i=0; $i<$len; $i++) {
$r = base_convert($str[$i], $frombase, 10);
$q = bcadd(bcmul($q, $frombase), $r);
}
}
else $q = $str;
if (intval($tobase) != 10) {
$s = '';
while (bccomp($q, '0', 0) > 0) {
$r = intval(bcmod($q, $tobase));
$s = base_convert($r, 10, $tobase) . $s;
$q = bcdiv($q, $tobase, 0);
}
}
else $s = $q;
return $s;
}
This function uses the bc math library which supports arbitrary precision mathematics because it uses strings to store the numbers instead of integers/floats etc.

Converting long int to string in PHP

I'm using FMS along with PHP and I need the client's ID in order to disconnect some user at some point. So, I retrieve client's ID from FMS, but FMS sends the ID as a long int, such as 4702111234508538223.
Here's my problem; I need to convert this number to something like oAACAAAA in PHP. Is there any short way or some kind of library exists to doing this? Otherwise I have to convert this AS3 library into PHP.
This function converts something like "4702111234525315439" into something like "oAADAAAA":
function convert_id_to_str($id)
{
if (strspn($id, '0123456789') != strlen($id)) {
return false;
}
$str = '';
if (PHP_INT_SIZE >= 8) {
while ($id) {
$str .= chr($id & 255);
$id >>= 8;
}
} else {
while ($id) {
$str .= chr(bcmod($id, '256'));
$id = bcdiv($id, '256', 0);
}
}
return $str;
}
You could use either BC Math or GMP PHP functionalities to be sure to handle 64-bit number on 32 and 64-bit PHP capable server, and then pack the result in a formatted string, e.g. :
$id = "4702111234508538223";
$hi = bcdiv($id, pow(2, 32));
$lo = bcsub($id, bcmul($hi, pow(2, 32)));
var_dump(pack("LL", $lo, $hi));
Returns string(8) "oAACAAAA".
The code is exploded but could easily be turned to a one-liner or function as well. The use of a big number extension ensure compatibility with 32-bits platforms but if you're sure the platform hosting your PHP interpreter have 64-bit capabilities you could just use
pack("LL", $id, $id / pow(2, 32));

Categories