Completed
Branch master (2ce3bf)
by Christoper
01:58
created

EncryptedFile::serialize()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6667
cc 2
eloc 6
nc 1
nop 0
1
<?php namespace Wubbajack\Encryption;
2
3
/**
4
 * Encrypted File class
5
 *
6
 * Contains information about an encrypted file:
7
 *  - The IV used for encryption
8
 *  - The amount of padding encryption has added to the file
9
 *  - A SHA1 checksum of the original file
10
 *  - A reference to the encrypted file, using \SplFileInfo
11
 *
12
 * @author  Chris Stolk <[email protected]>
13
 * @package Wubbajack\Encryption
14
 * @license MIT <https://opensource.org/licenses/MIT>
15
 * @since   0.0.1
16
 */
17
class EncryptedFile implements \Serializable
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $iv;
23
24
    /**
25
     * @var string
26
     */
27
    protected $checksum;
28
29
    /**
30
     * @var \SplFileInfo
31
     */
32
    protected $file;
33
34
    /**
35
     * @var int
36
     */
37
    protected $padding = 0;
38
39
    /**
40
     * EncryptedFile constructor.
41
     *
42
     * @param string       $iv
43
     * @param string       $checksum
44
     * @param \SplFileInfo $file
45
     */
46
    public function __construct($iv, $checksum, \SplFileInfo $file)
47
    {
48
        $this->iv       = $iv;
49
        $this->file     = $file;
50
        $this->checksum = $checksum;
51
    }
52
53
    /**
54
     * Creates a new encrypted file object
55
     *
56
     * @param string              $iv
57
     * @param string              $checksum
58
     * @param int                 $padding
59
     * @param \SplFileInfo|string $file
60
     *
61
     * @return static
62
     */
63
    public static function create($iv, $checksum, $padding, $file)
64
    {
65
        if (!($file instanceof \SplFileInfo)) {
66
            $file = new \SplFileInfo($file);
67
        }
68
69
        // Returns the encrypted file
70
        $encryptedFile = new static($iv, $checksum, $file);
71
        return $encryptedFile->setPadding($padding);
72
    }
73
74
    /**
75
     * Returns the IV
76
     *
77
     * @return string
78
     */
79
    public function getIv()
80
    {
81
        return $this->iv;
82
    }
83
84
    /**
85
     * Returns the checksum of the encrypted file
86
     *
87
     * @return string
88
     */
89
    public function getChecksum()
90
    {
91
        return $this->checksum;
92
    }
93
94
    /**
95
     * Returns the encrypted file
96
     *
97
     * @return \SplFileInfo
98
     */
99
    public function getFile()
100
    {
101
        return $this->file;
102
    }
103
104
    /**
105
     * Returns the amount of bytes that have been padded
106
     *
107
     * @return int
108
     */
109
    public function getPadding()
110
    {
111
        return $this->padding;
112
    }
113
114
    /**
115
     * Sets the padding
116
     *
117
     * @param int$padding
118
     * @return $this
119
     */
120
    public function setPadding($padding)
121
    {
122
        $this->padding = (int)$padding;
123
124
        return $this;
125
    }
126
127
    /**
128
     * String representation of object
129
     *
130
     * @link  http://php.net/manual/en/serializable.serialize.php
131
     * @return string the string representation of the object or null
132
     * @since 5.1.0
133
     */
134
    public function serialize()
135
    {
136
        return serialize([
137
            'iv'       => $this->iv,
138
            'checksum' => $this->checksum,
139
            'padding'  => $this->padding,
140
            'file'     => $this->file instanceof \SplFileInfo ? $this->file->getRealPath() : null
141
        ]);
142
    }
143
144
    /**
145
     * Constructs the object
146
     *
147
     * @link  http://php.net/manual/en/serializable.unserialize.php
148
     * @param string $serialized <p>
149
     *                           The string representation of the object.
150
     *                           </p>
151
     * @return void
152
     * @since 5.1.0
153
     */
154
    public function unserialize($serialized)
155
    {
156
        $data = unserialize($serialized);
157
158
        $this->iv       = $data['iv'];
159
        $this->checksum = $data['checksum'];
160
        $this->padding  = $data['padding'];
161
162
        if ($data['file']) {
163
            $this->file = new \SplFileInfo($data['file']);
164
        }
165
    }
166
}
167