Skip to content

Instantly share code, notes, and snippets.

@paragonie-scott
Last active January 24, 2018 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paragonie-scott/87cd39d21301ac0658027b822ab53c32 to your computer and use it in GitHub Desktop.
Save paragonie-scott/87cd39d21301ac0658027b822ab53c32 to your computer and use it in GitHub Desktop.
Peachpie Sodium_compat test/benchmarking scripts
scott@paragonie-test:~/dotnet$ php program.php 
Time: 11.9136 seconds.

Doesn't build on Windows:

C:\Users\Scott\.nuget\packages\peachpie.net.sdk\0.9.0-ci00687\build\Peachpie.NET.Core.Sdk.targets(148,5): error MSB3073: The command "dotnet compile-php @obj\Debug\netcoreapp2.0\compile-php-args.rsp" exited with code -532462766. [D:\dotnet\dotnet.msbuildproj]
{
"require": {
"paragonie/sodium_compat": "^1"
},
"license": "MIT",
"authors": [
{
"name": "Paragon Initiative Enterprises, LLC",
"email": "security@paragonie.com"
}
]
}
<?php
require 'vendor/autoload.php';
function main()
{
$start = $end = microtime(true);
$x_kp = ParagonIE_Sodium_Compat::crypto_sign_keypair();
$x_sk = ParagonIE_Sodium_Compat::crypto_sign_secretkey($x_kp);
$x_pk = ParagonIE_Sodium_Compat::crypto_sign_publickey($x_kp);
$x_encrypt_sk = ParagonIE_Sodium_Compat::crypto_sign_ed25519_sk_to_curve25519($x_sk);
$x_encrypt_pk = ParagonIE_Sodium_Compat::crypto_sign_ed25519_pk_to_curve25519($x_pk);
for ($i = 0; $i < 100; ++$i) {
$y_kp = ParagonIE_Sodium_Compat::crypto_sign_keypair();
$y_sk = ParagonIE_Sodium_Compat::crypto_sign_secretkey($y_kp);
$y_pk = ParagonIE_Sodium_Compat::crypto_sign_publickey($y_kp);
$y_encrypt_sk = ParagonIE_Sodium_Compat::crypto_sign_ed25519_sk_to_curve25519($y_sk);
$y_encrypt_pk = ParagonIE_Sodium_Compat::crypto_sign_ed25519_pk_to_curve25519($y_pk);
$message = 'This is a secret message.';
$nonce = random_bytes(24);
$x_shared_kp = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey($x_encrypt_sk, $y_encrypt_pk);
$encrypted = ParagonIE_Sodium_Compat::crypto_box($message, $nonce, $x_shared_kp);
$y_shared_kp = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey($y_encrypt_sk, $x_encrypt_pk);
$decrypted = ParagonIE_Sodium_Compat::crypto_box_open($encrypted, $nonce, $y_shared_kp);
}
$end = microtime(true);
$diff = $end - $start;
echo 'Time: ', number_format($diff, 4), ' seconds.', PHP_EOL;
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment