Skip to content

Commit db50306

Browse files
committed
PDFBOX-5660: optimize, as suggested by Valery Bokov; closes #432
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1932892 13f79535-47bb-0310-9956-ffa450edef68
1 parent 7f70394 commit db50306

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/StandardSecurityHandler.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -522,13 +522,15 @@ private void prepareEncryptionDictRev234(String ownerPassword, String userPasswo
522522
COSArray idArray = document.getDocument().getDocumentID();
523523

524524
//check if the document has an id yet. If it does not then generate one
525+
byte[] userPasswordBytes = userPassword.getBytes(StandardCharsets.ISO_8859_1);
526+
byte[] ownerPasswordBytes = ownerPassword.getBytes(StandardCharsets.ISO_8859_1);
525527
if (idArray == null || idArray.size() < 2)
526528
{
527529
MessageDigest md = MessageDigests.getMD5();
528530
BigInteger time = BigInteger.valueOf(System.currentTimeMillis());
529531
md.update(time.toByteArray());
530-
md.update(ownerPassword.getBytes(StandardCharsets.ISO_8859_1));
531-
md.update(userPassword.getBytes(StandardCharsets.ISO_8859_1));
532+
md.update(ownerPasswordBytes);
533+
md.update(userPasswordBytes);
532534
md.update(document.getDocument().toString().getBytes(StandardCharsets.ISO_8859_1));
533535

534536
byte[] id = md.digest(this.toString().getBytes(StandardCharsets.ISO_8859_1));
@@ -541,14 +543,15 @@ private void prepareEncryptionDictRev234(String ownerPassword, String userPasswo
541543
COSString id = (COSString) idArray.getObject(0);
542544

543545
byte[] ownerBytes = computeOwnerPassword(
544-
ownerPassword.getBytes(StandardCharsets.ISO_8859_1),
545-
userPassword.getBytes(StandardCharsets.ISO_8859_1), revision, length);
546+
ownerPasswordBytes,
547+
userPasswordBytes, revision, length);
546548

549+
byte[] idBytes = id.getBytes();
547550
byte[] userBytes = computeUserPassword(
548-
userPassword.getBytes(StandardCharsets.ISO_8859_1),
549-
ownerBytes, permissionInt, id.getBytes(), revision, length, true);
551+
userPasswordBytes,
552+
ownerBytes, permissionInt, idBytes, revision, length, true);
550553

551-
setEncryptionKey(computeEncryptedKeyRev234(userPassword.getBytes(StandardCharsets.ISO_8859_1), ownerBytes, permissionInt, id.getBytes(), true, length, revision));
554+
setEncryptionKey(computeEncryptedKeyRev234(userPasswordBytes, ownerBytes, permissionInt, idBytes, true, length, revision));
552555

553556
encryptionDictionary.setOwnerKey(ownerBytes);
554557
encryptionDictionary.setUserKey(userBytes);

0 commit comments

Comments
 (0)