summaryrefslogtreecommitdiffstats
path: root/Net
diff options
context:
space:
mode:
authortailor <cygnus@janrain.com>2005-12-28 21:52:09 +0000
committertailor <cygnus@janrain.com>2005-12-28 21:52:09 +0000
commit3319724398a82a37f785d6b8ba1783a630a08cb2 (patch)
tree342c9343fa09ed6e04f6e39b14319037e0e5ebf5 /Net
parent3594ce31eab937a9745c93e45ec341b3e7bc27d0 (diff)
downloadphp-openid-3319724398a82a37f785d6b8ba1783a630a08cb2.zip
php-openid-3319724398a82a37f785d6b8ba1783a630a08cb2.tar.gz
php-openid-3319724398a82a37f785d6b8ba1783a630a08cb2.tar.bz2
[project @ Ported CryptUtils functions and added some tests for CryptUtils.]
Diffstat (limited to 'Net')
-rw-r--r--Net/OpenID/CryptUtil.php130
1 files changed, 130 insertions, 0 deletions
diff --git a/Net/OpenID/CryptUtil.php b/Net/OpenID/CryptUtil.php
index f26ca72..9214cd9 100644
--- a/Net/OpenID/CryptUtil.php
+++ b/Net/OpenID/CryptUtil.php
@@ -8,6 +8,10 @@ if (!defined('Net_OpenID_RAND_SOURCE')) {
define('Net_OpenID_RAND_SOURCE', '/dev/urandom');
}
+require('HMACSHA1.php');
+
+$Net_OpenID_CryptUtil_duplicate_cache = array();
+
/**
* Cryptographic utility functions
*/
@@ -45,6 +49,132 @@ class Net_OpenID_CryptUtil {
}
return $bytes;
}
+
+ /**
+ * Computes the SHA1 hash.
+ *
+ * @param string $str The input string.
+ * @static
+ * @return string The resulting SHA1 hash.
+ */
+ function sha1($str) {
+ return sha1($str, true);
+ }
+
+ /**
+ * Computes an HMAC-SHA1 digest.
+ */
+ function hmacSha1($key, $text) {
+ return Net_OpenID_HMACSHA1($key, $text);
+ }
+
+ function fromBase64($str) {
+ return base64_decode($str);
+ }
+
+ function toBase64($str) {
+ return base64_encode($str);
+ }
+
+ function longToBinary($long) {
+ return pack("L", $long);
+ }
+
+ function binaryToLong($str) {
+ return unpack($str, "L");
+ }
+
+ function base64ToLong($str) {
+ return Net_OpenID_CryptUtil::binaryToLong(Net_OpenID_CryptUtil::fromBase64($str));
+ }
+
+ function longToBase64($long) {
+ return Net_OpenID_CryptUtil::toBase64(Net_OpenID_CryptUtil::longToBinary($long));
+ }
+
+ function strxor($x, $y) {
+ if (strlen($x) != strlen($y)) {
+ return null;
+ }
+
+ $str = "";
+ for ($i = 0; $i < strlen($x); $i++) {
+ $str .= chr(ord($x[$i]) ^ ord($y[$i]));
+ }
+
+ return $str;
+ }
+
+ function reversed($list) {
+ if (is_string($list)) {
+ return strrev($list);
+ } else if (is_array($list)) {
+ return array_reverse($list);
+ } else {
+ return null;
+ }
+ }
+
+ function randrange($start, $stop = null, $step = 1) {
+
+ global $Net_OpenID_CryptUtil_duplicate_cache;
+
+ if ($stop == null) {
+ $stop = $start;
+ $start = 0;
+ }
+
+ $r = ($stop - $start);
+
+ if (array_key_exists($r, $Net_OpenID_CryptUtil_duplicate_cache)) {
+ list($duplicate, $nbytes) = $Net_OpenID_CryptUtil_duplicate_cache[$r];
+ } else {
+ $rbytes = Net_OpenID_CryptUtil::longToBinary($r);
+ if ($rbytes[0] == '\x00') {
+ $nbytes = strlen($rbytes) - 1;
+ } else {
+ $nbytes = strlen($rbytes);
+ }
+
+ $mxrand = (pow(256, $nbytes));
+
+ // If we get a number less than this, then it is in the
+ // duplicated range.
+ $duplicate = $mxrand % $r;
+
+ if (count($Net_OpenID_CryptUtil_duplicate_cache) > 10) {
+ $Net_OpenID_CryptUtil_duplicate_cache = array();
+ }
+
+ $Net_OpenID_CryptUtil_duplicate_cache[$r] = array($duplicate, $nbytes);
+ }
+
+ while (1) {
+ $bytes = '\x00' + Net_OpenID_CryptUtil::getBytes($nbytes);
+ $n = Net_OpenID_CryptUtil::binaryToLong($bytes);
+ // Keep looping if this value is in the low duplicated range
+ if ($n >= $duplicate) {
+ break;
+ }
+ }
+ return $start + ($n % $r) * $step;
+ }
+
+ /**
+ * Produce a string of length random bytes, chosen from chrs.
+ */
+ function randomString($length, $chrs = null) {
+ if ($chrs == null) {
+ return getBytes($length);
+ } else {
+ $n = strlen($chrs);
+ $str = "";
+ for ($i = 0; $i < $length; $i++) {
+ $str .= $chrs[Net_OpenID_CryptUtil::randrange($n)];
+ }
+ return $str;
+ }
+ }
}
?> \ No newline at end of file