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;
3
 
4
use Aws\Crypto\Polyfill\AesGcm;
5
use Aws\Crypto\Polyfill\Key;
6
use GuzzleHttp\Psr7;
7
use GuzzleHttp\Psr7\StreamDecoratorTrait;
8
use Psr\Http\Message\StreamInterface;
9
use \RuntimeException;
10
 
11
/**
12
 * @internal Represents a stream of data to be gcm encrypted.
13
 */
14
class AesGcmEncryptingStream implements AesStreamInterface, AesStreamInterfaceV2
15
{
16
    use StreamDecoratorTrait;
17
 
18
    private $aad;
19
 
20
    private $initializationVector;
21
 
22
    private $key;
23
 
24
    private $keySize;
25
 
26
    private $plaintext;
27
 
28
    private $tag = '';
29
 
30
    private $tagLength;
31
 
32
    /**
1441 ariadna 33
     * @var StreamInterface
34
     */
35
    private $stream;
36
 
37
    /**
1 efrain 38
     * Same as non-static 'getAesName' method, allowing calls in a static
39
     * context.
40
     *
41
     * @return string
42
     */
43
    public static function getStaticAesName()
44
    {
45
        return 'AES/GCM/NoPadding';
46
    }
47
 
48
    /**
49
     * @param StreamInterface $plaintext
50
     * @param string $key
51
     * @param string $initializationVector
52
     * @param string $aad
53
     * @param int $tagLength
54
     * @param int $keySize
55
     */
56
    public function __construct(
57
        StreamInterface $plaintext,
58
        $key,
59
        $initializationVector,
60
        $aad = '',
61
        $tagLength = 16,
62
        $keySize = 256
63
    ) {
64
 
65
        $this->plaintext = $plaintext;
66
        $this->key = $key;
67
        $this->initializationVector = $initializationVector;
68
        $this->aad = $aad;
69
        $this->tagLength = $tagLength;
70
        $this->keySize = $keySize;
1441 ariadna 71
        // unsetting the property forces the first access to go through
72
        // __get().
73
        unset($this->stream);
1 efrain 74
    }
75
 
76
    public function getOpenSslName()
77
    {
78
        return "aes-{$this->keySize}-gcm";
79
    }
80
 
81
    /**
82
     * Same as static method and retained for backwards compatibility
83
     *
84
     * @return string
85
     */
86
    public function getAesName()
87
    {
88
        return self::getStaticAesName();
89
    }
90
 
91
    public function getCurrentIv()
92
    {
93
        return $this->initializationVector;
94
    }
95
 
96
    public function createStream()
97
    {
98
        if (version_compare(PHP_VERSION, '7.1', '<')) {
99
            return Psr7\Utils::streamFor(AesGcm::encrypt(
100
                (string) $this->plaintext,
101
                $this->initializationVector,
102
                new Key($this->key),
103
                $this->aad,
104
                $this->tag,
105
                $this->keySize
106
            ));
107
        } else {
108
            return Psr7\Utils::streamFor(\openssl_encrypt(
109
                (string)$this->plaintext,
110
                $this->getOpenSslName(),
111
                $this->key,
112
                OPENSSL_RAW_DATA,
113
                $this->initializationVector,
114
                $this->tag,
115
                $this->aad,
116
                $this->tagLength
117
            ));
118
        }
119
    }
120
 
121
    /**
122
     * @return string
123
     */
124
    public function getTag()
125
    {
126
        return $this->tag;
127
    }
128
 
1441 ariadna 129
    public function isWritable(): bool
1 efrain 130
    {
131
        return false;
132
    }
133
}