blob: 8c627284c7ffb75e89864fb0ac1b809441e364f2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
<?php
use phpseclib3\Crypt\RSA;
abstract class OERIdentity extends SimpleORMap
{
/**
* configures this class
* @param array $config
*/
protected static function configure($config = [])
{
$config['registered_callbacks']['before_store'][] = "cbCreateKeysIfNecessary";
parent::configure($config);
}
public function createSignature($text)
{
return RSA::loadPrivateKey($this['private_key'])->sign($text);
}
public function verifySignature($text, $signature)
{
return RSA::loadPublicKey($this['public_key'])->verify($text, $signature);
}
public function cbCreateKeysIfNecessary()
{
if (!$this['public_key']) {
$this->createKeys();
}
}
protected function createKeys()
{
$keypair = RSA::createKey(4096);
$this['private_key'] = preg_replace("/\r/", "", $keypair['privatekey']);
$this['public_key'] = preg_replace("/\r/", "", $keypair['publickey']);
}
}
|