PHP code example of i-avatar777 / service-ecdsa

1. Go to this page and download the library: Download i-avatar777/service-ecdsa library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

i-avatar777 / service-ecdsa example snippets


use iAvatar777\services\EllipticCurve\Ecdsa;
use iAvatar777\services\EllipticCurve\PrivateKey;

# Generate privateKey from PEM string
$privateKey = 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 = Ecdsa::sign($message, $privateKey);

# Generate Signature in base64.
echo "\n" . $signature->toBase64();

# To double check if message matches the signature
$publicKey = $privateKey->publicKey();

echo "\n" . Ecdsa::verify($message, $signature, $publicKey);

# Generate new Keys
use iAvatar777\services\EllipticCurve\PrivateKey;
use iAvatar777\services\EllipticCurve\Ecdsa;

$privateKey = new PrivateKey;
$publicKey = $privateKey->publicKey();

$message = "My test message";

# Generate Signature
$signature = Ecdsa::sign($message, $privateKey);

# Verify if signature is valid
echo "\n" . Ecdsa::verify($message, $signature, $publicKey);

use iAvatar777\services\EllipticCurve\Utils\File;
use iAvatar777\services\EllipticCurve\PublicKey;
use iAvatar777\services\EllipticCurve\Signature;
use iAvatar777\services\EllipticCurve\Ecdsa;

$publicKeyPem = File::read("publicKey.pem");
$signatureDer = File::read("signatureDer.txt");
$message = File::read("message.txt");

$publicKey = PublicKey::fromPem($publicKeyPem);
$signature = Signature::fromDer($signatureDer);

echo "\n" . Ecdsa::verify($message, $signature, $publicKey);

use iAvatar777\services\EllipticCurve\Utils\File;
use iAvatar777\services\EllipticCurve\Signature;

$signatureDer = File::read("signatureDer.txt");

$signature = Signature::fromDer($signatureDer);

echo "\n" . $signature->toBase64();
sh
php tests/test.php