* @link http://status.net/
*/
+if (!defined('STATUSNET')) {
+ exit(1);
+}
+
require_once 'Crypt/RSA.php';
class Magicsig extends Memcached_DataObject
{
-
const PUBLICKEYREL = 'magic-public-key';
-
+
public $__table = 'magicsig';
public $user_id;
public $keypair;
public $alg;
-
+
public $publicKey;
public $privateKey;
-
+
public function __construct($alg = 'RSA-SHA256')
{
$this->alg = $alg;
}
-
+
public /*static*/ function staticGet($k, $v=null)
{
$obj = parent::staticGet(__CLASS__, $k, $v);
64, false));
}
-
function keys()
{
return array_keys($this->keyTypes());
public function generate($user_id)
{
$rsa = new Crypt_RSA();
-
+
$keypair = $rsa->createKey();
$rsa->loadKey($keypair['privatekey']);
$this->publicKey = new Crypt_RSA();
$this->publicKey->loadKey($keypair['publickey']);
-
+
$this->user_id = $user_id;
$this->insert();
}
-
public function toString($full_pair = true)
{
$mod = Magicsig::base64_url_encode($this->publicKey->modulus->toBytes());
$private_exp = '.' . Magicsig::base64_url_encode($this->privateKey->exponent->toBytes());
}
- return 'RSA.' . $mod . '.' . $exp . $private_exp;
+ return 'RSA.' . $mod . '.' . $exp . $private_exp;
}
-
+
public static function fromString($text)
{
$magic_sig = new Magicsig();
-
+
// remove whitespace
$text = preg_replace('/\s+/', '', $text);
if (!preg_match('/RSA\.([^\.]+)\.([^\.]+)(.([^\.]+))?/', $text, $matches)) {
return false;
}
-
+
$mod = $matches[1];
$exp = $matches[2];
if (!empty($matches[4])) {
$this->publicKey = $rsa;
}
}
-
+
public function getName()
{
return $this->alg;
case 'RSA-SHA256':
return 'sha256';
}
-
}
-
+
public function sign($bytes)
{
$sig = $this->privateKey->sign($bytes);
return base64_decode(strtr($input, '-_', '+/'));
}
}
-
-