PHP Classes

File: src/Context.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   HPKE PHP   src/Context.php   Download  
File: src/Context.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,578 bytes
 

Contents

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

use
ParagonIE\HPKE\Interfaces\SymmetricKeyInterface;
use
SodiumException;

abstract class
Context
{
    public function
__construct(
        protected
HPKE $hpke,
       
#[\SensitiveParameter] protected SymmetricKeyInterface $key,
        #[\SensitiveParameter] protected string $baseNonce,
       
protected int $sequence,
       
#[\SensitiveParameter] protected string $exporterSecret,
   
) {}

   
/**
     * @param string $exporterContext
     * @param int $length
     * @return string
     */
   
public function export(string $exporterContext, int $length): string
   
{
        return
$this->hpke->labeledExpand(
           
$this->exporterSecret,
           
'sec',
           
$exporterContext,
           
$length
       
);
    }

   
/**
     * @return string
     */
   
protected function computeNonce(): string
   
{
       
$seq_bytes = str_pad(
           
pack('J', $this->sequence),
           
$this->hpke->aead->nonceLength(),
           
"\0",
           
STR_PAD_LEFT
       
);
        return
$this->baseNonce ^ $seq_bytes;
    }

   
/**
     * @return void
     *
     * @throws HPKEException
     */
   
protected function incrementSeq(): void
   
{
       
$max = PHP_INT_MAX; // (1 << ($this->hpke->aead->nonceLength() * 8)) - 1;
       
if ($this->sequence >= $max) {
            throw new
HPKEException('Message limit reached');
        }
        ++
$this->sequence;
    }
}