summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortailor <cygnus@janrain.com>2005-12-30 00:57:30 +0000
committertailor <cygnus@janrain.com>2005-12-30 00:57:30 +0000
commit67b85c935df7914d01d339944bea84739afb2f5c (patch)
tree4d06badb9fafb55a671707b2c56fbfd428704c22
parent16290783af6055a5f779e042f9abead4c2770c09 (diff)
downloadphp-openid-67b85c935df7914d01d339944bea84739afb2f5c.zip
php-openid-67b85c935df7914d01d339944bea84739afb2f5c.tar.gz
php-openid-67b85c935df7914d01d339944bea84739afb2f5c.tar.bz2
[project @ Implemented longToBinary and binaryToLong functions]
-rw-r--r--Net/OpenID/CryptUtil.php55
1 files changed, 53 insertions, 2 deletions
diff --git a/Net/OpenID/CryptUtil.php b/Net/OpenID/CryptUtil.php
index 2c75e20..e2ca995 100644
--- a/Net/OpenID/CryptUtil.php
+++ b/Net/OpenID/CryptUtil.php
@@ -114,11 +114,62 @@ class Net_OpenID_CryptUtil {
}
function longToBinary($long) {
- return pack("L", $long);
+
+ $lib =& Net_OpenID_MathLibrary::getLibWrapper();
+
+ if ($lib->cmp($long, 0) < 0) {
+ print "numToBytes takes only positive integers.";
+ return null;
+ }
+
+ if ($long == 0) {
+ return "\x00";
+ }
+
+ $bytes = array();
+
+ while ($long) {
+ array_unshift($bytes, $lib->mod($long, 256));
+ $long = $lib->div($long, pow(2, 8));
+ }
+
+ if ($bytes && ($bytes[0] > 127)) {
+ array_unshift($bytes, 0);
+ }
+
+ $string = '';
+ foreach ($bytes as $byte) {
+ $string .= pack('C', $byte);
+ }
+
+ return $string;
}
function binaryToLong($str) {
- return unpack($str, "L");
+
+ $lib =& Net_OpenID_MathLibrary::getLibWrapper();
+
+ if ($str === null) {
+ return null;
+ }
+
+ // Use array_merge to return a zero-indexed array instead of a
+ // one-indexed array.
+ $bytes = array_merge(unpack('C*', $str));
+
+ $n = $lib->init(0);
+
+ if ($bytes && ($bytes[0] > 127)) {
+ print "bytesToNum works only for positive integers.";
+ return null;
+ }
+
+ foreach ($bytes as $byte) {
+ $n = $lib->mul($n, pow(2, 8));
+ $n = $lib->add($n, $byte);
+ }
+
+ return $n;
}
function base64ToLong($str) {