v2: Updates

* Simplifies & beautifies everything
* Introduces a new Class system.
* Errors are defaulted to AWS's handler.
* New function names & more efficient handling.
* Should fix a majority of the errors.

Please read the README for more!
This commit is contained in:
Devang Srivastava 2020-09-28 15:32:51 +05:30
commit e6d7753dc8
1095 changed files with 45088 additions and 2911 deletions

View file

@ -1,6 +1,8 @@
<?php
namespace Aws\Crypto;
use Aws\Crypto\Polyfill\AesGcm;
use Aws\Crypto\Polyfill\Key;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\StreamDecoratorTrait;
use Psr\Http\Message\StreamInterface;
@ -9,7 +11,7 @@ use \RuntimeException;
/**
* @internal Represents a stream of data to be gcm encrypted.
*/
class AesGcmEncryptingStream implements AesStreamInterface
class AesGcmEncryptingStream implements AesStreamInterface, AesStreamInterfaceV2
{
use StreamDecoratorTrait;
@ -27,6 +29,17 @@ class AesGcmEncryptingStream implements AesStreamInterface
private $tagLength;
/**
* Same as non-static 'getAesName' method, allowing calls in a static
* context.
*
* @return string
*/
public static function getStaticAesName()
{
return 'AES/GCM/NoPadding';
}
/**
* @param StreamInterface $plaintext
* @param string $key
@ -43,11 +56,6 @@ class AesGcmEncryptingStream implements AesStreamInterface
$tagLength = 16,
$keySize = 256
) {
if (version_compare(PHP_VERSION, '7.1', '<')) {
throw new RuntimeException(
'AES-GCM decryption is only supported in PHP 7.1 or greater'
);
}
$this->plaintext = $plaintext;
$this->key = $key;
@ -62,9 +70,14 @@ class AesGcmEncryptingStream implements AesStreamInterface
return "aes-{$this->keySize}-gcm";
}
/**
* Same as static method and retained for backwards compatibility
*
* @return string
*/
public function getAesName()
{
return 'AES/GCM/NoPadding';
return self::getStaticAesName();
}
public function getCurrentIv()
@ -74,16 +87,27 @@ class AesGcmEncryptingStream implements AesStreamInterface
public function createStream()
{
return Psr7\stream_for(openssl_encrypt(
(string) $this->plaintext,
$this->getOpenSslName(),
$this->key,
OPENSSL_RAW_DATA,
$this->initializationVector,
$this->tag,
$this->aad,
$this->tagLength
));
if (version_compare(PHP_VERSION, '7.1', '<')) {
return Psr7\stream_for(AesGcm::encrypt(
(string) $this->plaintext,
$this->initializationVector,
new Key($this->key),
$this->aad,
$this->tag,
$this->keySize
));
} else {
return Psr7\stream_for(\openssl_encrypt(
(string)$this->plaintext,
$this->getOpenSslName(),
$this->key,
OPENSSL_RAW_DATA,
$this->initializationVector,
$this->tag,
$this->aad,
$this->tagLength
));
}
}
/**