Passed
Push — master ( bc9222...b41766 )
by Rutger
12:36
created

CryptGPGTrait::signViaCryptGPG()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace rhertogh\Yii2SecurityTxt\helpers\GPG\traits;
4
5
use Crypt_GPG;
6
use Crypt_GPG_Exception;
7
use PEAR_Exception;
8
use Yii;
9
10
trait CryptGPGTrait
11
{
12
    /**
13
     * @throws Crypt_GPG_Exception
14
     * @throws PEAR_Exception
15
     */
16
    protected static function signViaCryptGPG(string $message, string $privateKey)
17
    {
18
        Yii::beginProfile('Generate PGP signature', __METHOD__);
19
        $gpg = new Crypt_GPG(static::cryptGpgDefaultOptions());
20
        $keyInfo = $gpg->importKey($privateKey);
21
        $gpg->addSignKey($keyInfo['fingerprint']);
22
        $output = $gpg->sign($message, Crypt_GPG::SIGN_MODE_CLEAR);
23
        Yii::endProfile('Generate PGP signature', __METHOD__);
24
        return $output;
25
    }
26
27
    protected static function cryptGpgDefaultOptions()
28
    {
29
        $gnupgHome = getenv('GNUPGHOME');
30
        return $gnupgHome ? ['homedir' => $gnupgHome] : [];
31
    }
32
}
33