• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Smoren / encryption-tools-php / 6638087893

25 Oct 2023 08:59AM UTC coverage: 69.061% (-13.6%) from 82.677%
6638087893

push

github

Smoren
minor changes

125 of 181 relevant lines covered (69.06%)

2.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

65.22
/src/Helpers/SymmetricEncryptionHelper.php
1
<?php
2

3
namespace Smoren\EncryptionTools\Helpers;
4

5
use Smoren\EncryptionTools\Exceptions\JsonException;
6
use Smoren\EncryptionTools\Exceptions\SymmetricEncryptionException;
7

8
/**
9
 * Class SymmetricEncryptionHelper
10
 * @author Smoren <ofigate@gmail.com>
11
 */
12
class SymmetricEncryptionHelper
13
{
14
    /**
15
     * Returns data encrypted by secret key
16
     * @param mixed $data data to encrypt
17
     * @param string $secretKey secret key
18
     * @param string $cipherMethod encryption method
19
     * @return string encrypted data
20
     * @throws SymmetricEncryptionException
21
     * @throws JsonException
22
     */
23
    public static function encrypt($data, string $secretKey, string $cipherMethod = 'aes-256-cbc'): string
24
    {
25
        static::checkCipherMethodAvailable($cipherMethod);
5✔
26
        $data = JsonHelper::encode($data);
5✔
27

28
        $ivLen = openssl_cipher_iv_length($cipherMethod);
5✔
29
        if($ivLen === false) {
5✔
30
            throw new SymmetricEncryptionException(
×
31
                'openssl_cipher_iv_length() returned false',
×
32
                SymmetricEncryptionException::OPENSSL_ERROR
×
33
            );
×
34
        }
35
        $iv = openssl_random_pseudo_bytes($ivLen);
5✔
36
        if(!$iv) {
5✔
37
            throw new SymmetricEncryptionException(
×
38
                'openssl_random_pseudo_bytes() returned false',
×
39
                SymmetricEncryptionException::OPENSSL_ERROR
×
40
            );
×
41
        }
42
        $cipherText = openssl_encrypt($data, $cipherMethod, $secretKey, OPENSSL_RAW_DATA, $iv);
5✔
43
        if($cipherText === false) {
5✔
44
            throw new SymmetricEncryptionException(
×
45
                'openssl_encrypt() returned false',
×
46
                SymmetricEncryptionException::OPENSSL_ERROR
×
47
            );
×
48
        }
49
        $hmac = hash_hmac('sha256', $cipherText, $secretKey, true);
5✔
50

51
        return base64_encode($iv.$hmac.$cipherText);
5✔
52
    }
53

54
    /**
55
     * Returns data decrypted by secret key
56
     * @param string $encryptedData data to decrypt
57
     * @param string $secretKey secret key
58
     * @param string $cipherMethod encryption method
59
     * @return mixed decrypted data
60
     * @throws SymmetricEncryptionException
61
     */
62
    public static function decrypt(string $encryptedData, string $secretKey, string $cipherMethod = 'aes-256-cbc')
63
    {
64
        static::checkCipherMethodAvailable($cipherMethod);
5✔
65

66
        $c = base64_decode($encryptedData);
5✔
67
        $ivLen = openssl_cipher_iv_length($cipherMethod);
5✔
68
        if($ivLen === false) {
5✔
69
            throw new SymmetricEncryptionException(
×
70
                'openssl_cipher_iv_length() returned false',
×
71
                SymmetricEncryptionException::OPENSSL_ERROR
×
72
            );
×
73
        }
74
        $iv = substr($c, 0, $ivLen);
5✔
75
        $hmac = substr($c, $ivLen, $sha2len=32);
5✔
76
        $cipherText = substr($c, $ivLen+$sha2len);
5✔
77

78
        $data = openssl_decrypt($cipherText, $cipherMethod, $secretKey, OPENSSL_RAW_DATA, $iv);
5✔
79

80
        if($data === false) {
5✔
81
            throw new SymmetricEncryptionException(
1✔
82
                'incorrect secret key',
1✔
83
                SymmetricEncryptionException::CANNOT_DECRYPT
1✔
84
            );
1✔
85
        }
86
        return json_decode($data, true);
5✔
87
    }
88

89
    /**
90
     * Returns list of available encryption methods
91
     * @return array<string>
92
     */
93
    public static function getCipherMethodList(): array
94
    {
95
        return openssl_get_cipher_methods();
5✔
96
    }
97

98
    /**
99
     * Checks if encryption method available
100
     * @param string $cipherMethod encryption method
101
     * @throws SymmetricEncryptionException if method is unavailable
102
     */
103
    public static function checkCipherMethodAvailable(string $cipherMethod): void
104
    {
105
        if(!in_array($cipherMethod, static::getCipherMethodList(), true)) {
5✔
106
            throw new SymmetricEncryptionException(
1✔
107
                "unknown cipher method '{$cipherMethod}'",
1✔
108
                SymmetricEncryptionException::UNKNOWN_METHOD
1✔
109
            );
1✔
110
        }
111
    }
112
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc