aboutsummaryrefslogtreecommitdiff
path: root/lib/models/OERIdentity.php
blob: 71322e8101a2c362c9c292ee36defd3869b75aeb (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
41
42
<?php

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)
    {
        $rsa = new \phpseclib\Crypt\RSA();
        $rsa->loadKey($this['private_key']);
        return $rsa->sign($text);
    }

    public function verifySignature($text, $signature)
    {
        $rsa = new \phpseclib\Crypt\RSA();
        $rsa->loadKey($this['public_key']);
        return $rsa->verify($text, $signature);
    }

    public function cbCreateKeysIfNecessary()
    {
        if (!$this['public_key']) {
            $this->createKeys();
        }
    }

    protected function createKeys() {
        $rsa = new \phpseclib\Crypt\RSA();
        $keypair = $rsa->createKey(4096);
        $this['private_key'] = preg_replace("/\r/", "", $keypair['privatekey']);
        $this['public_key'] = preg_replace("/\r/", "", $keypair['publickey']);
    }
}