Passed
Push — master ( 765d06...d035c7 )
by Joni
02:39
created

Crypto   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 1
dl 0
loc 67
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
sign() 0 2 ?
verify() 0 2 ?
encrypt() 0 1 ?
decrypt() 0 1 ?
A getDefault() 0 9 2
1
<?php
2
3
namespace Sop\CryptoBridge;
4
5
use Sop\CryptoTypes\AlgorithmIdentifier\Cipher\CipherAlgorithmIdentifier;
6
use Sop\CryptoTypes\AlgorithmIdentifier\Feature\SignatureAlgorithmIdentifier;
7
use Sop\CryptoTypes\Asymmetric\PrivateKeyInfo;
8
use Sop\CryptoTypes\Asymmetric\PublicKeyInfo;
9
use Sop\CryptoTypes\Signature\Signature;
10
11
/**
12
 * Base class for crypto engine implementations.
13
 */
14
abstract class Crypto
15
{
16
    /**
17
     * Sign data with given algorithm using given private key.
18
     *
19
     * @param string $data Data to sign
20
     * @param PrivateKeyInfo $privkey_info Private key
21
     * @param SignatureAlgorithmIdentifier $algo Signature algorithm
22
     * @return Signature
23
     */
24
    abstract public function sign($data, PrivateKeyInfo $privkey_info,
25
        SignatureAlgorithmIdentifier $algo);
26
    
27
    /**
28
     * Verify signature with given algorithm using given public key.
29
     *
30
     * @param string $data Data to verify
31
     * @param Signature $signature Signature
32
     * @param PublicKeyInfo $pubkey_info Public key
33
     * @param SignatureAlgorithmIdentifier $algo Signature algorithm
34
     * @return bool True if signature matches
35
     */
36
    abstract public function verify($data, Signature $signature,
37
        PublicKeyInfo $pubkey_info, SignatureAlgorithmIdentifier $algo);
38
    
39
    /**
40
     * Encrypt data with given algorithm using given key.
41
     *
42
     * Padding must be added by the caller. Initialization vector is
43
     * taken from the algorithm identifier if available.
44
     *
45
     * @param string $data Plaintext
46
     * @param string $key Encryption key
47
     * @param CipherAlgorithmIdentifier $algo Encryption algorithm
48
     * @return string Ciphertext
49
     */
50
    abstract public function encrypt($data, $key, CipherAlgorithmIdentifier $algo);
51
    
52
    /**
53
     * Decrypt data with given algorithm using given key.
54
     *
55
     * Possible padding is not removed and must be handled by the caller.
56
     * Initialization vector is taken from the algorithm identifier if
57
     * available.
58
     *
59
     * @param string $data Ciphertext
60
     * @param string $key Encryption key
61
     * @param CipherAlgorithmIdentifier $algo Encryption algorithm
62
     * @return string Plaintext
63
     */
64
    abstract public function decrypt($data, $key, CipherAlgorithmIdentifier $algo);
65
    
66
    /**
67
     * Get default supported crypto implementation.
68
     *
69
     * @return self
70
     */
71 1
    public static function getDefault()
72
    {
73 1
        if (defined("OPENSSL_VERSION_NUMBER")) {
74 1
            return new Crypto\OpenSSLCrypto();
75
        }
76
        // @codeCoverageIgnoreStart
77
        throw new \RuntimeException("No crypto implementation available.");
78
        // @codeCoverageIgnoreEnd
79
    }
80
}
81