Passed
Push — master ( 22c4a0...baf55d )
by
unknown
03:13
created

Crypto   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 19
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A encrypt() 0 6 1
A decrypt() 0 9 1
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
}