iamtgc

reCAPTCHA Mailhide template filter for Django

September 6th, 2007 by tgc

reCAPTCHA’s Mailhide is a captcha that “helps you protect your inbox by asking people to solve a reCAPTCHA before they can view your email address.”

The Mailhide API is well documented, information on it’s inner workings can be found here.

In order to leverage Mailhide, you will need to request your API Key.

Additionally, you’ll also need amk’s Python Cryptography Toolkit. The version of the Python Cryptography Toolkit tested is 2.0.1, but any version that supports AES should work.

Now, once you have installed the necessary packages and requested your API Key, it’s time to put them to use.

In your settings.py you’ll want to add your API Keys in their respective variables, MAILHIDE_PUB_KEY is your Public Key, and MAILHIDE_PRIV_KEY is your Private Key.
MAILHIDE_PUB_KEY = '01QvLWux5x-zHdXXYv-VumOg==' MAILHIDE_PRIV_KEY = '41F69FFAF768AE6ED69A96C15A13252A'

Next, you’ll want to add the filter, I’ve named this mailhide.py and it should reside in your app/templatetags/ directory. If this does not exist, you will need to create it and place an empty __init__.py file in it. Additional information on Extending the template system can be found here.

app/templatetags/mailhide.py (download)
import base64 import binascii from Crypto.Cipher import AES from django.conf import settings from django import template register = template.Library() def pad_string (str, block_size): numpad = block_size - (len (str) % block_size) return str + numpad * chr (numpad) def enc_string(str): key = binascii.unhexlify(settings.MAILHIDE_PRIV_KEY) mode = AES.MODE_CBC iv = '\000' * 16 obj = AES.new(key, mode, iv) return obj.encrypt(str) @register.filter() def mailhide(value): args = {} padded_value = pad_string(value, 16) encrypted_value = enc_string(padded_value) args['public_key'] = settings.MAILHIDE_PUB_KEY args['encrypted_email'] = base64.urlsafe_b64encode(encrypted_value) args['domain'] = value[value.index('@')+1:] return '''<a href="http://mailhide.recaptcha.net/d?k=%(public_key)s&c=%(encrypted_email)s" onclick="window.open('http://mailhide.recaptcha.net/d?k=%(public_key)s&c=%(encrypted_email)s', '', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=300'); return false;" title="Reveal this e-mail address">...@%(domain)s</a>''' % args

This filter expects an email address, fred.flinstone@domain.tld, for example, and will return a link to the captcha titled …@domain.tld.

Here is an example of how you would use this in your template.
templates/app/index.html
{% load mailhide %} {{ user_obj.email|mailhide }}

Enjoy!

Please leave any feedback in the comments.

Posted in Django

2 Responses

  1. Using reCAPTCHA with Python (and Django) « free sofware tour guide

    [...] see? it’s really simple. Now download this and start playing.Also check this, tgc has developed a django template filter allowing the use of reCAPTCHA’s mailhide directly [...]

  2. Artagnon

    Awesome! I found this really useful, thanks :)

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.