cheich/cryptonite

This package is abandoned and no longer maintained. No replacement package was suggested.

Encryption and password hashing

dev-master 2017-09-22 16:20 UTC

This package is not auto-updated.

Last update: 2022-03-19 13:39:15 UTC


README

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...";
}