Password::getData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Xls\Record;
4
5
class Password extends AbstractRecord
6
{
7
    const NAME = 'PASSWORD';
8
    const ID = 0x0013;
9
10
    /**
11
     * Generate the PASSWORD biff record
12
     *
13
     * @param $plaintextPassword
14
     *
15
     * @return string
16
     */
17
    public function getData($plaintextPassword)
18
    {
19
        $data = pack("v", $this->encode($plaintextPassword));
20
21
        return $this->getFullRecord($data);
22
    }
23
24
    /**
25
     * Based on the algorithm provided by Daniel Rentz of OpenOffice.
26
     * @param string $plaintext The password to be encoded in plaintext.
27
     * @return string The encoded password
28
     */
29
    protected function encode($plaintext)
30
    {
31
        $password = 0x0000;
32
        $pos = 1; // char position
33
34
        // split the plain text password in its component characters
35
        $chars = preg_split('//', $plaintext, -1, PREG_SPLIT_NO_EMPTY);
36
        foreach ($chars as $char) {
37
            $value = ord($char) << $pos; // shifted ASCII value
38
            $rotatedBits = $value >> 15; // rotated bits beyond bit 15
39
            $value &= 0x7fff; // first 15 bits
40
            $password ^= ($value | $rotatedBits);
41
            $pos++;
42
        }
43
44
        $password ^= strlen($plaintext);
45
        $password ^= 0xCE4B;
46
47
        return $password;
48
    }
49
}
50