move base64_url_(encode|decode) to static functions in Magicsig

This commit is contained in:
James Walker 2010-03-26 13:37:46 -04:00
parent 9c63ae6e44
commit c6c4510192
2 changed files with 21 additions and 19 deletions

View File

@ -129,11 +129,11 @@ class Magicsig extends Memcached_DataObject
public function toString($full_pair = true) public function toString($full_pair = true)
{ {
$mod = base64_url_encode($this->publicKey->modulus->toBytes()); $mod = Magicsig::base64_url_encode($this->publicKey->modulus->toBytes());
$exp = base64_url_encode($this->publicKey->exponent->toBytes()); $exp = Magicsig::base64_url_encode($this->publicKey->exponent->toBytes());
$private_exp = ''; $private_exp = '';
if ($full_pair && $this->privateKey->exponent->toBytes()) { if ($full_pair && $this->privateKey->exponent->toBytes()) {
$private_exp = '.' . base64_url_encode($this->privateKey->exponent->toBytes()); $private_exp = '.' . Magicsig::base64_url_encode($this->privateKey->exponent->toBytes());
} }
return 'RSA.' . $mod . '.' . $exp . $private_exp; return 'RSA.' . $mod . '.' . $exp . $private_exp;
@ -174,9 +174,9 @@ class Magicsig extends Memcached_DataObject
$rsa = new Crypt_RSA(); $rsa = new Crypt_RSA();
$rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1; $rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1;
$rsa->setHash('sha256'); $rsa->setHash('sha256');
$rsa->modulus = new Math_BigInteger(base64_url_decode($mod), 256); $rsa->modulus = new Math_BigInteger(Magicsig::base64_url_decode($mod), 256);
$rsa->k = strlen($rsa->modulus->toBytes()); $rsa->k = strlen($rsa->modulus->toBytes());
$rsa->exponent = new Math_BigInteger(base64_url_decode($exp), 256); $rsa->exponent = new Math_BigInteger(Magicsig::base64_url_decode($exp), 256);
if ($type == 'private') { if ($type == 'private') {
$this->privateKey = $rsa; $this->privateKey = $rsa;
@ -203,23 +203,25 @@ class Magicsig extends Memcached_DataObject
public function sign($bytes) public function sign($bytes)
{ {
$sig = $this->privateKey->sign($bytes); $sig = $this->privateKey->sign($bytes);
return base64_url_encode($sig); return Magicsig::base64_url_encode($sig);
} }
public function verify($signed_bytes, $signature) public function verify($signed_bytes, $signature)
{ {
$signature = base64_url_decode($signature); $signature = Magicsig::base64_url_decode($signature);
return $this->publicKey->verify($signed_bytes, $signature); return $this->publicKey->verify($signed_bytes, $signature);
} }
public static function base64_url_encode($input)
{
return strtr(base64_encode($input), '+/', '-_');
}
public static function base64_url_decode($input)
{
return base64_decode(strtr($input, '-_', '+/'));
}
} }
function base64_url_encode($input)
{
return strtr(base64_encode($input), '+/', '-_');
}
function base64_url_decode($input)
{
return base64_decode(strtr($input, '-_', '+/'));
}

View File

@ -83,7 +83,7 @@ class MagicEnvelope
public function signMessage($text, $mimetype, $keypair) public function signMessage($text, $mimetype, $keypair)
{ {
$signature_alg = Magicsig::fromString($keypair); $signature_alg = Magicsig::fromString($keypair);
$armored_text = base64_url_encode($text); $armored_text = Magicsig::base64_url_encode($text);
return array( return array(
'data' => $armored_text, 'data' => $armored_text,
@ -121,7 +121,7 @@ class MagicEnvelope
public function unfold($env) public function unfold($env)
{ {
$dom = new DOMDocument(); $dom = new DOMDocument();
$dom->loadXML(base64_url_decode($env['data'])); $dom->loadXML(Magicsig::base64_url_decode($env['data']));
if ($dom->documentElement->tagName != 'entry') { if ($dom->documentElement->tagName != 'entry') {
return false; return false;
@ -178,7 +178,7 @@ class MagicEnvelope
return false; return false;
} }
$text = base64_url_decode($env['data']); $text = Magicsig::base64_url_decode($env['data']);
$signer_uri = $this->getAuthor($text); $signer_uri = $this->getAuthor($text);
try { try {