Encrypt::decode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Jaeger
4
 *
5
 * @copyright	Copyright (c) 2015-2016, mithra62
6
 * @link		http://jaeger-app.com
7
 * @version		1.0
8
 * @filesource 	./Encrypt.php
9
 */
10
namespace JaegerApp;
11
12
use phpseclib\Crypt\AES;
13
14
/**
15
 * Jaeger - Encryption Object
16
 *
17
 * Handles encrypting and decrypting items from and for storage
18
 *
19
 * @package Encrypt
20
 * @author Eric Lamb <[email protected]>
21
 */
22
class Encrypt
23
{
24
25
    /**
26
     * The encryption key/salt to use for the request
27
     * 
28
     * @var string
29
     */
30
    protected $key = 'MolliePosellIsLikeSoAmazingYouCantEvenLookWithoutYourHeadBlowingUp';
31
32
    /**
33
     * The encryption library we're using
34
     * 
35
     * @var \Crypt_AES
36
     */
37
    protected $api = null;
38
39
    /**
40
     * Sets the encryption key
41
     * 
42
     * @param string $key            
43
     * @return \mithra62\Encrypt
44
     */
45
    public function setKey($key)
46
    {
47
        if ($key != '') {
48
            $this->key = $key;
49
        }
50
        
51
        return $this;
52
    }
53
54
    /**
55
     * Returns the encryption key
56
     * 
57
     * @return \mithra62\string
58
     */
59
    public function getKey()
60
    {
61
        return $this->key;
62
    }
63
64
    /**
65
     * Encrypts a string
66
     * 
67
     * @param string $string
68
     *            The string to encrypt
69
     * @return string
70
     */
71
    public function encode($string)
72
    {
73
        return base64_encode($this->getApi()->encrypt($string));
74
    }
75
76
    /**
77
     * Decrypts a previously encrypted string
78
     * 
79
     * @param string $string
80
     *            The string to decrypt
81
     * @return string
82
     */
83
    public function decode($string)
84
    {
85
        return $this->getApi()->decrypt(base64_decode($string));
86
    }
87
88
    /**
89
     * Returns an instance of the Crypto library
90
     * 
91
     * @return Crypt_AES
92
     */
93
    public function getApi()
94
    {
95
        if (is_null($this->api)) {
96
            $this->api = new AES();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \phpseclib\Crypt\AES() of type object<phpseclib\Crypt\AES> is incompatible with the declared type object<Crypt_AES> of property $api.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
97
            $this->api->setKey($this->getKey());
98
        }
99
        
100
        return $this->api;
101
    }
102
    
103
    /**
104
     * Generates a Guid if able
105
     * @return string
106
     */
107
    public function guid()
108
    {
109
        if (function_exists('com_create_guid')) {
110
            return com_create_guid();
111
        } else {
112
            mt_srand((double) microtime() * 10000); // optional for php 4.2.0 and up.
113
            $charid = strtoupper(md5(uniqid(rand(), true) . $this->getKey()));
114
            $hyphen = chr(45); // "-"
115
            $uuid = substr($charid, 0, 8) . $hyphen . substr($charid, 8, 4) . $hyphen . substr($charid, 12, 4) . $hyphen . substr($charid, 16, 4) . $hyphen . substr($charid, 20, 12);
116
            return strtolower($uuid);
117
        }
118
    }    
119
}