Replaced hex encoding with base64
Replaced hex encoding with base64

Makes your constant strings shorter and solves compatibility issues with
older PHP versions.

--- a/src/php/token.php
+++ b/src/php/token.php
@@ -1,27 +1,14 @@
 <?php
 require_once 'func.php';
 
-if ( !function_exists( 'hex2bin' ) ) {
-    function hex2bin( $str ) {
-        $sbin = "";
-        $len = strlen( $str );
-        for ( $i = 0; $i < $len; $i += 2 ) {
-            $sbin .= pack( "H*", substr( $str, $i, 2 ) );
-        }
-
-        return $sbin;
-    }
-}
-
 function generateRequestToken($country, $phone) {
-	$waString = "53160F52030A44C28310C282C29AC28DC28C463A2169295741";
-	$noMediaHash = "0001A9339CEF0E7172AEC99E99F0044DCC3F90F58C45C0A85BDEAB054DA9A61B4438258D5614A9F105FA";
-	$waPrefix = "636F6D2E7768617473617070";
-	$signature = "30820332308202F0A00302010202044C2536A4300B06072A8648CE3804030500307C310B3009060355040613025553311330110603550408130A43616C69666F726E6961311430120603550407130B53616E746120436C61726131163014060355040A130D576861747341707020496E632E31143012060355040B130B456E67696E656572696E67311430120603550403130B427269616E204163746F6E301E170D3130303632353233303731365A170D3434303231353233303731365A307C310B3009060355040613025553311330110603550408130A43616C69666F726E6961311430120603550407130B53616E746120436C61726131163014060355040A130D576861747341707020496E632E31143012060355040B130B456E67696E656572696E67311430120603550403130B427269616E204163746F6E308201B83082012C06072A8648CE3804013082011F02818100FD7F53811D75122952DF4A9C2EECE4E7F611B7523CEF4400C31E3F80B6512669455D402251FB593D8D58FABFC5F5BA30F6CB9B556CD7813B801D346FF26660B76B9950A5A49F9FE8047B1022C24FBBA9D7FEB7C61BF83B57E7C6A8A6150F04FB83F6D3C51EC3023554135A169132F675F3AE2B61D72AEFF22203199DD14801C70215009760508F15230BCCB292B982A2EB840BF0581CF502818100F7E1A085D69B3DDECBBCAB5C36B857B97994AFBBFA3AEA82F9574C0B3D0782675159578EBAD4594FE67107108180B449167123E84C281613B7CF09328CC8A6E13C167A8B547C8D28E0A3AE1E2BB3A675916EA37F0BFA213562F1FB627A01243BCCA4F1BEA8519089A883DFE15AE59F06928B665E807B552564014C3BFECF492A0381850002818100D1198B4B81687BCF246D41A8A725F0A989A51BCE326E84C828E1F556648BD71DA487054D6DE70FFF4B49432B6862AA48FC2A93161B2C15A2FF5E671672DFB576E9D12AAFF7369B9A99D04FB29D2BBBB2A503EE41B1FF37887064F41FE2805609063500A8E547349282D15981CDB58A08BEDE51DD7E9867295B3DFB45FFC6B259300B06072A8648CE3804030500032F00302C021400A602A7477ACF841077237BE090DF436582CA2F0214350CE0268D07E71E55774AB4EACD4D071CD1EFAD";
-	$classesMd5 = "2f8c600d1b5d497aee2adf672abb87b7";
-	$k = "3E44F029266A51F014C91D2B3D0F21609D3036C410DDD5B5FB74829F24D721F100C714BBE45C240DFE3BC0DBFF73CA4FDE9D065CA47A38E426872111C31EF87F0D51612535D08E2BD60C815436D1278D2989D8CCE351B5235A0D";
+	$waString = "UxYPUgMKRMKDEMKCwprCjcKMRjohaSlXQQ==";
+	$noMediaHash = "AAGpM5zvDnFyrsmemfAETcw/kPWMRcCoW96rBU2pphtEOCWNVhSp8QX6";
+	$waPrefix = "Y29tLndoYXRzYXBw";
+	$signature = "MIIDMjCCAvCgAwIBAgIETCU2pDALBgcqhkjOOAQDBQAwfDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFDASBgNVBAcTC1NhbnRhIENsYXJhMRYwFAYDVQQKEw1XaGF0c0FwcCBJbmMuMRQwEgYDVQQLEwtFbmdpbmVlcmluZzEUMBIGA1UEAxMLQnJpYW4gQWN0b24wHhcNMTAwNjI1MjMwNzE2WhcNNDQwMjE1MjMwNzE2WjB8MQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEUMBIGA1UEBxMLU2FudGEgQ2xhcmExFjAUBgNVBAoTDVdoYXRzQXBwIEluYy4xFDASBgNVBAsTC0VuZ2luZWVyaW5nMRQwEgYDVQQDEwtCcmlhbiBBY3RvbjCCAbgwggEsBgcqhkjOOAQBMIIBHwKBgQD9f1OBHXUSKVLfSpwu7OTn9hG3UjzvRADDHj+AtlEmaUVdQCJR+1k9jVj6v8X1ujD2y5tVbNeBO4AdNG/yZmC3a5lQpaSfn+gEexAiwk+7qdf+t8Yb+DtX58aophUPBPuD9tPFHsMCNVQTWhaRMvZ1864rYdcq7/IiAxmd0UgBxwIVAJdgUI8VIwvMspK5gqLrhAvwWBz1AoGBAPfhoIXWmz3ey7yrXDa4V7l5lK+7+jrqgvlXTAs9B4JnUVlXjrrUWU/mcQcQgYC0SRZxI+hMKBYTt88JMozIpuE8FnqLVHyNKOCjrh4rs6Z1kW6jfwv6ITVi8ftiegEkO8yk8b6oUZCJqIPf4VrlnwaSi2ZegHtVJWQBTDv+z0kqA4GFAAKBgQDRGYtLgWh7zyRtQainJfCpiaUbzjJuhMgo4fVWZIvXHaSHBU1t5w//S0lDK2hiqkj8KpMWGywVov9eZxZy37V26dEqr/c2m5qZ0E+ynSu7sqUD7kGx/zeIcGT0H+KAVgkGNQCo5Uc0koLRWYHNtYoIvt5R3X6YZylbPftF/8ayWTALBgcqhkjOOAQDBQADLwAwLAIUAKYCp0d6z4QQdyN74JDfQ2WCyi8CFDUM4CaNB+ceVXdKtOrNTQcc0e+t";
+	$classesMd5 = "L4xgDRtdSXruKt9nKruHtw==";
+	$k = "PkTwKSZqUfAUyR0rPQ8hYJ0wNsQQ3dW1+3SCnyTXIfEAxxS75FwkDf47wNv/c8pP3p0GXKR6OOQmhyERwx74fw1RYSU10I4r1gyBVDbRJ40pidjM41G1I1oN";
 	$KEY = "The piano has been drinking";
-
 
 	//TODO: This phone prefix split XXX-ZZZZZ... is ok for +34 numbers, but needs to be checked
 	//      for other countries
@@ -29,11 +16,11 @@
 	$phone2 = substr($phone, 3);
 
 	// This AES secret is not really needed right now
-	$id = hex2bin($waString) . $country . $phone2;
-	$salt = substr(hex2bin($noMediaHash),2,4);
+	$id = base64_decode($waString) . $country . $phone2;
+	$salt = substr(base64_decode($noMediaHash),2,4);
 	$key = pbkdf2('sha1', $id, $salt, 16, 16, true);
-	$iv = substr(hex2bin($noMediaHash),6,16);
-	$data = substr(hex2bin($noMediaHash),22);
+	$iv = substr(base64_decode($noMediaHash),6,16);
+	$data = substr(base64_decode($noMediaHash),22);
 	$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', 'nofb', '');
 	mcrypt_generic_init($td, $key, $iv);
 	$aes_secret = mcrypt_generic($td, $data);
@@ -51,10 +38,10 @@
         	}
 	}
 
-	$d = hex2bin($waPrefix) . $f;
-	$key2 = pbkdf2('sha1', $d, hex2bin($k), 128, 80, true);
+	$d = base64_decode($waPrefix) . $f;
+	$key2 = pbkdf2('sha1', $d, base64_decode($k), 128, 80, true);
 
-	$data = hex2bin($signature . $classesMd5) . $phone1 . $phone2;
+	$data = base64_decode($signature) . base64_decode($classesMd5) . $phone;
 
 	$opad = str_repeat(chr(0x5C), 64);
 	$ipad = str_repeat(chr(0x36), 64);
@@ -65,8 +52,8 @@
 
 	$pack = 'H'.strlen(hash("sha1", 'lalala'));
 
-	$output = hash("sha1", $opad.pack($pack, hash("sha1", $ipad.$data)));
+	$output = hash("sha1", $opad.pack($pack, hash("sha1", $ipad.$data)), true);
 
-	return base64_encode(hex2bin($output));
+	return base64_encode($output);
 }