yii-phpseclib

Yii wrapper around phpseclib 0.3.6
6 followers

Yii PhpSecLib is a Yii wrapper around phpseclib 0.3.6

Support:

  • BigIntegers
  • RSA
  • SSH2
  • SFTP
  • X.509
  • Symmetric key encryptions (AES, Rijndael, Twofish, Blowfish, DES, 3DES, RC4 and RC2)
  • Hashes (md2, md5, md5-96, sha1, sha1-96, sha256, sha384, and sha512)
  • generate random bytes

Changelog

0.2.1:

Requirements

  • Yii 1.1.x
  • Optional: mcrypt, gmp or bcmath php extension

Installation

  1. Copy the phpseclib directory to your protected/extensions folder.

  2. Add to your components in config/main.php:

return array(
  // ...
  'components' => array(
    // ...
    'phpseclib' => array(
      'class' => 'ext.phpseclib.PhpSecLib'
    ),
    // ...
  )
  // ...
)

Examples:

Component like usage:

BigIntegers

// http://phpseclib.sourceforge.net/math/examples.html
$a = Yii::app()->phpseclib->createBigInteger(5);
$b = Yii::app()->phpseclib->createBigInteger(30);
echo $a->add($b); // 35
// http://phpseclib.sourceforge.net/math/examples.html#isprime
var_dump(Yii::app()->phpseclib->createBigInteger(15485863)->isPrime()); // bool(true)

RSA

// http://phpseclib.sourceforge.net/rsa/examples.html
$keys = Yii::app()->phpseclib->createRSA()->createKey();
print_r($keys);
// http://phpseclib.sourceforge.net/rsa/examples.html
$keys = Yii::app()->phpseclib->createRSA()->createKey();
$rsa = Yii::app()->phpseclib->createRSA();
$rsa->loadKey($keys["publickey"]);
$ciphertext = $rsa->encrypt("hello world");
 
$rsa = Yii::app()->phpseclib->createRSA();
$rsa->loadKey($keys["privatekey"]);
 
echo $rsa->decrypt($ciphertext); // hello world
echo $keys["publickey"];         // -----BEGIN PUBLIC KEY-----MIGf....QAB-----END PUBLIC KEY-----
echo $keys["privatekey"];        // -----BEGIN RSA PRIVATE KEY-----MIIC....ig==-----END RSA PRIVATE KEY-----

SSH2

// http://phpseclib.sourceforge.net/ssh/auth.html
$ssh = Yii::app()->phpseclib->createSSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
  exit('Login Failed');
}
echo $ssh->exec('pwd');

X.509

// http://phpseclib.sourceforge.net/x509/examples.html#getpublickey
$x509 = Yii::app()->phpseclib->createX509();
$cert = $x509->loadX509("...");
echo $x509->getPublicKey();

Symmetric key encryption:

// http://phpseclib.sourceforge.net/crypt/examples.html
$cipher = Yii::app()->phpseclib->createAES();
$cipher->setKey('abcdefghijklmnopijklmnop');
$encrypted = $cipher->encrypt("helloworld");
 
$cipher = Yii::app()->phpseclib->createAES();
$cipher->setKey('abcdefghijklmnopijklmnop');
echo $cipher->decrypt($encrypted);

Hash

echo bin2hex(Yii::app()->phpseclib->hash("md2", "test"))."\n";
echo bin2hex(Yii::app()->phpseclib->hash("md5", "test"))."\n";
echo bin2hex(Yii::app()->phpseclib->hash("md5-96", "test"))."\n";
echo bin2hex(Yii::app()->phpseclib->hash("sha1", "test"))."\n";
echo bin2hex(Yii::app()->phpseclib->hash("sha1-96", "test"))."\n";
echo bin2hex(Yii::app()->phpseclib->hash("sha256", "test"))."\n";
echo bin2hex(Yii::app()->phpseclib->hash("sha384", "test"))."\n";
echo bin2hex(Yii::app()->phpseclib->hash("sha512", "test"))."\n";

Random

var_dump(bin2hex(Yii::app()->phpseclib->random(5))); // string(10) "d7244e5da1"

phpseclib like usage:

please add the "phpseclib" to the preload section in the config:

'preload'=>array('log','phpseclib'),

and after that you can use the normal instantiation, for example:

$cipher = new Crypt_AES();
$a = new Math_BigInteger(5);
$sftp = new Net_SFTP('www.domain.tld');
// etc.

phpseclib constants

if you want to use the phpseclib constants, please use the preload method, for example:

Yii::app()->phpseclib->preload("Crypt_AES");
$cipher = new Crypt_AES(CRYPT_AES_MODE_ECB);
Yii::app()->phpseclib->preload("Crypt_AES");
$cipher = Yii::app()->phpseclib->createAES(CRYPT_AES_MODE_ECB);
// http://phpseclib.sourceforge.net/ssh/examples.html#logging
Yii::app()->phpseclib->preload('Net_SSH2');
define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);
$ssh = new Net_SSH2('www.domain.ltd'); // or: $ssh = Yii::app()->phpseclib->createSSH2('www.domain.ltd');
// ...
echo $ssh->getLog();

Resources

Be the first person to leave a comment

Please to leave your comment.

Create extension