Skip to content
This repository has been archived by the owner on Mar 13, 2022. It is now read-only.

cheich/Cryptonite

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cryptonite - the hacker's kryptonite

Features

  • Asymmetric encryption
  • Symmetric encryption
  • Password hashing

Install

Composer

Command Line

composer require cheich/cryptonite

composer.json

{
  "require": {
    "cheich/cryptonite": "dev-master"
  }
}

Requirements

  • PHP >= 5.3.3
  • OpenSSL >= 0.9.6 - Should not between 1.0.1 and 1.0.1f (prevent Heardbleed exploits, see http://heartbleed.com/)
  • Place minimalistic openssl.cnf file near Cryptonite.php as fall-back

Examples

Symmetric encryption

Encrypt data

try {
  $symmetric = new SymmetricEncryption();
  $encrypted = $symmetric->encrypt('String to encrypt', 'MyPassphrase');
  $iv        = $symmetric->getInitVector();
} catch (EncryptionException $e) {
  echo "Something went wrong...";
}

Decrypt data

try {
  $symmetric = new SymmetricEncryption();
  $encrypted = /* get encrypted data from database */;
  $iv        = /* get iv from database */;

  $decrypted = $symmetric->decrypt($encrypted, 'MyPassphrase', $iv);
} catch (EncryptionException $e) {
  echo "Something went wrong...";
}

Asymetric encryption

Generate private/public key pair

try {
  $asymmetric = new AsymmetricEncryption();
  $publicKey  = $asymmetric->getPublicKey(); // or export to a file
  $privateKey = $asymmetric->getPrivateKey('MyPassphrase'); // or export to a file
  $encrypted  = $asymmetric->encrypt('String to encrypt', $publicKey);
} catch (EncryptionException $e) {
  echo "Something went wrong...";
}

Decrypt with private key

try {
  $asymmetric = new AsymmetricEncryption();
  $privateKey = /* get private key */;
  $encrypted  = /* get encrypted data */;

  $decrypted = $asymmetric->decrypt($encrypted, $privateKey, 'MyPassphrase');
} catch (EncryptionException $e) {
  echo "Something went wrong...";
}

Passwords

Hash password

try {
  $password = new Password();
  $hashed   = $password->hash('MyPassphrase');
} catch (EncryptionException $e) {
  echo "Something went wrong...";
}

Verify password

try {
  $password = new Password();
  $hashed   = /* get hashed password */;

  // Returns true or false
  $password->verify('MyPassphrase', $hashed);
} catch (EncryptionException $e) {
  echo "Something went wrong...";
}

Releases

No releases published

Packages

No packages published

Languages