Skip to content

Instantly share code, notes, and snippets.

@aaronranard
Created January 28, 2016 22:55
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 aaronranard/62aed8148395deafed71 to your computer and use it in GitHub Desktop.
Save aaronranard/62aed8148395deafed71 to your computer and use it in GitHub Desktop.
PHP: PHPECC
<?php
$loader = new PemLoader();
$math = EccFactory::getAdapter();
$this->messages = new MessageFactory($math);
$privKeySerializer = new PemPrivateKeySerializer(new DerPrivateKeySerializer());
$pubKeySerializer = new PemPublicKeySerializer(new DerPublicKeySerializer());
$PrivateKeyPath = env('KEY_LOCATION').'my.priv';
$PublicKeyPath = storage_path().'/app/'.uniqid().'.pub';
$key = str_replace(" ", "+", Input::get('key'));
file_put_contents($PublicKeyPath, "-----BEGIN PUBLIC KEY-----\r\n". $key ."\r\n-----END PUBLIC KEY-----");
$private = $privKeySerializer->parse($loader->loadPrivateKeyData($PrivateKeyPath));
$public = $pubKeySerializer->parse($loader->loadPublicKeyData($PublicKeyPath));
$privateDh = $private->createExchange($this->messages, $public);
$key = $privateDh->calculateSharedKey();
return $key;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment