Crypto::decrypt()   A
last analyzed

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
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Omnipay\SmartPay\Message;
4
class Crypto
5
{
6
    public function encrypt($plainText, $key) {
7
        $method = "AES-256-GCM";
8
        $initVector = openssl_random_pseudo_bytes(16);
9
        $openMode = openssl_encrypt( $plainText, $method, $key, OPENSSL_RAW_DATA, $initVector, $tag );
10
11
        return bin2hex( $initVector ).bin2hex( $openMode . $tag );
12
    }
13
14
    public function decrypt($encryptedText, $key) {
15
        $method = 'AES-256-GCM';
16
        $encryptedText = hex2bin( $encryptedText );
17
        $iv_len = $tag_length = 16;
18
        $iv = substr( $encryptedText, 0, $iv_len );
19
        $tag = substr( $encryptedText, -$tag_length, $iv_len );
20
        $ciphertext = substr( $encryptedText, $iv_len, -$tag_length );
21
22
        return openssl_decrypt( $ciphertext, $method, $key, OPENSSL_RAW_DATA, $iv, $tag );
23
    }
24
}