Scout APM is PHP application performance monitoring designed for developers. With tracing logic that ties issues back to the line of code causing them, you can pinpoint n+1 queries, memory leaks, and other abnormalities in real time so you can knock them out and get back to building a great product. Start your free 14-day trial today and get the performance insight you need in less than 4 minutes.

Encrypting and signing data using private/public keys in PHP

Original – by Freek Van der Herten – 2 minute read

For a project, I needed to make sure that a particular piece of data actually came from a specific source. There are already many packages that allow you to do this, but most are not fun or easy to use. That's why we created a new package called spatie/crypto to do this.

Using spatie/crypto

Using this package, it's easy to generate a private and public key.

[$privateKey, $publicKey] = (new Spatie\Crypto\RsaKeyPair())->generate();

When passing paths, the generated keys will be passed to those paths.

(new KeyPair())->generate($pathToPrivateKey, $pathToPublicKey);

Using a private key, you can sign a message.

$privateKey = Spatie\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey);
$signature = $privateKey->sign('my message'); // returns a string

The public key can use the signature to determine that the message was not tampered with.

$publicKey = Spatie\Crypto\Rsa\PublicKey::fromFile($pathToPublicKey);

$publicKey->verify('my message', $signature) // returns true;
$publicKey->verify('my modified message', $signature) // returns false;
$publicKey->verify('my message', 'invalid signature') // returns false;

Alternatives

This package aims to be very lightweight and easy to use. If you need more features, consider using of one these alternatives:

A word on the usage of RSA

At the time of writing, RSA is secure enough for the use case we've built this package for.

To know more about why RSA might not be good enough for you, read this post on public-key encryption at Paragonie.com

In closing

Spatie/crypt can also encrypt and decrypt messages. To learn more, head over to the readme of spatie/crypto on GitHub.

On our company website, you'll find a list of packages our team has created previously. If you would like to support us, consider picking up one of our paid products or sponsoring us on GitHub.

Stay up to date with all things Laravel, PHP, and JavaScript.

Follow me on Twitter. I regularly tweet out programming tips, and what I myself have learned in ongoing projects.

Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

Webmentions

Manojkiran liked on 12th November 2020
Kenneth L liked on 12th November 2020
ReynerHL liked on 11th November 2020
Laravel 360 liked on 11th November 2020
Barnabas Kecskes liked on 11th November 2020
zero1 liked on 11th November 2020
mojtabaa hn liked on 11th November 2020
Abu Hurairah liked on 11th November 2020
Volkan Baskın liked on 10th November 2020
Bikash Khatri liked on 10th November 2020
Danijel K. liked on 10th November 2020
Martin Medina liked on 10th November 2020
ali ali liked on 10th November 2020
Hirusha Cooray liked on 10th November 2020
Fajar Rizky Hidayat liked on 10th November 2020
Jason Kenyon liked on 10th November 2020
Simpledev liked on 10th November 2020
Hardik Shah liked on 10th November 2020
Hardik Shah liked on 10th November 2020
Abhishek Jain liked on 10th November 2020
Abhishek Jain liked on 10th November 2020
Amir liked on 10th November 2020
Amir liked on 10th November 2020
Zaknafein liked on 10th November 2020
Yassen Sayed liked on 10th November 2020
Roman Pronskiy liked on 10th November 2020
Oilmone liked on 10th November 2020
Ronald Rotteveel liked on 10th November 2020
Loki liked on 10th November 2020
Amer Anwar liked on 10th November 2020
yaslam liked on 10th November 2020
شريف مراد liked on 10th November 2020
Gaël Reyrol liked on 10th November 2020
Daniele Rosario liked on 10th November 2020
Oluwatobi Samuel Omisakin liked on 10th November 2020
Yann Haefliger liked on 10th November 2020
Shadow liked on 10th November 2020
Roberto B 🚀 liked on 10th November 2020
Christopher Pearse liked on 10th November 2020
Tauseef shah liked on 10th November 2020
Tauseef shah liked on 10th November 2020
NoMad42 liked on 10th November 2020
Enzo Notario liked on 10th November 2020
Chew liked on 10th November 2020
Enzo Notario liked on 10th November 2020
Mohan Raj liked on 10th November 2020
Paco Orozco liked on 10th November 2020
Mohan Raj liked on 10th November 2020
José Pais liked on 10th November 2020
newwave liked on 10th November 2020
Andrew Woods liked on 10th November 2020
Jake Casto liked on 10th November 2020
Ernest Chiang liked on 10th November 2020
Le Huu Phuc liked on 10th November 2020
Leonardo Prabangkoro liked on 10th November 2020
Paulo Freitas 🇧🇷 liked on 10th November 2020
Fran Delgado liked on 10th November 2020
Julio Serpone liked on 10th November 2020
Luigi Cruz liked on 10th November 2020
Jānis Lācis liked on 10th November 2020
Julio Serpone liked on 10th November 2020
Jess Archer liked on 10th November 2020
TheSteed liked on 10th November 2020
TheSteed liked on 10th November 2020
Avenger liked on 10th November 2020
π liked on 10th November 2020
Oilmone liked on 10th November 2020
Nanderson Castro liked on 10th November 2020
Vaggelis Yfantis liked on 10th November 2020
Jeremy Gimbel liked on 10th November 2020
!Alf In The Movies! liked on 10th November 2020
Daniel Ramirez liked on 10th November 2020
kanamastaka liked on 9th November 2020
Stephen Shead liked on 9th November 2020
Mahdi Jedari liked on 9th November 2020
Aleksandar Mitić liked on 9th November 2020
Salman Zafar liked on 9th November 2020
Atish Goswami retweeted on 9th November 2020
Azerdaoui Othmane liked on 9th November 2020
Owen Andrews liked on 9th November 2020
Akin liked on 9th November 2020
Phil Rowley liked on 9th November 2020
#SARSMUSTEND ! Josiah Yahaya liked on 9th November 2020
Andreas Heigl retweeted on 9th November 2020
Scott Dutton liked on 9th November 2020
Andreas Heigl liked on 9th November 2020
Lennart Fischer liked on 9th November 2020
Vaggelis Yfantis liked on 9th November 2020
Web Developer MF liked on 9th November 2020
Gregori Piñeres liked on 9th November 2020
सोझो केटो liked on 9th November 2020
Alex Goller liked on 9th November 2020
Sadegh PM liked on 9th November 2020
Mu96afa liked on 9th November 2020
Nathan Snyder 🦇 liked on 9th November 2020
Abrar Ahmad liked on 9th November 2020
Omar Andrés Barbosa Ortiz liked on 9th November 2020
José Cage liked on 9th November 2020
guille~1.txt liked on 9th November 2020
Norbert Tóth liked on 9th November 2020
José Cage liked on 9th November 2020
Sid 🏄 retweeted on 9th November 2020
Sid 🏄 liked on 9th November 2020
apo ⚡️ liked on 9th November 2020
ArielMejiaDev retweeted on 9th November 2020
Faisal Khan retweeted on 9th November 2020
ArielMejiaDev liked on 9th November 2020
Owen Voke retweeted on 9th November 2020
Songhua Hu liked on 9th November 2020
Jānis Lācis liked on 9th November 2020
Faisal Khan liked on 9th November 2020
Niko Halink liked on 9th November 2020
Ankit jain 🐋 liked on 9th November 2020
JOHN DOE liked on 9th November 2020
23slider liked on 9th November 2020
vluzrmos liked on 9th November 2020
BinaryKitten (Kat) retweeted on 9th November 2020
Ahmet Elçik liked on 9th November 2020
Shadow liked on 9th November 2020
Dep Infect retweeted on 9th November 2020
Faruk Nasir liked on 9th November 2020
BinaryKitten (Kat) liked on 9th November 2020
Sebastiaan Luca 🤖 liked on 9th November 2020
JOHN DOE liked on 9th November 2020
Dalton Sutton liked on 9th November 2020
Songhua Hu liked on 9th November 2020
Dep Infect liked on 9th November 2020
. liked on 9th November 2020
Ruben Van Assche liked on 9th November 2020
Ahmet Mirzabeyoğlu liked on 9th November 2020
Spatie retweeted on 9th November 2020
Nick Ciske liked on 9th November 2020
Ahmet Mirzabeyoğlu retweeted on 9th November 2020
thao liked on 9th November 2020
Owen Voke liked on 9th November 2020
Willan Correia liked on 9th November 2020
Tiagosimoes liked on 9th November 2020
Rafael Delgado liked on 9th November 2020
Tiago Rodrigues liked on 9th November 2020
Robin Dirksen liked on 9th November 2020
Cyril de Wit liked on 9th November 2020
@MenjilX liked on 9th November 2020
Alexander Six liked on 9th November 2020
Faruk Nasir liked on 9th November 2020
Manojkiran liked on 9th November 2020
Sadegh PM liked on 9th November 2020
Hassan liked on 9th November 2020
Owen Voke liked on 9th November 2020
Web Design Lumajang retweeted on 9th November 2020
Ryan Chandler liked on 9th November 2020
Web Design Lumajang liked on 9th November 2020
Jimit 👨🏽‍💻 liked on 9th November 2020
Ian Rodrigues liked on 9th November 2020
Marco Messa liked on 9th November 2020
Hadi Swais liked on 9th November 2020
Roberto B 🚀 liked on 9th November 2020
ahgood liked on 9th November 2020
Tom Witkowski liked on 9th November 2020
Wayne liked on 9th November 2020
José Francisco liked on 9th November 2020
Dhani liked on 9th November 2020
Peter Brinck 🤘 liked on 9th November 2020
Piotr Gumienny liked on 9th November 2020
Bruno CHIREZ liked on 9th November 2020
Robin Dirksen liked on 9th November 2020
Doug Black Jr 🦃🦃🦃🦃 liked on 9th November 2020
Amit Merchant liked on 9th November 2020
Anggit Ari Utomo 🐘 liked on 9th November 2020
🐘 Wolfgang Blessen liked on 9th November 2020
Mark Topper liked on 9th November 2020