PHP Classes

File: tests/unit/KeyConversionTest.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   PHP Sodium Compat   tests/unit/KeyConversionTest.php   Download  
File: tests/unit/KeyConversionTest.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP Sodium Compat
Cryptographic functions of libsodium in pure PHP
Author: By
Last change: Merge pull request #175 from paragonie/box-v2

Remove vendor/paragonie from box.json
Date: 13 days ago
Size: 1,766 bytes
 

Contents

Class file image Download
<?php
use PHPUnit\Framework\TestCase;

/**
 * Class KeyConversionTest
 */
class KeyConversionTest extends TestCase
{
   
/**
     * @before
     */
   
public function before(): void
   
{
       
ParagonIE_Sodium_Compat::$disableFallbackForUnitTests = true;
    }

   
/**
     * @throws SodiumException
     * @throws TypeError
     */
   
public function testPublicKeyConversion(): void
   
{
       
$sign_keypair = ParagonIE_Sodium_Compat::crypto_sign_keypair();
       
$sign_secret = ParagonIE_Sodium_Compat::crypto_sign_secretkey($sign_keypair);
       
$sign_public = ParagonIE_Sodium_Compat::crypto_sign_publickey($sign_keypair);

       
$sk_convert = ParagonIE_Sodium_Compat::crypto_sign_ed25519_sk_to_curve25519($sign_secret);
       
$pk_expect = ParagonIE_Sodium_Compat::crypto_box_publickey_from_secretkey($sk_convert);
       
$pk_convert = ParagonIE_Sodium_Compat::crypto_sign_ed25519_pk_to_curve25519($sign_public);
       
$this->assertSame(
           
ParagonIE_Sodium_Core_Util::bin2hex($pk_expect),
           
ParagonIE_Sodium_Core_Util::bin2hex($pk_convert),
           
'Different strings from different approaches of converting Ed25519 -> X25519'
       
);
       
$messages = array(
           
'test',
           
str_repeat('A', 100),
           
random_bytes(100)
        );

        foreach(
$messages as $message) {
           
$sealed = ParagonIE_Sodium_Compat::crypto_box_seal($message, $pk_convert);
           
$opened = ParagonIE_Sodium_Compat::crypto_box_seal_open($sealed, $sk_convert . $pk_convert);
           
$this->assertSame($message, $opened);
           
$opened = ParagonIE_Sodium_Compat::crypto_box_seal_open($sealed, $sk_convert . $pk_expect);
           
$this->assertSame($message, $opened);
        }
    }
}