晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。 林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。 见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝) 既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。 南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。
|
Server : Apache System : Linux srv.rainic.com 4.18.0-553.47.1.el8_10.x86_64 #1 SMP Wed Apr 2 05:45:37 EDT 2025 x86_64 User : rainic ( 1014) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/rainic/www/oldTZh/wp-content/plugins/digits/libphonenumber/vendor/starkbank/ecdsa/ |
Upload File : |
## A lightweight and fast PHP ECDSA
### Overview
This is a PHP implementation of the Elliptic Curve Digital Signature Algorithm. It is compatible with PHP 5.5+. Please note that this library relies heavily on the openssl package for PHP, so - depending on your PHP installation - you may need to re-compile it with the "–with-openssl" flag.
### Installation
#### Composer
To install the package with Composer, run:
```sh
composer require starkbank/ecdsa
```
To use the bindings, use Composer's autoload:
```sh
require_once('vendor/autoload.php');
```
### Curves
The module is wrapped around the builtin openssl functions, so all standar curves should be supported. The default is `secp256k1`.
### Speed
We ran a test on a MAC Pro i7 2017. We ran the library 100 times and got the average time displayed bellow:
| Library | sign | verify |
| ------------------ |:-------------:| -------:|
| starkbank-ecdsa | 0.6ms | 0.4ms |
### Sample Code
How to sign a json message for [Stark Bank]:
```php
# Generate privateKey from PEM string
$privateKey = EllipticCurve\PrivateKey::fromPem("
-----BEGIN EC PARAMETERS-----
BgUrgQQACg==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MHQCAQEEIODvZuS34wFbt0X53+P5EnSj6tMjfVK01dD1dgDH02RzoAcGBSuBBAAK
oUQDQgAE/nvHu/SQQaos9TUljQsUuKI15Zr5SabPrbwtbfT/408rkVVzq8vAisbB
RmpeRREXj5aog/Mq8RrdYy75W9q/Ig==
-----END EC PRIVATE KEY-----
");
# Create message from json
$message = array(
"transfers" => array(
array(
"amount" => 100000000,
"taxId" => "594.739.480-42",
"name" => "Daenerys Targaryen Stormborn",
"bankCode" => "341",
"branchCode" => "2201",
"accountNumber" => "76543-8",
"tags" => array("daenerys", "targaryen", "transfer-1-external-id")
)
)
);
$message = json_encode($message, JSON_PRETTY_PRINT);
$signature = EllipticCurve\Ecdsa::sign($message, $privateKey);
# Generate Signature in base64. This result can be sent to Stark Bank in header as Digital-Signature parameter
echo "\n" . $signature->toBase64();
# To double check if message matches the signature
$publicKey = $privateKey->publicKey();
echo "\n" . EllipticCurve\Ecdsa::verify($message, $signature, $publicKey);
```
Simple use:
```php
# Generate new Keys
$privateKey = new EllipticCurve\PrivateKey;
$publicKey = $privateKey->publicKey();
$message = "My test message";
# Generate Signature
$signature = EllipticCurve\Ecdsa::sign($message, $privateKey);
# Verify if signature is valid
echo "\n" . EllipticCurve\Ecdsa::verify($message, $signature, $publicKey);
```
### OpenSSL
This library is compatible with OpenSSL, so you can use it to generate keys:
```
openssl ecparam -name secp256k1 -genkey -out privateKey.pem
openssl ec -in privateKey.pem -pubout -out publicKey.pem
```
Create a message.txt file and sign it:
```
openssl dgst -sha256 -sign privateKey.pem -out signatureDer.txt message.txt
```
It's time to verify:
```php
$publicKeyPem = EllipticCurve\Utils\File::read("publicKey.pem");
$signatureDer = EllipticCurve\Utils\File::read("signatureDer.txt");
$message = EllipticCurve\Utils\File::read("message.txt");
$publicKey = EllipticCurve\PublicKey::fromPem($publicKeyPem);
$signature = EllipticCurve\Signature::fromDer($signatureDer);
echo "\n" . EllipticCurve\Ecdsa::verify($message, $signature, $publicKey);
```
You can also verify it on terminal:
```
openssl dgst -sha256 -verify publicKey.pem -signature signatureDer.txt message.txt
```
NOTE: If you want to create a Digital Signature to use in the [Stark Bank], you need to convert the binary signature to base64.
```
openssl base64 -in signatureDer.txt -out signatureBase64.txt
```
You can also verify it with this library:
```php
$signatureDer = EllipticCurve\Utils\File::read("signatureDer.txt");
$signature = EllipticCurve\Signature::fromDer($signatureDer);
echo "\n" . $signature->toBase64();
```
[Stark Bank]: https://starkbank.com
### Run all unit tests
```sh
php tests/test.php
```
[python-ecdsa]: https://github.com/warner/python-ecdsa