From 9df15634928aaccdbe032fb7ee179704ac07f007 Mon Sep 17 00:00:00 2001 From: Jeremy Whiting Date: Thu, 1 Oct 2020 22:41:57 -0600 Subject: [PATCH 1/3] Use open instead of file() to export keys to file. Fixes export of Kindle keys in calibre 5.0.1 here. --- DeDRM_plugin/config.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/DeDRM_plugin/config.py b/DeDRM_plugin/config.py index 93f69ae..ad6e82e 100644 --- a/DeDRM_plugin/config.py +++ b/DeDRM_plugin/config.py @@ -431,15 +431,15 @@ class ManageKeysDialog(QDialog): defaultname = "{0}.{1}".format(keyname, self.keyfile_ext) filename = choose_save_file(self, unique_dlg_name, caption, filters, all_files=False, initial_filename=defaultname) if filename: - with file(filename, 'wb') as fname: + with open(filename, 'wb') as fname: if self.binary_file: fname.write(self.plugin_keys[keyname].decode('hex')) elif self.json_file: - fname.write(json.dumps(self.plugin_keys[keyname])) + fname.write(json.dumps(self.plugin_keys[keyname]).encode()) elif self.android_file: for key in self.plugin_keys[keyname]: - fname.write(key) - fname.write("\n") + fname.write(key.encode()) + fname.write(b"\n") else: fname.write(self.plugin_keys[keyname]) From 62e0a69089b7061fe91022d3118352e001af66b0 Mon Sep 17 00:00:00 2001 From: Jeremy Whiting Date: Fri, 2 Oct 2020 00:17:47 -0600 Subject: [PATCH 2/3] Fix launching help link from customization dialog. To fix error with python3 when launching help link open files in binary mode. --- DeDRM_plugin/config.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DeDRM_plugin/config.py b/DeDRM_plugin/config.py index ad6e82e..68c6c28 100644 --- a/DeDRM_plugin/config.py +++ b/DeDRM_plugin/config.py @@ -153,7 +153,7 @@ class ConfigWidget(QWidget): # Copy the HTML helpfile to the plugin directory each time the # link is clicked in case the helpfile is updated in newer plugins. file_path = os.path.join(config_dir, "plugins", "DeDRM", "help", help_file_name) - with open(file_path,'w') as f: + with open(file_path,'wb') as f: f.write(self.load_resource(help_file_name)) return file_path url = 'file:///' + get_help_file_resource() @@ -357,7 +357,7 @@ class ManageKeysDialog(QDialog): # link is clicked in case the helpfile is updated in newer plugins. help_file_name = "{0}_{1}_Help.htm".format(PLUGIN_NAME, self.key_type_name) file_path = os.path.join(config_dir, "plugins", "DeDRM", "help", help_file_name) - with open(file_path,'w') as f: + with open(file_path,'wb') as f: f.write(self.parent.load_resource(help_file_name)) return file_path url = 'file:///' + get_help_file_resource() From dca0cf7d00ae40f04f99a94f0f47567dad1b9c72 Mon Sep 17 00:00:00 2001 From: Jeremy Whiting Date: Fri, 2 Oct 2020 00:19:49 -0600 Subject: [PATCH 3/3] Fix kgenpids string vs bytes usage for python3 for calibre 5.1. In order to properly get pids etc. we need to pass bytes to MD5 and SHA1 instead of unicode strings. Also ord() is no longer needed since data is bytes value gets int and we need chr() to get characters from the mapping bytearrays. --- DeDRM_plugin/kgenpids.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/DeDRM_plugin/kgenpids.py b/DeDRM_plugin/kgenpids.py index 3e80598..b100641 100644 --- a/DeDRM_plugin/kgenpids.py +++ b/DeDRM_plugin/kgenpids.py @@ -52,11 +52,11 @@ def SHA1(message): def encode(data, map): result = '' for char in data: - value = ord(char) + value = char Q = (value ^ 0x80) // len(map) R = value % len(map) - result += map[Q] - result += map[R] + result += chr(map[Q]) + result += chr(map[R]) return result # Hash the bytes in data and then encode the digest with the characters in map @@ -238,18 +238,18 @@ def getK4Pids(rec209, token, kindleDatabase): # Get the UserName we added UserName = bytearray.fromhex((kindleDatabase[1])['UserName']).decode() # encode it - encodedUsername = encodeHash(UserName,charMap1) + encodedUsername = encodeHash(UserName.encode(),charMap1) #print "encodedUsername",encodedUsername.encode('hex') except KeyError: print("Keys not found in the database {0}.".format(kindleDatabase[0])) return pids # Get the ID string used - encodedIDString = encodeHash(IDString,charMap1) + encodedIDString = encodeHash(IDString.encode(),charMap1) #print "encodedIDString",encodedIDString.encode('hex') # concat, hash and encode to calculate the DSN - DSN = encode(SHA1(MazamaRandomNumber+encodedIDString+encodedUsername),charMap1) + DSN = encode(SHA1((MazamaRandomNumber+encodedIDString+encodedUsername).encode()),charMap1) #print "DSN",DSN.encode('hex') pass