diff --git a/src/calibre/ebooks/conversion/plugins/epub_input.py b/src/calibre/ebooks/conversion/plugins/epub_input.py index 409c128836..9d29fbbbf2 100644 --- a/src/calibre/ebooks/conversion/plugins/epub_input.py +++ b/src/calibre/ebooks/conversion/plugins/epub_input.py @@ -3,7 +3,7 @@ __license__ = 'GPL 3' __copyright__ = '2009, Kovid Goyal ' __docformat__ = 'restructuredtext en' -import os +import os, re from itertools import cycle from calibre.customize.conversion import InputFormatPlugin, OptionRecommendation @@ -37,9 +37,10 @@ class EPUBInput(InputFormatPlugin): def process_encryption(self, encfile, opf, log): from lxml import etree import uuid, hashlib - idpf_key = opf.unique_identifier + idpf_key = opf.raw_unique_identifier if idpf_key: - idpf_key = hashlib.sha1(idpf_key).digest() + idpf_key = re.sub(u'\u0020\u0009\u000d\u000a', u'', idpf_key) + idpf_key = hashlib.sha1(idpf_key.encode('utf-8')).digest() key = None for item in opf.identifier_iter(): scheme = None diff --git a/src/calibre/ebooks/oeb/polish/container.py b/src/calibre/ebooks/oeb/polish/container.py index 5dd478b1ce..c5c0c5e16e 100644 --- a/src/calibre/ebooks/oeb/polish/container.py +++ b/src/calibre/ebooks/oeb/polish/container.py @@ -940,7 +940,7 @@ class EpubContainer(Container): if not fonts: return - package_id = unique_identifier = idpf_key = None + package_id = raw_unique_identifier = idpf_key = None for attrib, val in self.opf.attrib.iteritems(): if attrib.endswith('unique-identifier'): package_id = val @@ -948,10 +948,12 @@ class EpubContainer(Container): if package_id is not None: for elem in self.opf_xpath('//*[@id=%r]'%package_id): if elem.text: - unique_identifier = elem.text.rpartition(':')[-1] + raw_unique_identifier = elem.text break - if unique_identifier is not None: - idpf_key = hashlib.sha1(unique_identifier).digest() + if raw_unique_identifier is not None: + idpf_key = raw_unique_identifier + idpf_key = re.sub(u'\u0020\u0009\u000d\u000a', u'', idpf_key) + idpf_key = hashlib.sha1(idpf_key.encode('utf-8')).digest() key = None for item in self.opf_xpath('//*[local-name()="metadata"]/*' '[local-name()="identifier"]'):