galkahana / HummusJS

Node.js module for high performance creation, modification and parsing of PDF files and streams
http://www.pdfhummus.com
Other
1.14k stars 169 forks source link

multiple userProtection flag #358

Open bjrmatos opened 5 years ago

bjrmatos commented 5 years ago

hi! i have one question regard the userProtection flag option when using the encryption feature.

from this blog post i see the available options for the flag, which i will list here to have everything in context.

if i understand correctly, if i pass in options something like this: userProtectionFlag: 4 it will set the bit 4 to 1 and will enable the allow modification of the document, now my question if it is possible to set multiple flags/bits if i want a combinations of things that are allowed, for example let's say that i want to allow two things allow printing the document in a good res. Setting just 3 will normally print a low res form (bit 12) and allow extracting text and graphics in support of accessibility only (bit 10), if it is possible to set multiple flags then i would like to see how to pass that as option userProtectionFlag.

also if i don't pass any userProtectionFlag, should i expect everything to be disallowed? or is there also some way to set the flag in negative form?

thanks!

tomfogg commented 5 years ago

I think because these are bit flags you need to OR the flags together to get a number to use as the parameter.

eg he says that a good choice for most cases is 4 because it allows printing. the printing flag is bit 3 which means it is the value of the third bit in a binary number or 1 << 3 == 4 which is why he says use 4 for the value and not 3.

To combine bit 10 and bit 12 you OR the value of the 10th and 12th binary bits. in code you normally make constants for the different flags and then or those:

const ACCESSIBILITY_FLAG=1<<10; // 1024
const HIRES_FLAG=1<<12; // 4096

const flag = ACCESSIBILITY_FLAG | HIRES_FLAG; // 5120
xeonray-origin commented 5 years ago

https://github.com/chunyenHuang/hummusRecipe/blob/master/lib/encrypt.js

took a peek in the encrypt.js seems like you can provide multiple protection flags by inputting flags with a comma separation.

@param {string} flags from the list print, modify, copy, edit, fillform, extract, assemble, and printbest

bjrmatos commented 4 years ago

@tomfogg thanks for the explanation, i think it is confusing because the examples are not using the flag in this way, for example in the original blog post the example pass directly the flag 4 (number) without any conversion so it gets tricky to understand how it works.

@amri-ubicuo thanks for that, it is useful to know that there is a method to get the flag from string and that it also support multiple flags