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;
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
    /**
33
     * Same as non-static 'getAesName' method, allowing calls in a static
34
     * context.
35
     *
36
     * @return string
37
     */
38
    public static function getStaticAesName()
39
    {
40
        return 'AES/GCM/NoPadding';
41
    }
42
 
43
    /**
44
     * @param StreamInterface $plaintext
45
     * @param string $key
46
     * @param string $initializationVector
47
     * @param string $aad
48
     * @param int $tagLength
49
     * @param int $keySize
50
     */
51
    public function __construct(
52
        StreamInterface $plaintext,
53
        $key,
54
        $initializationVector,
55
        $aad = '',
56
        $tagLength = 16,
57
        $keySize = 256
58
    ) {
59
 
60
        $this->plaintext = $plaintext;
61
        $this->key = $key;
62
        $this->initializationVector = $initializationVector;
63
        $this->aad = $aad;
64
        $this->tagLength = $tagLength;
65
        $this->keySize = $keySize;
66
    }
67
 
68
    public function getOpenSslName()
69
    {
70
        return "aes-{$this->keySize}-gcm";
71
    }
72
 
73
    /**
74
     * Same as static method and retained for backwards compatibility
75
     *
76
     * @return string
77
     */
78
    public function getAesName()
79
    {
80
        return self::getStaticAesName();
81
    }
82
 
83
    public function getCurrentIv()
84
    {
85
        return $this->initializationVector;
86
    }
87
 
88
    public function createStream()
89
    {
90
        if (version_compare(PHP_VERSION, '7.1', '<')) {
91
            return Psr7\Utils::streamFor(AesGcm::encrypt(
92
                (string) $this->plaintext,
93
                $this->initializationVector,
94
                new Key($this->key),
95
                $this->aad,
96
                $this->tag,
97
                $this->keySize
98
            ));
99
        } else {
100
            return Psr7\Utils::streamFor(\openssl_encrypt(
101
                (string)$this->plaintext,
102
                $this->getOpenSslName(),
103
                $this->key,
104
                OPENSSL_RAW_DATA,
105
                $this->initializationVector,
106
                $this->tag,
107
                $this->aad,
108
                $this->tagLength
109
            ));
110
        }
111
    }
112
 
113
    /**
114
     * @return string
115
     */
116
    public function getTag()
117
    {
118
        return $this->tag;
119
    }
120
 
121
    public function isWritable()
122
    {
123
        return false;
124
    }
125
}