Issues (195)

lib/Entropy/Entropy.php (3 issues)

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
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
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
    /**
61
     * Calculates the standard deviation of a continoues series by passing
62
     * the current number of values used to calculate the standard deviation
63
     * the sum of all these values and the mean of all these values.
64
     * 
65
     * @param float $step current value
66
     * @param float $sum sum of all values
67
     * @param float $mean mean of all values
68
     * 
69
     * @return float
70
     */
71
    public function calculateStandardDeviationOfSeries($step, $sum, $mean) {
72
        return sqrt((1 / $step) * $sum - pow($mean, 2));
73
    }
74
75
    /**
76
     * Calculates the mean of a continoues series by passing the old mean 
77
     * the new value and the number of values used to calculate the mean
78
     * including the new one.
79
     * 
80
     * @param float $oldMean
81
     * @param float $value
82
     * @param float $step
83
     * 
84
     * @param float
85
     */
86
    public function calculateMeanOfSeries($oldMean, $value, $step) {
87
        $mean = 0;
88
        if ($step === 1) {
0 ignored issues
show
The condition $step === 1 is always false.
Loading history...
89
            $mean = (($step - 1) / $step) + ($value / $step);
90
        } else {
91
            $mean = $oldMean * (($step - 1) / $step) + ($value / $step);
92
        }
93
        return $mean;
94
    }
95
}
96