Home arrow PHP arrow PHP: Building A Simple Password Encrypter

PHP: Building A Simple Password Encrypter

If you use design patterns in your daily PHP programming work and want to learn how to implement another one, then hopefully this four-part series of articles has been instructive. It demonstrated how to create easily immutable value objects by means of the Value Object pattern. In this last part, you'll learn how to use this design pattern for encrypting passwords.

  1. PHP: Building A Simple Password Encrypter
  2. Regenerating passwords via a simple method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
November 15, 2010

print this article



Needless to say, if you've already read all of the articles previous to this one, you probably have a solid background in how to apply the pattern in distinct use cases. Over the course of those tutorials I demonstrated, with some practical examples, how to create different types of immutable objects. These ranged from simple URL objects to HTML widgets capable of rendering simple div elements on screen.

This doesn’t imply, however, that the implementation of the Value Object pattern must be restricted to generating the kind of objects just mentioned. The schema can be easily extended to other objects as well, depending on the domain layer being modeled. With that premise in mind, in this final installment of the series I’m going to create yet another easy-to-follow example. It will show how build immutable objects whose functionality will be focused on encrypting passwords (or any other string value) via a set of discrete methods.

With the goal of this article already set, it’s time to tackle the last chapter of this series on implementing the Value Object pattern in PHP. Let’s jump in!

Getting started: building a basic class for encrypting passwords

As I stated in the introduction, I plan to implement the Value Object pattern in the construction of a basic password encrypting class. Similar to the examples developed previously, it will be immutable as well. Since the term “encrypt” describes a somewhat generic action, the class will be an implementer of an interface called “EncrypterInterface,” which will define the structure of generic encrypting objects. The definition of this interface looks like this:



interface EncrypterInterface
    public function encrypt($value); 

As you can see, the above interface is very simple, as it only declares a single method called “encrypt().” With that base structure already set on top of the hierarchy, the next step is to define the implementer of the interface, which will be a class tasked with constructing immutable password encrypting objects.

Having said that, the definition of this class (at least at its initial stage) will be as follows:  



final class PasswordEncrypter implements EncrypterInterface
    private $_password;
    public function __construct($password)
        if (!is_string($password) || empty($password)) {
            throw new PasswordEncrypterException('The specified password is invalid.');
        $this->_password = $this->encrypt($password);
    // get the encrypted password
    public function getPassword()
        return $this->_password;
    // encrypt the password
    public function encrypt($value)
        return sha1($value . (string) microtime(true));

As with other immutable classes that you saw in earlier parts of this series, the one above acts like a raw data container. It stores the password passed through the constructor, after being encrypted via a “sha1” hash. Naturally, it’s possible to apply more complex approaches to encrypting values, but for the sake of clarity I’m going to keep the class simple.

So far, half of the goal has been accomplished. The “PasswordEncrypter” class is effectively capable of generating immutable value objects. But, is that all of the behavior that it will be given? To be frank, it’d be helpful to extend its current functionality a bit further; right now, it does nothing particularly interesting.

To satisfy this requirement, and make things a little more exciting, in the next section I’m going to add an additional method to the class. This method will be responsible for generating new encrypted passwords, while preserving the class’s immutability.

This sounds pretty good, right? However, to learn how this method will be implemented, you’ll have to read the following segment. Fear not; it’s only one click away.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: