EncryptedBlob::getMnemonic()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Btccom\JustEncrypt;
4
5
use AESGCM\AESGCM;
6
use BitWasp\Buffertools\Buffer;
7
use BitWasp\Buffertools\BufferInterface;
8
use BitWasp\Buffertools\Parser;
9
10
class EncryptedBlob
11
{
12
    /**
13
     * @var HeaderBlob
14
     */
15
    private $header;
16
17
    /**
18
     * @var BufferInterface
19
     */
20
    private $iv;
21
22
    /**
23
     * @var BufferInterface
24
     */
25
    private $cipherText;
26
27
    /**
28
     * @var BufferInterface
29
     */
30
    private $tag;
31
32
    /**
33
     * EncryptedBlob constructor.
34
     * @param HeaderBlob $header
35
     * @param BufferInterface $iv
36
     * @param BufferInterface $cipherText
37
     * @param BufferInterface $tag
38
     */
39 4
    public function __construct(HeaderBlob $header, BufferInterface $iv, BufferInterface $cipherText, BufferInterface $tag)
40
    {
41 4
        if ($iv->getSize() !== 16) {
42
            throw new \RuntimeException('IV must be exactly 16 bytes');
43
        }
44
45 4
        if ($tag->getSize() !== 16) {
46
            throw new \RuntimeException('Tag must be exactly 16 bytes');
47
        }
48
49 4
        $this->header = $header;
50 4
        $this->iv = $iv;
51 4
        $this->cipherText = $cipherText;
52 4
        $this->tag = $tag;
53 4
    }
54
55
    /**
56
     * @return HeaderBlob
57
     */
58 1
    public function getHeader()
59
    {
60 1
        return $this->header;
61
    }
62
63
    /**
64
     * @return BufferInterface
65
     */
66 1
    public function getIv()
67
    {
68 1
        return $this->iv;
69
    }
70
71
    /**
72
     * @return BufferInterface
73
     */
74 1
    public function getCipherText()
75
    {
76 1
        return $this->cipherText;
77
    }
78
79
    /**
80
     * @return BufferInterface
81
     */
82 1
    public function getTag()
83
    {
84 1
        return $this->tag;
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getMnemonic()
91
    {
92
        return EncryptionMnemonic::encode($this->getBuffer());
93
    }
94
95
    /**
96
     * @param BufferInterface $password
97
     * @return Buffer
98
     */
99 3
    public function decrypt(BufferInterface $password)
100
    {
101 3
        return new Buffer(
102 3
            AESGCM::decrypt(
103 3
                $this->header->deriveKey($password)->getBinary(),
104 3
                $this->iv->getBinary(),
105 3
                $this->cipherText->getBinary(),
106 3
                $this->header->getBinary(),
107 3
                $this->tag->getBinary()
108
            )
109
        );
110
    }
111
112
    /**
113
     * @return string
114
     */
115 4
    public function getBinary()
116
    {
117
        return
118 4
            $this->header->getBinary() .
119 4
            $this->iv->getBinary() .
120 4
            $this->cipherText->getBinary() .
121 4
            $this->tag->getBinary();
122
    }
123
124
    /**
125
     * @return BufferInterface
126
     */
127 4
    public function getBuffer()
128
    {
129 4
        return new Buffer($this->getBinary());
130
    }
131
132
    /**
133
     * @param Parser $parser
134
     * @return EncryptedBlob
135
     */
136 3
    public static function fromParser(Parser $parser)
137
    {
138 3
        $size = $parser->getBuffer()->getSize();
139
140 3
        return new EncryptedBlob(
141 3
            HeaderBlob::fromParser($parser),
142 3
            $parser->readBytes(16),
143 3
            $parser->readBytes($size - $parser->getPosition() - 16),
144 3
            $parser->readBytes(16)
145
        );
146
    }
147
}
148