Completed
Push — master ( 1e3cc9...c00213 )
by Ivan
02:12
created

TimestampRequest::generateFromFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 5
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace vakata\asn1\structures;
4
5
use \vakata\asn1\ASN1;
6
use \vakata\asn1\ASN1Exception;
7
use \vakata\asn1\Encoder;
8
9
class TimestampRequest extends Structure
10
{
11
    /**
12
     * Generate a timestamp request (tsq) for a file path
13
     * @param  string           $path        the path to the file to be timestamped
14
     * @param  boolean|string   $nonce       should a nonce be used - defaults to true, could be a value to use as nonce
15
     * @param  boolean          $requireCert should a certificate be returned in the response, defaults to false
16
     * @param  string           $alg         the algorithm to use, defaults to 'sha1'
17
     * @param  string|null      $policy      the policy to use, defaults to null
18
     * @return string                        the raw timestamp request
19
     * @codeCoverageIgnore
20
     */
21
    public static function generateFromFile($path, $nonce = true, $requireCert = false, $alg = 'sha1', $policy = null)
0 ignored issues
show
Unused Code introduced by
The parameter $nonce is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

21
    public static function generateFromFile($path, /** @scrutinizer ignore-unused */ $nonce = true, $requireCert = false, $alg = 'sha1', $policy = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
22
    {
23
        return static::generateFromData(file_get_contents($path), $none, $requireCert, $alg, $policy);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $none does not exist. Did you maybe mean $nonce?
Loading history...
24
    }
25
    /**
26
     * Generate a timestamp request (tsq) for a string
27
     * @param  string           $data        the data to be timestamped
28
     * @param  boolean|string   $nonce       should a nonce be used - defaults to true, could be a value to use as nonce
29
     * @param  boolean          $requireCert should a certificate be returned in the response, defaults to false
30
     * @param  string           $alg         the algorithm to use, defaults to 'sha1'
31
     * @param  string|null      $policy      the policy to use, defaults to null
32
     * @return string                        the raw timestamp request
33
     */
34
    public static function generateFromData($data, $nonce = true, $requireCert = false, $alg = 'sha1', $policy = null)
35
    {
36
        if (!in_array($alg, ['sha1', 'sha256', 'sha384', 'sha512', 'md5'])) {
37
            throw new TimestampException('Unsupported hash algorithm');
0 ignored issues
show
Bug introduced by
The type vakata\asn1\structures\TimestampException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
38
        }
39
        $hash = hash($alg, $data, true);
40
        if ($nonce === true) {
41
            $nonce = rand(1, PHP_INT_MAX);
42
        }
43
        if (!$nonce) {
44
            $nonce = null;
45
        }
46
        
47
        $src = [
48
            'version' => 'v1',
49
            'reqPolicy' => $policy,
50
            'messageImprint' => [
51
                'hashAlgorithm' => [ "algorithm" => $alg, 'parameters' => null ],
52
                'hashedMessage' => base64_encode($hash),
53
            ],
54
            'nonce' => $nonce,
55
            'certReq' => $requireCert
56
        ];
57
58
        return Encoder::encode($src, static::map());
59
    }
60
    /**
61
     * Generate a timestamp request (tsq) for a given hash
62
     * @param  string           $data        the hash to be timestamped (raw binary)
63
     * @param  boolean|string   $nonce       should a nonce be used - defaults to true, could be a value to use as nonce
64
     * @param  boolean          $requireCert should a certificate be returned in the response, defaults to false
65
     * @param  string           $alg         the algorithm to use, defaults to 'sha1'
66
     * @param  string|null      $policy      the policy to use, defaults to null
67
     * @return string                        the raw timestamp request
68
     */
69
    public static function generateFromHash($data, $nonce = true, $requireCert = false, $alg = 'sha1', $policy = null)
70
    {
71
        if (!in_array($alg, ['sha1', 'sha256', 'sha384', 'sha512', 'md5'])) {
72
            throw new ASN1Exception('Unsupported hash algorithm');
73
        }
74
        if ($nonce === true) {
75
            $nonce = rand(1, PHP_INT_MAX);
76
        }
77
        if (!$nonce) {
78
            $nonce = null;
79
        }
80
        
81
        $src = [
82
            'version' => 'v1',
83
            'reqPolicy' => $policy,
84
            'messageImprint' => [
85
                'hashAlgorithm' => [ "algorithm" => $alg, 'parameters' => null ],
86
                'hashedMessage' => base64_encode($data),
87
            ],
88
            'nonce' => $nonce,
89
            'certReq' => $requireCert
90
        ];
91
92
        return Encoder::encode($src, static::map());
93
    }
94
95
    public static function map()
96
    {
97
        return [
98
            'tag' => ASN1::TYPE_SEQUENCE,
99
            'children' => [
100
                'version' => [
101
                    'tag' => ASN1::TYPE_INTEGER,
102
                    'map' => [1=>'v1','v2','v3']
103
                ],
104
                'reqPolicy' => [
105
                    'tag' => ASN1::TYPE_OBJECT_IDENTIFIER,
106
                    'optional' => true,
107
                ],
108
                'messageImprint' => [
109
                    'tag' => ASN1::TYPE_SEQUENCE,
110
                    'children' => [
111
                       'hashAlgorithm' => Common::AlgorithmIdentifier(),
112
                       'hashedMessage' => [
113
                            'tag' => ASN1::TYPE_OCTET_STRING
114
                       ]
115
                    ]
116
                ],
117
                'nonce' => [
118
                    'tag' => ASN1::TYPE_INTEGER,
119
                    'optional' => true
120
                ],
121
                'certReq' => [
122
                    'tag' => ASN1::TYPE_BOOLEAN,
123
                    'optional' => true
124
                ]
125
            ]
126
        ];
127
    }
128
}
129