php > $value = hexdec('B2A80498A3CEDC09');
php > echo dechex($value);
b2a80498a3cee000
这似乎行不通。可能是由于浮点转换。
我看到了 bcmath 的一种解决方案。
这是来自confusa(http://www.assembla.com/code/confusa/git/nodes/lib/ca/Certificate.php?rev=a80a040c97fde2c170bb290d756c6729883fe80a):
/*
* PHP will return the serial as an integer, whereas
* everybody else use the hex-represenatation of the
* number.
*
* Due to the fact that Comodo uses *insanely* large
* serial-numbers, we need to be a bit creative when we
* get the serial as PHP won't cope with numbers larger
* than MAX_INT (2**32 on 32 bits arch)
*/
$serial = $this->x509_parsed['serialNumber'] . "";
$base = bcpow("2", "32");
$counter = 100;
$res = "";
$val = $serial;
while($counter > 0 && $val > 0) {
$counter = $counter - 1;
$tmpres = dechex(bcmod($val, $base)) . "";
/* adjust for 0's */
for ($i = 8-strlen($tmpres); $i > 0; $i = $i-1) {
$tmpres = "0$tmpres";
}
$res = $tmpres .$res;
$val = bcdiv($val, $base);
}
if ($counter <= 0) {
return false;
}
return strtoupper($res);
我没有启用 bcmath,所以目前无法对其进行测试。