Passed
Push — master ( 83aa3d...907404 )
by Thomas
07:17
created

Der::octetString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace MadWizard\WebAuthn\Crypto;
4
5
class Der
6
{
7 20
    private static function length(int $len): string
8
    {
9 20
        if ($len < 128) {
10 20
            return \chr($len);
11
        }
12
13 6
        $lenBytes = '';
14 6
        while ($len > 0) {
15 6
            $lenBytes = \chr($len % 256) . $lenBytes;
16 6
            $len = \intdiv($len, 256);
17
        }
18 6
        return \chr(0x80 | \strlen($lenBytes)) . $lenBytes;
19
    }
20
21 17
    public static function sequence(string $contents): string
22
    {
23 17
        return "\x30" . self::length(\strlen($contents)) . $contents;
24
    }
25
26 17
    public static function oid(string $encoded): string
27
    {
28 17
        return "\x06" . self::length(\strlen($encoded)) . $encoded;
29
    }
30
31 8
    public static function unsignedInteger(string $bytes): string
32
    {
33 8
        $len = \strlen($bytes);
34
35
        // Remove leading zero bytes
36 8
        for ($i = 0; $i < ($len - 1); $i++) {
37 8
            if (\ord($bytes[$i]) !== 0) {
38 8
                break;
39
            }
40
        }
41 8
        if ($i !== 0) {
42 1
            $bytes = \substr($bytes, $i);
43
        }
44
45
        // If most significant bit is set, prefix with another zero to prevent it being seen as negative number
46 8
        if ((\ord($bytes[0]) & 0x80) !== 0) {
47 8
            $bytes = "\x00" . $bytes;
48
        }
49
50 8
        return "\x02" . self::length(\strlen($bytes)) . $bytes;
51
    }
52
53 17
    public static function bitString(string $bytes): string
54
    {
55 17
        $len = \strlen($bytes) + 1;
56
57 17
        return "\x03" . self::length($len) . "\x00" . $bytes;
58
    }
59
60 1
    public static function octetString(string $bytes): string
61
    {
62 1
        $len = \strlen($bytes);
63
64 1
        return "\x04" . self::length($len) . $bytes;
65
    }
66
67 1
    public static function contextTag(int $tag, bool $constructed, string $content): string
68
    {
69 1
        return \chr(($tag & 0x1F) |   // Context specific tag number
70
            (1 << 7) |  // Context-specific flag
71 1
            ($constructed ? (1 << 5) : 0)) .
72 1
            self::length(\strlen($content)) .
73 1
            $content;
74
    }
75
76 6
    public static function nullValue(): string
77
    {
78 6
        return "\x05\x00";
79
    }
80
81 14
    public static function pem(string $type, string $der): string
82
    {
83 14
        return sprintf("-----BEGIN %s-----\n", strtoupper($type)) .
84 14
            chunk_split(base64_encode($der), 64, "\n") .
85 14
            sprintf("-----END %s-----\n", strtoupper($type));
86
    }
87
}
88