PHP Classes

File: tests/KEM/MockDHKEM.php

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

Contents

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

use
Mdanter\Ecc\Crypto\Key\PrivateKey;
use
Mdanter\Ecc\Crypto\Key\PrivateKeyInterface;
use
Mdanter\Ecc\Math\GmpMath;
use
ParagonIE\EasyECC\EasyECC;
use
ParagonIE\HPKE\KEM\DHKEM\Curve;
use
ParagonIE\HPKE\KEM\DiffieHellmanKEM;
use
ParagonIE\HPKE\Util;

class
MockDHKEM extends DiffieHellmanKEM
{
    private
string|PrivateKeyInterface|null $mockPrivateKey = null;

    public function
setPrivateKey(string|PrivateKeyInterface $mockPrivateKey = null): static
    {
        if (
$this->curve === Curve::X25519) {
            if (
strlen($mockPrivateKey) === 32) {
               
$pk = sodium_crypto_box_publickey_from_secretkey($mockPrivateKey);
               
$this->mockPrivateKey = $mockPrivateKey . $pk;
                return
$this;
            }
        }
       
$this->mockPrivateKey = $mockPrivateKey;
        return
$this;
    }

    public function
generatePrivateKey(EasyECC $ecc): string|PrivateKeyInterface
   
{
        if (
is_null($this->mockPrivateKey)) {
            return
parent::generatePrivateKey($ecc);
        }
        if (
$ecc->getCurveName() === 'sodium') {
           
$result = $this->mockPrivateKey;
        } else {
           
$gen = EasyECC::getGenerator($ecc->getCurveName());
           
$result = new PrivateKey(
                new
GmpMath(),
               
$gen,
               
Util::bytesToGmp($this->mockPrivateKey)
            );
        }
        unset(
$this->mockPrivateKey);
        return
$result;
    }
}