Git Product home page Git Product logo

petrknap / php-crypto-sodium Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 1.0 55 KB

A simple library that packages functional `sodium_crypt_*` into objects.

License: GNU Lesser General Public License v3.0

Dockerfile 2.52% PHP 97.48%
chacha20 cryptography curve25519 decryption encryption php php-library poly1305 security x25519 sodium asymmetric-cryptography block-cipher public-key-cryptography stream-cipher symmetric-cryptography xchacha20 secret-key-cryptography aes aead

php-crypto-sodium's Introduction

Crypto Sodium

A simple library that packages functional sodium_crypt_* into objects.

Inputs and outputs are binary data, don't be afraid to use the petrknap/binary.

Examples

Symmetric block encryption

use PetrKnap\CryptoSodium\SecretBox;

$secretBox = new SecretBox();
$message = 'Hello World!';
$key = $secretBox->generateKey();

$ciphertext = $secretBox->encrypt($message, $key);

echo $secretBox->decrypt($ciphertext, $key);

$secretBox->eraseData($key);

Asymmetric block encryption

use PetrKnap\CryptoSodium\Box;

$box = new Box();
$message = 'Hello World!';
$keyPair = $box->generateKeyPair();

$ciphertext = $box->encrypt($message, $keyPair);

echo $box->decrypt($ciphertext, $keyPair);

$box->eraseData($keyPair);

Symmetric stream encryption

use PetrKnap\CryptoSodium\SecretStream\XChaCha20Poly1305;

$xChaCha20Poly1305 = new XChaCha20Poly1305();
$messageChunk1 = 'Hello ';
$messageChunk2 = 'World!';
$key = $xChaCha20Poly1305->generateKey();

$pushStream = $xChaCha20Poly1305->initPush($key);
$ciphertextHeader = $pushStream->header;
$ciphertextChunk1 = $pushStream->push($messageChunk1);
$ciphertextChunk2 = $pushStream->push($messageChunk2, tag: XChaCha20Poly1305::TAG_FINAL);

$pullStream = $xChaCha20Poly1305->initPull($ciphertextHeader, $key);
echo $pullStream->pull($ciphertextChunk1);
echo $pullStream->pull($ciphertextChunk2);

$xChaCha20Poly1305->eraseData($key);

Symmetric block encryption with additional data

use PetrKnap\CryptoSodium\Aead\Aes256Gcm;

$aes256Gcm = new Aes256Gcm();
$message = 'Hello World!';
$purpose = 'example';
$key = $aes256Gcm->generateKey();

$ciphertext = $aes256Gcm->encrypt($message, $key, additionalData: $purpose);

echo $aes256Gcm->decrypt($ciphertext, $key, additionalData: $purpose);

$aes256Gcm->eraseData($key);

Run composer require petrknap/crypto-sodium to install it. You can support this project via donation. The project is licensed under the terms of the LGPL-3.0-or-later.

php-crypto-sodium's People

Contributors

petrknap avatar

Stargazers

greenarmor avatar

Watchers

 avatar

Forkers

mozhdehtanha

php-crypto-sodium's Issues

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.