Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace Aws\Crypto\Polyfill;
3
 
4
/**
5
 * Class Key
6
 *
7
 * Wraps a string to keep it hidden from stack traces.
8
 */
9
class Key
10
{
11
    /**
12
     * @var string $internalString
13
     */
14
    private $internalString;
15
 
16
    /**
17
     * Hide contents of
18
     *
19
     * @return array
20
     */
21
    public function __debugInfo()
22
    {
23
        return [];
24
    }
25
 
26
    /**
27
     * Key constructor.
28
     * @param string $str
29
     */
30
    public function __construct($str)
31
    {
32
        $this->internalString = $str;
33
    }
34
 
35
    /**
36
     * Defense in depth:
37
     *
38
     * PHP 7.2 includes the Sodium cryptography library, which (among other things)
39
     * exposes a function called sodium_memzero() that we can use to zero-fill strings
40
     * to minimize the risk of sensitive cryptographic materials persisting in memory.
41
     *
42
     * If this function is not available, we XOR the string in-place with itself as a
43
     * best-effort attempt.
44
     */
45
    public function __destruct()
46
    {
47
        if (extension_loaded('sodium') && function_exists('sodium_memzero')) {
48
            try {
49
                \sodium_memzero($this->internalString);
50
            } catch (\SodiumException $ex) {
51
                // This is a best effort, but does not provide the same guarantees as sodium_memzero():
52
                $this->internalString ^= $this->internalString;
53
            }
54
        }
55
    }
56
 
57
    /**
58
     * @return string
59
     */
60
    public function get()
61
    {
62
        return $this->internalString;
63
    }
64
 
65
    /**
66
     * @return int
67
     */
68
    public function length()
69
    {
70
        if (\is_callable('\\mb_strlen')) {
71
            return (int) \mb_strlen($this->internalString, '8bit');
72
        }
73
        return (int) \strlen($this->internalString);
74
    }
75
}