Passed
Push — master ( 8b057b...bf1f51 )
by Matthias
01:44
created

Entropy::streamMean()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (c) 2017 Matthias Held <[email protected]>
5
 * @author Matthias Held <[email protected]>
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace OCA\RansomwareDetection\Entropy;
23
24
use OCP\ILogger;
0 ignored issues
show
Bug introduced by
The type OCP\ILogger 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...
25
26
class Entropy
27
{
28
    /** @var ILogger */
29
    private $logger;
30
31
    /**
32
     * @param IConfig $config
0 ignored issues
show
Bug introduced by
The type OCA\RansomwareDetection\Entropy\IConfig 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...
33
     */
34
    public function __construct(
35
        ILogger $logger
36
    ) {
37
        $this->logger = $logger;
38
    }
39
40
    /**
41
     * Calculates the entropy of data.
42
     *
43
     * @param string $data
44
     *
45
     * @return float
46
     */
47
    public function calculateEntropy($data)
48
    {
49
        $entropy = 0;
50
        $size = strlen($data);
51
52
        foreach (count_chars($data, 1) as $value) {
53
            $p = $value / $size;
54
            $entropy -= $p * log($p) / log(2);
55
        }
56
57
        return $entropy;
58
    }
59
60
    public function streamStandardDeviation($n, $sum, $mean) {
61
        return sqrt((1 / $n) * $sum - pow($mean, 2));
62
    }
63
64
    public function streamMean($oldMean, $value, $step) {
65
        $mean = 0;
66
        if ($step === 1) {
67
            $mean = (($step - 1) / $step) + ($value / $step);
68
        } else {
69
            $mean = $oldMean * (($step - 1) / $step) + ($value / $step);
70
        }
71
        return $mean;
72
    }
73
}
74