def encodePDF(key, objectNumber, generationNumber, string, revision=2): "Encodes a string or stream" #print 'encodePDF (%s, %d, %d, %s)' % (hexText(key), objectNumber, generationNumber, string) # extend 3 bytes of the object Number, low byte first newkey = key n = objectNumber for i in range(3): newkey += int2Byte(n & 0xff) n = n >> 8 # extend 2 bytes of the generationNumber n = generationNumber for i in range(2): newkey += int2Byte(n & 0xff) n = n >> 8 md5output = md5(newkey).digest() if revision == 2: key = md5output[:10] elif revision == 3: key = md5output #all 16 bytes from reportlab.lib.arciv import ArcIV encrypted = ArcIV(key).encode(string) #print 'encrypted=', hexText(encrypted) if DEBUG: print('encodePDF(%s,%s,%s,%s,%s)==>%s' % tuple([ hexText(str(x)) for x in (key, objectNumber, generationNumber, string, revision, encrypted) ])) return encrypted
def encodePDF(key, objectNumber, generationNumber, string, revision=None): "Encodes a string or stream" revision = checkRevision(revision) #print 'encodePDF (%s, %d, %d, %s)' % (hexText(key), objectNumber, generationNumber, string) # extend 3 bytes of the object Number, low byte first if revision in (2, 3): newkey = key n = objectNumber for i in range(3): newkey += int2Byte(n & 0xff) n = n >> 8 # extend 2 bytes of the generationNumber n = generationNumber for i in range(2): newkey += int2Byte(n & 0xff) n = n >> 8 md5output = md5(newkey).digest() if revision == 2: key = md5output[:10] elif revision == 3: key = md5output #all 16 bytes from reportlab.lib.arciv import ArcIV encrypted = ArcIV(key).encode(string) #print 'encrypted=', hexText(encrypted) elif revision == 5: iv = os_urandom(16) encrypter = pyaes.Encrypter(pyaes.AESModeOfOperationCBC(key, iv=iv)) # pkcs7 style padding so that the size of the encrypted block is multiple of 16 string_len = len(string) padding = "" padding_len = (16 - (string_len % 16)) if string_len > 16 else (16 - string_len) if padding_len > 0: padding = chr(padding_len) * padding_len if isinstance(string, str): string = (string + padding).encode("utf-8") else: string += asBytes(padding) encrypted = iv + encrypter.feed(string) encrypted += encrypter.feed() if DEBUG: print('encodePDF(%s,%s,%s,%s,%s)==>%s' % tuple([ hexText(str(x)) for x in (key, objectNumber, generationNumber, string, revision, encrypted) ])) return encrypted
def makeRow(self, row): """Works out the character values for this Big5 row. Rows start at 0xA1""" cells = [] if self.encodingName.find('B5') > -1: # big 5, different row size for y in [4, 5, 6, 7, 10, 11, 12, 13, 14, 15]: for x in range(16): col = y * 16 + x ch = int2Byte(row) + int2Byte(col) cells.append(ch) else: cells.append([None] * 160) return cells
def makeRow(self, row): """Works out the character values for this Big5 row. Rows start at 0xA1""" cells = [] if self.encodingName.find('B5') > -1: # big 5, different row size for y in [4,5,6,7,10,11,12,13,14,15]: for x in range(16): col = y*16+x ch = int2Byte(row) + int2Byte(col) cells.append(ch) else: cells.append([None] * 160) return cells
def makeRow(self, row): """Works out the character values for this kuten row""" cells = [] if self.encodingName.find('EUC') > -1: # it is an EUC family encoding. for col in range(1, 95): ch = int2Byte(row + 160) + int2Byte(col+160) cells.append(ch) ## elif self.encodingName.find('GB') > -1: ## # it is an EUC family encoding. ## for col in range(1, 95): ## ch = int2Byte(row + 160) + int2Byte(col+160) else: cells.append([None] * 94) return cells
def encryptionkey(password, OwnerKey, Permissions, FileId1, revision=2): # FileId1 is first string of the fileid array # add padding string #AR force same as iText example #Permissions = -1836 #int(Permissions - 2**31) password = asBytes(password) + PadString # truncate to 32 bytes password = password[:32] # translate permissions to string, low order byte first p = Permissions # + 2**32L permissionsString = b"" for i in range(4): byte = (p & 0xff) # seems to match what iText does p = p >> 8 permissionsString += int2Byte(byte % 256) hash = md5(asBytes(password)) hash.update(asBytes(OwnerKey)) hash.update(asBytes(permissionsString)) hash.update(asBytes(FileId1)) md5output = hash.digest() if revision == 2: key = md5output[:5] elif revision == 3: #revision 3 algorithm - loop 50 times for x in range(50): md5output = md5(md5output).digest() key = md5output[:16] if DEBUG: print('encryptionkey(%s,%s,%s,%s,%s)==>%s' % tuple([ hexText(str(x)) for x in (password, OwnerKey, Permissions, FileId1, revision, key) ])) return key
def encryptionkey(password, OwnerKey, Permissions, FileId1, revision=2): # FileId1 is first string of the fileid array # add padding string #AR force same as iText example #Permissions = -1836 #int(Permissions - 2**31) password = asBytes(password) + PadString # truncate to 32 bytes password = password[:32] # translate permissions to string, low order byte first p = Permissions# + 2**32L permissionsString = b"" for i in range(4): byte = (p & 0xff) # seems to match what iText does p = p>>8 permissionsString += int2Byte(byte % 256) hash = md5(asBytes(password)) hash.update(asBytes(OwnerKey)) hash.update(asBytes(permissionsString)) hash.update(asBytes(FileId1)) md5output = hash.digest() if revision==2: key = md5output[:5] elif revision==3: #revision 3 algorithm - loop 50 times for x in range(50): md5output = md5(md5output).digest() key = md5output[:16] if DEBUG: print('encryptionkey(%s,%s,%s,%s,%s)==>%s' % tuple([hexText(str(x)) for x in (password, OwnerKey, Permissions, FileId1, revision, key)])) return key
def testSplitString(self): "Tests TTFont.splitString" doc = PDFDocument() font = TTFont("Vera", "Vera.ttf") text = b"".join(utf8(i) for i in range(511)) allchars = b"".join(int2Byte(i) for i in range(256)) nospace = allchars[:32] + allchars[33:] chunks = [(0, allchars), (1, nospace)] self.assertEquals(font.splitString(text, doc), chunks) # Do it twice self.assertEquals(font.splitString(text, doc), chunks) text = b"".join(utf8(i) for i in range(510, -1, -1)) allchars = b"".join(int2Byte(i) for i in range(255, -1, -1)) nospace = allchars[:223] + allchars[224:] chunks = [(1, nospace), (0, allchars)] self.assertEquals(font.splitString(text, doc), chunks)
def unHexText(hexText): equalityCheck(hexText[0], '<', 'bad hex text') equalityCheck(hexText[-1], '>', 'bad hex text') hexText = hexText[1:-1] out = b'' for i in range(int(len(hexText) / 2.0)): slice = hexText[i * 2:i * 2 + 2] char = int2Byte(eval('0x' + slice)) out = out + char return out
def unHexText(hexText): equalityCheck(hexText[0], '<', 'bad hex text') equalityCheck(hexText[-1], '>', 'bad hex text') hexText = hexText[1:-1] out = b'' for i in range(int(len(hexText)/2.0)): slice = hexText[i*2: i*2+2] char = int2Byte(eval('0x'+slice)) out = out + char return out
def draw(self): dx = self.width / 16.0 dy = self.height / 16.0 g = Group() g.add(Rect(self.x, self.y, self.width, self.height, fillColor=None, strokeColor=colors.black)) for x in range(16): for y in range(16): charValue = y * 16 + x if charValue > 32: s = String(self.x + x * dx, self.y + (self.height - y*dy), int2Byte(charValue)) g.add(s) return g
def encodePDF(key, objectNumber, generationNumber, string, revision=2): "Encodes a string or stream" #print 'encodePDF (%s, %d, %d, %s)' % (hexText(key), objectNumber, generationNumber, string) # extend 3 bytes of the object Number, low byte first newkey = key n = objectNumber for i in range(3): newkey += int2Byte(n & 0xff) n = n>>8 # extend 2 bytes of the generationNumber n = generationNumber for i in range(2): newkey += int2Byte(n & 0xff) n = n>>8 md5output = md5(newkey).digest() if revision == 2: key = md5output[:10] elif revision == 3: key = md5output #all 16 bytes from reportlab.lib.arciv import ArcIV encrypted = ArcIV(key).encode(string) #print 'encrypted=', hexText(encrypted) if DEBUG: print('encodePDF(%s,%s,%s,%s,%s)==>%s' % tuple([hexText(str(x)) for x in (key, objectNumber, generationNumber, string, revision,encrypted)])) return encrypted
def testSplitString(self): "Tests TTFont.splitString" doc = PDFDocument() font = TTFont("Vera", "Vera.ttf") text = b"".join(utf8(i) for i in range(511)) allchars = b"".join(int2Byte(i) for i in range(256)) chunks = [(0, allchars[:0xa0] + b' ' + allchars[0xa0:])] + [ (1, allchars[1:32] + allchars[33:]) if rl_config.reserveTTFNotdef else (1, allchars[:32] + allchars[33:-1]) ] self.assertEqual(font.splitString(text, doc), chunks) # Do it twice self.assertEqual(font.splitString(text, doc), chunks) text = b"".join(utf8(i) for i in range(510, -1, -1)) revver = lambda b: map(int2Byte, reversed(b)) chunks = [(i[0], b"".join(revver(i[1]))) for i in reversed(chunks)] self.assertEqual(font.splitString(text, doc), chunks)