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: Made the test suite compatible with PHPUnit 9.
Date: 4 years ago
Size: 1,740 bytes
 

Contents

Class file image Download
<?php

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

   
/**
     * @throws SodiumException
     * @throws TypeError
     */
   
public function testPublicKeyConversion()
    {
       
$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);
        }
    }
}