This is the example I built while learning how to properly protect password before they are stored in the database. I’ve seen dozens of plugins simply take in API paramters like “username and password” and just store those values in the database for use. How horrible is that! We decided to do the right thing and learn about encryption security for use with our plugin. YOU SHOULD DO THE SAME!
Why aren’t we hashing the passwords!?! Hashing passwords is safer, however some passwords are used for “3rd party tools” and therefore can’t be hashed because they need to be “unencrypted” and sent to the 3rd party service. If you hash the password, you can only “compare hashes” to see if local PHP logic should pass or fail, while the API tools require we have the real password for remote authentication.
In my case, we were building an connector between WordPress and Salesforce.com, which requires a user/pass/secretkey for the SOAP API tools. I ended up using the “secretkey” as my unique key for encrypting… making the site a little safer. Just make sure, whatever you choose for a key, IS STATIC. I originally used __DIR__ and then moved the project from local dev to live and it broke the decryption process because the secret key changed.
(rather then post the code here, I decided to leave it at github, that way when somebody points out any/all of my mistakes, I can update the code for everyone).