Add CanonicalizationPolicy, which encapsulates the combined hybrid simple/relaxed schemes.

This commit is contained in:
William Grant
2011-06-04 14:05:54 +10:00
parent 19b554212e
commit 7b1a3f70dc
2 changed files with 21 additions and 3 deletions
+7 -3
View File
@@ -25,7 +25,10 @@ import logging
import re import re
import time import time
from dkim.canonicalization import algorithms from dkim.canonicalization import (
algorithms,
CanonicalizationPolicy,
)
from dkim.crypto import ( from dkim.crypto import (
DigestTooLargeError, DigestTooLargeError,
HASH_ALGORITHMS, HASH_ALGORITHMS,
@@ -334,8 +337,9 @@ def verify(message, logger=None, dnsfunc=get_txt):
except KeyError as e: except KeyError as e:
logger.error("unknown canonicalization algorithm: %s" % e.message) logger.error("unknown canonicalization algorithm: %s" % e.message)
return False return False
headers = header_algorithm.canonicalize_headers(headers) canon_policy = CanonicalizationPolicy(header_algorithm, body_algorithm)
body = body_algorithm.canonicalize_body(body) headers = canon_policy.canonicalize_headers(headers)
body = canon_policy.canonicalize_body(body)
try: try:
hasher = HASH_ALGORITHMS[sig[b'a']] hasher = HASH_ALGORITHMS[sig[b'a']]
+14
View File
@@ -23,6 +23,7 @@ import re
__all__ = [ __all__ = [
'algorithms', 'algorithms',
'CanonicalizationPolicy',
] ]
@@ -83,4 +84,17 @@ class Relaxed:
compress_whitespace(strip_trailing_whitespace(body))) compress_whitespace(strip_trailing_whitespace(body)))
class CanonicalizationPolicy:
def __init__(self, header_algorithm, body_algorithm):
self.header_algorithm = header_algorithm
self.body_algorithm = body_algorithm
def canonicalize_headers(self, headers):
return self.header_algorithm.canonicalize_headers(headers)
def canonicalize_body(self, body):
return self.body_algorithm.canonicalize_body(body)
algorithms = dict((c.name, c) for c in (Simple, Relaxed)) algorithms = dict((c.name, c) for c in (Simple, Relaxed))