from __future__ import print_function, division, absolute_import

import hmac
import hashlib

from flask import url_for

from wqflask import app

def hmac_creation(stringy):
    """Helper function to create the actual hmac"""

    secret = app.config['SECRET_HMAC_CODE']

    hmaced = hmac.new(secret, stringy, hashlib.sha1)
    hm = hmaced.hexdigest()
    # ZS: Leaving the below comment here to ask Pjotr about
    # "Conventional wisdom is that you don't lose much in terms of security if you throw away up to half of the output."
    # http://www.w3.org/QA/2009/07/hmac_truncation_in_xml_signatu.html
    hm = hm[:20]
    return hm

def data_hmac(stringy):
    """Takes arbitray data string and appends :hmac so we know data hasn't been tampered with"""
    return stringy + ":" + hmac_creation(stringy)

def url_for_hmac(endpoint, **values):
    """Like url_for but adds an hmac at the end to insure the url hasn't been tampered with"""

    url = url_for(endpoint, **values)

    hm = hmac_creation(url)
    if '?' in url:
        combiner = "&"
    else:
        combiner = "?"
    return url + combiner + "hm=" + hm

app.jinja_env.globals.update(url_for_hmac=url_for_hmac,
                             data_hmac=data_hmac)
@@ -139,16 +139,17 @@
 rt app%0A%0A
+%0A
 def hmac
@@ -265,17 +265,16 @@
 _CODE'%5D%0A
-%0A
     hmac
@@ -630,16 +630,17 @@
 urn hm%0A%0A
+%0A
 def data
@@ -675,16 +675,17 @@
  arbitra
+r
 y data s
@@ -804,16 +804,17 @@
 ringy)%0A%0A
+%0A
 def url_
@@ -1121,16 +1121,17 @@
 %22 + hm%0A%0A
+%0A
 app.jinj
@@ -1178,16 +1178,16 @@
 r_hmac,%0A
-
         
@@ -1227,8 +1227,9 @@
 ta_hmac)
+%0A

Apply pep8