PHP Classes

File: src/AEAD/AES256GCM.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   HPKE PHP   src/AEAD/AES256GCM.php   Download  
File: src/AEAD/AES256GCM.php
Role: Class source
Content type: text/plain
Description: Class source
Class: HPKE PHP
Encrypt and decrypt data using hybrid public keys
Author: By
Last change:
Date: 8 days ago
Size: 1,631 bytes
 

Contents

Class file image Download
<?php
declare(strict_types=1);
namespace
ParagonIE\HPKE\AEAD;

use
ParagonIE\HPKE\HPKEException;
use
ParagonIE\HPKE\Interfaces\AEADInterface;
use
ParagonIE\HPKE\SymmetricKey;

class
AES256GCM implements AEADInterface
{
    const
AEAD_ID = "\x00\x02";

    public function
getAeadId(): string
   
{
        return
self::AEAD_ID;
    }

    public function
keyLength(): int
   
{
        return
32;
    }

    public function
nonceLength(): int
   
{
        return
12;
    }

    public function
tagLength(): int
   
{
        return
16;
    }


    public function
encrypt(
       
#[\SensitiveParameter] SymmetricKey $key,
        #[\SensitiveParameter] string $plaintext,
       
string $nonce,
       
string $aad = ''
   
): array {
       
$tag = '';
       
$ciphertext = openssl_encrypt(
           
$plaintext,
           
'aes-256-gcm',
           
$key->bytes,
           
OPENSSL_RAW_DATA | OPENSSL_NO_PADDING,
           
$nonce,
           
$tag,
           
$aad
       
);
        return [
$ciphertext, $tag];
    }

   
/**
     * @throws HPKEException
     */
   
public function decrypt(
       
#[\SensitiveParameter] SymmetricKey $key,
       
string $ciphertext,
       
string $tag,
       
string $nonce,
       
string $aad = ''
   
): string {
       
$result = openssl_decrypt(
           
$ciphertext,
           
'aes-256-gcm',
           
$key->bytes,
           
OPENSSL_RAW_DATA | OPENSSL_NO_PADDING,
           
$nonce,
           
$tag,
           
$aad
       
);
        if (!
is_string($result)) {
            throw new
HPKEException('Decryption error');
        }
        return
$result;
    }
}