3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * A sample module to show best practices for StatusNet plugins
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author James Walker <james@status.net>
25 * @copyright 2010 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
27 * @link http://status.net/
30 require_once 'Crypt/RSA.php';
37 public function __construct($init = null)
42 $this->fromString($init);
47 public function generate($key_length = 512)
49 $keypair = new Crypt_RSA_KeyPair($key_length);
50 $params['public_key'] = $keypair->getPublicKey();
51 $params['private_key'] = $keypair->getPrivateKey();
53 PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
54 $this->keypair = new Crypt_RSA($params);
55 PEAR::popErrorHandling();
59 public function toString($full_pair = true)
61 $public_key = $this->keypair->_public_key;
62 $private_key = $this->keypair->_private_key;
64 $mod = base64_url_encode($public_key->getModulus());
65 $exp = base64_url_encode($public_key->getExponent());
67 if ($full_pair && $private_key->getExponent()) {
68 $private_exp = '.' . base64_url_encode($private_key->getExponent());
71 return 'RSA.' . $mod . '.' . $exp . $private_exp;
74 public function fromString($text)
76 PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
79 $text = preg_replace('/\s+/', '', $text);
82 if (!preg_match('/RSA\.([^\.]+)\.([^\.]+)(.([^\.]+))?/', $text, $matches)) {
86 $mod = base64_url_decode($matches[1]);
87 $exp = base64_url_decode($matches[2]);
89 $private_exp = base64_url_decode($matches[4]);
92 $params['public_key'] = new Crypt_RSA_KEY($mod, $exp, 'public');
93 if ($params['public_key']->isError()) {
94 $error = $params['public_key']->getLastError();
95 print $error->getMessage();
99 $params['private_key'] = new Crypt_RSA_KEY($mod, $private_exp, 'private');
100 if ($params['private_key']->isError()) {
101 $error = $params['private_key']->getLastError();
102 print $error->getMessage();
107 $this->keypair = new Crypt_RSA($params);
108 PEAR::popErrorHandling();
111 public function getName()
116 public function sign($bytes)
118 $sig = $this->keypair->createSign($bytes, null, 'sha256');
119 if ($this->keypair->isError()) {
120 $error = $this->keypair->getLastError();
121 common_log(LOG_DEBUG, 'RSA Error: '. $error->getMessage());
127 public function verify($signed_bytes, $signature)
129 $result = $this->keypair->validateSign($signed_bytes, $signature, null, 'sha256');
130 if ($this->keypair->isError()) {
131 $error = $this->keypair->getLastError();
132 //common_log(LOG_DEBUG, 'RSA Error: '. $error->getMessage());
133 print $error->getMessage();
140 // Define a sha256 function for hashing
141 // (Crypt_RSA should really be updated to use hash() )
142 function sha256($bytes)
144 return hash('sha256', $bytes);
147 function base64_url_encode($input)
149 return strtr(base64_encode($input), '+/', '-_');
152 function base64_url_decode($input)
154 return base64_decode(strtr($input, '-_', '+/'));