Coder Social home page Coder Social logo

cryptonite's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.