PHP Classes

File: src/Operations/Key/SealingSecretKey.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   PASERK PHP   src/Operations/Key/SealingSecretKey.php   Download  
File: src/Operations/Key/SealingSecretKey.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PASERK PHP
Extend PASETO to wrap and serialize keys
Author: By
Last change:
Date: 1 year ago
Size: 2,430 bytes
 

Contents

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

use
ParagonIE\EasyECC\ECDSA\{
   
PublicKey,
   
SecretKey
};
use
ParagonIE\EasyECC\Exception\NotImplementedException;
use
ParagonIE\Paseto\Keys\{
   
AsymmetricPublicKey,
   
AsymmetricSecretKey
};
use
ParagonIE\Paseto\Protocol\{
   
Version3,
   
Version4
};
use
ParagonIE\Paseto\ProtocolInterface;
use
ParagonIE\Paseto\Util;
use
Exception;
use
SodiumException;
use
TypeError;
use function
   
hash_equals,
   
sodium_crypto_sign_keypair,
   
sodium_crypto_sign_publickey_from_secretkey,
   
sodium_crypto_sign_secretkey;

/**
 * Class SealingSecretKey
 * @package ParagonIE\Paserk\Operations\Key
 */
class SealingSecretKey extends AsymmetricSecretKey
{
   
/**
     * @param ProtocolInterface|null $protocol
     * @return self
     *
     * @throws Exception
     * @throws NotImplementedException
     * @throws SodiumException
     */
   
public static function generate(ProtocolInterface $protocol = null): AsymmetricSecretKey
   
{
       
$protocol = $protocol ?? new Version4;
        if (
hash_equals($protocol::header(), Version3::HEADER)) {
            return new
self(
               
Util::dos2unix(SecretKey::generate(Version3::CURVE)->exportPem()),
               
$protocol
           
);
        }
        return new
self(
           
sodium_crypto_sign_secretkey(
               
sodium_crypto_sign_keypair()
            ),
           
$protocol
       
);
    }

   
/**
     * @return AsymmetricSecretKey
     *
     * @throws Exception
     */
   
public function toPasetoKey(): AsymmetricSecretKey
   
{
        return new
AsymmetricSecretKey(
           
$this->key,
           
$this->protocol
       
);
    }

   
/**
     * @return AsymmetricPublicKey
     *
     * @throws Exception
     * @throws TypeError
     */
   
public function getPublicKey(): AsymmetricPublicKey
   
{
        switch (
$this->protocol::header()) {
            case
Version3::HEADER:
               
/** @var PublicKey $pk */
               
$pk = SecretKey::importPem($this->key)->getPublicKey();
                return new
SealingPublicKey(
                   
$pk->exportPem(),
                   
$this->protocol
               
);
            default:
                return new
SealingPublicKey(
                   
sodium_crypto_sign_publickey_from_secretkey($this->key),
                   
$this->protocol
               
);
        }
    }
}