Kevin Eaton

Packages · Open source

Laravel Hash List

Keeps lists of email addresses as hashes. You can use it for a do-not-contact list, a list of banned signups, or a list you swap with a partner. Every spelling of an address matches the same entry, and the address itself is never stored.

Two companies that each hold a do-not-contact list and may not hand each other real addresses. They swap hashes. Then one of them hashes " Jane@Example.com" with the space still on it.

Before

// the addresses in your blocked_emails table
$blocked = ['jane@example.com'];

$emails = [
    'Jane@Example.com',
    ' jane@example.com',
    'Jane Doe <jane@example.com>',
    'riley@example.com',
];

foreach ($emails as $email) {
    $listed = in_array(strtolower(trim($email)), $blocked, true);

    printf("%-30s %s\n", "'{$email}'", $listed ? 'skip' : 'send');
}
'Jane@Example.com'             skip
' jane@example.com'            skip
'Jane Doe <jane@example.com>'  send
'riley@example.com'            send

That catches the capitals and the stray space. It misses the address with a name around it, which is how a mail header spells it, so Jane got the mail. And the list is a table full of the addresses you were asked to leave alone. Here is the same list with the package.

After

use Keaton\HashList\Facades\HashList;

HashList::add('jane@example.com');

foreach ($emails as $email) {
    $listed = HashList::has($email);

    printf("%-30s %s\n", "'{$email}'", $listed ? 'skip' : 'send');
}
'Jane@Example.com'             skip
' jane@example.com'            skip
'Jane Doe <jane@example.com>'  skip
'riley@example.com'            send

All three spellings of Jane are skipped. An address is cleaned up before it is hashed or looked up, so add() and has() always agree. The table holds only the hash, so there is no address in it to leak, and a check is one query on a unique index.

What it does

  • Keep several named lists in one table, like do-not-contact, banned and one for each partner.
  • Import and export lists as hash files. They are streamed, so a list doesn't have to fit in memory.
  • Import a file of plain addresses. Each one is hashed on the way in and is not written anywhere.
  • A NotListed validation rule keeps a listed address out of a signup form, however it is spelled.
  • An optional guard removes listed recipients from every mail your app sends.
  • Use SHA-256, or MD5 when a partner's files use it.

Next packageLaravel Dedupe