I want to generate private/public keys using Elliptic Curve Cryptography in PHP.
I have used this library: https://github.com/simplito/elliptic-php
my code:
<?php
use Elliptic\EC;
// Create and initialize EC context
// (better do it once and reuse it)
$ec = new EC('secp256k1');
// Generate keys
$key = $ec->genKeyPair();
$publicKey = $key->getPublic('hex');
$privateKey = $key->getPrivate('hex');
// Print the keys to the console
echo "The address1 is {$publicKey}. \r\n";
echo "The address1 is {$privateKey}. \r\n";
but it shows me this error:
PHP Fatal error: Uncaught Error: Class 'Elliptic\EC' not found in /home/istabraq/bctest/test1/keygenerator.php:6
I have installed composer Composer 1.6.3
from this tutorial:
https://linuxize.com/post/how-to-install-and-use-composer-on-ubuntu-18-04/
then installed sudo apt-get install php7.2-gmp
and installed composer require simplito/elliptic-php and finally installed composer require simplito/bn-php but the last command line shows me this output:
Using version ^1.1 for simplito/bn-php
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Writing lock file
Generating autoload files
what I have missed please help? I search for problem but did not have tutorial.
try include file with class or if exists vendor/autoload.php include it, for example:
<?php
include 'path/vendor/autoload.php'; //or 'path/file/EC.php'
use Elliptic\EC;
// Create and initialize EC context
// (better do it once and reuse it)
$ec = new EC('secp256k1');
// Generate keys
$key = $ec->genKeyPair();
$publicKey = $key->getPublic('hex');
$privateKey = $key->getPrivate('hex');
// Print the keys to the console
echo "The address1 is {$publicKey}. \r\n";
echo "The address1 is {$privateKey}. \r\n";
Related
I have a dedicated server with WHM and cPanel. It already has composer installed on it.
I'm trying to run the following php lines in a webpage:
require __DIR__ . '/../vendor/autoload.php';
// Configure API key authorization: JWT
$config = \Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Authorization', '[my api key]');
$apiInstance = new \Swagger\Client\Api\MessagesApi(
new \GuzzleHttp\Client(),
$config
);
I've created a composer.json in the public_html folder and put the following in it:
{
"autoload": {
"psr-4": { "Swagger\\Client\\" : "lib/" }
}
}
And then I ran composer update in the terminal which seemed to install the dependencies and all the relevant files.
It's seeing the autoload.php file but I'm still getting a class not found error:
Fatal error: Uncaught Error: Class 'Swagger\Client\Configuration' not found in /home/mywebsite/public_html/converter/sms.php:9 Stack trace: #0 {main} thrown in /home/mywebsite/public_html/converter/sms.php on line 9
I've been at this for 4 hours now. What am I doing wrong? I can't find anything online that will guide me in the right direction.
In Composer Basic Usage documentation about autoloading it says
For libraries that specify autoload information, Composer generates a vendor/autoload.php file. You can include this file and start using the classes that those libraries provide without any extra work
So, as #NicoHaase said in the comments, if you installed Swagger with Composer (adding a require key and running composer update, for example) you don't need to specify the autoload path to Swagger in composer.json.
I am trying to include a package from composer, but I am receiving a class not found error.
I have tried the below possibilities.
$supermeteor = new \Supermeteor\Supermeteor('XXXXXXXX');
and
use Supermeteor\Supermeteor;
$supermeteor = new Supermeteor('xxxxxxxx');
Packages composer.json:
"psr-4": {
"Supermeteor\\": ""
}
Packages namespace :
namespace Supermeteor;
Packages class name :
class Supermeteor() {}
Error message
Uncaught Error: Class 'Supermeteor\Supermeteor' not found in
C:\path\to\my\file.php:16
I just tested your package locally, and it seems to work fine for me using the same code as you provided in your question. This is how I tested it.
1. Create a new project
Create a new directory on your computer.
2. Add the package to a new project using Composer
Locate your new directory on the command line and add the package to your projects autoloader by running the below composer command.
composer require supermeteor/sdk-php
3. Use the package
Create an index.php file in the same directory as your composer.json and add the below code.
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Supermeteor\Supermeteor;
$supermeteor = new Supermeteor("xxx");
4. Test the results
In the terminal window start a new php server to serve your project.
php -S localhost:8089
Now access the site via your browser at http://localhost:8089.
I try to run Flysystem's basic example code for the Local adapter and get a Class 'League\Flysystem\Adapter\Local' not found error. This is my process:
version check:
php -v
PHP 5.5.9-1ubuntu4.23 (cli) (built: Feb 8 2018 21:59:47)
install Flysystem:
composer require league/flysystem
output shows I'm up-to-date (this is my 2nd time running it):
Using version ^1.0 for league/flysystem
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
now there's a vendor folder in web root. And within ./web/vendor/league/flysystem/src/Adapter$ are these files:
AbstractAdapter.php
AbstractFtpAdapter.php
CanOverwriteFiles.php
Ftpd.php
Ftp.php
Local.php
NullAdapter.php
Polyfill/
SynologyFtp.php
...just showing that it seems to be installed correctly(?) I create one test file and one test directory in my web root:
fly-local.php
myfiles/
Into fly-local.php I paste the text from their docs (https://flysystem.thephpleague.com/docs/adapter/local/):
<?php
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;
$adapter = new League\Flysystem\Adapter\Local(__DIR__.'/myfiles');
$filesystem = new Filesystem($adapter);
...and change the adapter's root folder to myfiles (is that correct?). Then I run it:
php fly-local.php
It outputs:
PHP Fatal error: Class 'League\Flysystem\Adapter\Local' not found in /[PROJECT DIR]/web/fly-local.php on line 6
PHP Stack trace:
PHP 1. {main}() /[PROJECT DIR]/web/fly-local.php:0
What am I doing wrong?
You used composer, then you need to include the composer autoload.php file.
The fly-local.php should be:
<?php
require __DIR__.'/vendor/autoload.php';
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;
$adapter = new League\Flysystem\Adapter\Local(__DIR__.'/myfiles');
$filesystem = new Filesystem($adapter);
If you use a framework, you can see it includes the autoload php file for you (index.php, in general). If your test/custom file is not included to framework, you need to include the file manually.
I'm trying to learn about Protobuf in PHP using https://github.com/google/protobuf/tree/master/php . Currently I'm stuck in an error.
My steps to install protobuf:
Install protobuf through pecl with command:
sudo pecl install protobuf-3.2.0a1
Set composer.json as below, then run sudo composer install
{
"require": {
"google/protobuf": "^3.2"
}
}
Below is my code:
Proto file:
syntax = "proto3";
message APIReq {
string functionName = 1;
string name = 2;
int32 time = 3;
string type = 4;
}
Command to generate PHP Class from .proto file:
protoc --php_out=/var/www/html/ MsgFormat.proto
The protoc command resulted in two file, APIReq.php and GPBMetadata/MsgFormat.php
After that, I added require_once __DIR__ . '/vendor/autoload.php'; and require_once __DIR__ . '/GPBMetadata/MsgFormat.php'; in the generated PHP file because when I ran php APIReq.php it came up with
PHP Fatal error: Class 'Google\Protobuf\Internal\Message' not found in /var/www/html/testing/APIReq.php on line 13
After I added those line, the error disappeared, so I assume both line fixed the problem
my PHP file (following example from https://developers.google.com/protocol-buffers/docs/reference/php-generated, section Messages):
<?php
require __DIR__ . '/vendor/autoload.php';
include_once('APIReq.php');
$param = new APIReq();
$param2 = new APIReq();
$param->setFunctionname('functionname');
$param->setName('name');
$param->setTime(123456);
$param->setType('type');
$dt = $param->encode();
$param2->decode($dt);
?>
When I run the PHP code, it returns error message:
PHP Fatal error: Call to undefined method APIReq::encode()
How can I fix this?
Edit: Tried this with protobuf 3.3.0 as well, with same result.
Encode & Decode not exist in the codebase as I traced down.
This change was introduced in 3.3.0
//to encode message
$data = $param->serializeToString();
//to decode message
$param2 = new APIReq();
$param2->mergeFromString($data);
I am trying to implement push notification.
I downloaded web-push library from: https://github.com/web-push-libs/web-push-php
The first thing I wanted was VAPID keys .
filename is vapidkeys.php, this file is inside pushnotification directory, and in pushnotification directory I have web-push-php-master directory.
<?php
require('web-push-php-master');
use Minishlink\WebPush\WebPush;
var_dump(VAPID::createVapidKeys());
?>
But the above line throws following error:
Fatal error: Class 'EccFactory' not found in pushnotification/vapidkeys.php on line 124
Please help me solve this issue, I am new to push notification and namespaces
You have to get web-push-php with Composer, so that all the dependencies are installed.
Install Composer
Run composer require minishlink/web-push. This will install web-push-php and all its dependencies in the vendor folder.
In your PHP file, require it : require __DIR__ . '/vendor/autoload.php';
Here's a basic example that uses web-push-php: https://github.com/Minishlink/web-push-php-example
Hope this helps.
HTTPD Website Only Generate Key,Your website is HTTP can't generate Key