Proyectos de Subversion Moodle

Rev

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