Passed
Push — master ( e9ba59...c2c890 )
by Tony Karavasilev (Тони
19:20
created

DigitalSignature::extractVerifiedData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * Cryptographic digital signature protocol.
5
 */
6
7
namespace CryptoManana\CryptographicProtocol;
8
9
use \CryptoManana\Core\Abstractions\Containers\AbstractCryptographicProtocol as CryptographicProtocol;
10
use \CryptoManana\Core\Abstractions\MessageEncryption\AbstractAsymmetricEncryptionAlgorithm as AsymmetricCipher;
11
use \CryptoManana\Core\Interfaces\MessageEncryption\DataSigningInterface as DataSigning;
12
use \CryptoManana\Core\Interfaces\Containers\SignatureStandardInjectableInterface as SignatureStandardSetter;
13
use \CryptoManana\Core\Interfaces\Containers\SignedDataInterface as SignedDataObjectProcessing;
14
use \CryptoManana\Core\Traits\Containers\SignatureStandardInjectableTrait as SignatureStandardSetterImplementation;
15
use \CryptoManana\DataStructures\SignedData as SignedDataStructure;
16
17
/**
18
 * Class DigitalSignature - The digital signature cryptographic protocol object.
19
 *
20
 * @package CryptoManana\CryptographicProtocol
21
 *
22
 * @mixin SignatureStandardSetterImplementation
23
 */
24
class DigitalSignature extends CryptographicProtocol implements SignatureStandardSetter, SignedDataObjectProcessing
25
{
26
    /**
27
     * The digital signature service dependency injection via a setter method implementation.
28
     *
29
     * {@internal Reusable implementation of `SignatureStandardInjectableInterface`. }}
30
     */
31
    use SignatureStandardSetterImplementation;
32
33
    /**
34
     * The digital signature service property storage.
35
     *
36
     * @var AsymmetricCipher|DataSigning|null The digital signature service.
37
     */
38
    protected $signatureSource = null;
39
40
    /**
41
     * Container constructor.
42
     *
43
     * @param AsymmetricCipher|DataSigning|null $signatureAlgorithm The digital signature service.
44
     *
45
     * @throws \Exception Initialization validation.
46
     */
47
    public function __construct(AsymmetricCipher $signatureAlgorithm = null)
48
    {
49
        if ($signatureAlgorithm instanceof DataSigning) {
50
            $this->signatureSource = $signatureAlgorithm;
51
        } else {
52
            throw new \RuntimeException('No digital signature standard service has been set.');
53
        }
54
    }
55
56
    /**
57
     * Container destructor.
58
     */
59
    public function __destruct()
60
    {
61
        unset($this->signatureSource);
62
    }
63
64
    /**
65
     * Container cloning via deep copy.
66
     */
67
    public function __clone()
68
    {
69
        $this->signatureSource = clone $this->signatureSource;
70
    }
71
72
    /**
73
     * Creates a signed data object for the given input data.
74
     *
75
     * @param string $plainData The plain input string.
76
     *
77
     * @return SignedDataStructure The signed data object.
78
     * @throws \Exception Validation errors.
79
     */
80
    public function createSignedData($plainData)
81
    {
82
        $signatureData = $this->signatureSource->signData($plainData);
2 ignored issues
show
Bug introduced by
The method signData() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
        /** @scrutinizer ignore-call */ 
83
        $signatureData = $this->signatureSource->signData($plainData);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method signData() does not exist on CryptoManana\Core\Abstra...tricEncryptionAlgorithm. It seems like you code against a sub-type of CryptoManana\Core\Abstra...tricEncryptionAlgorithm such as CryptoManana\Core\Abstra...on\AbstractDsaSignature. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
        /** @scrutinizer ignore-call */ 
83
        $signatureData = $this->signatureSource->signData($plainData);
Loading history...
83
84
        return new SignedDataStructure($plainData, $signatureData);
85
    }
86
87
    /**
88
     * Verifies and extracts the plain data from a signed data object.
89
     *
90
     * @param SignedDataStructure $signedData The signed data object.
91
     *
92
     * @return string The verified plain information.
93
     * @throws \Exception Validation errors.
94
     */
95
    public function extractVerifiedData(SignedDataStructure $signedData)
96
    {
97
        if (!$this->signatureSource->verifyDataSignature($signedData->signature, $signedData->data)) {
1 ignored issue
show
Bug introduced by
The method verifyDataSignature() does not exist on CryptoManana\Core\Abstra...tricEncryptionAlgorithm. It seems like you code against a sub-type of CryptoManana\Core\Abstra...tricEncryptionAlgorithm such as CryptoManana\Core\Abstra...on\AbstractDsaSignature. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
        if (!$this->signatureSource->/** @scrutinizer ignore-call */ verifyDataSignature($signedData->signature, $signedData->data)) {
Loading history...
98
            throw new \RuntimeException('Wrong signature, the data has been tampered with or defected.');
99
        }
100
101
        return $signedData->data;
102
    }
103
}
104