(encryption: EncryptionDict)
| 186 | * @returns A PdfDict that can be written to the PDF |
| 187 | */ |
| 188 | export function reconstructEncryptDict(encryption: EncryptionDict): PdfDict { |
| 189 | const dict = PdfDict.of({ |
| 190 | Filter: PdfName.of(encryption.filter), |
| 191 | V: PdfNumber.of(encryption.version), |
| 192 | R: PdfNumber.of(encryption.revision), |
| 193 | Length: PdfNumber.of(encryption.keyLengthBits), |
| 194 | O: PdfString.fromBytes(encryption.ownerHash), |
| 195 | U: PdfString.fromBytes(encryption.userHash), |
| 196 | P: PdfNumber.of(encryption.permissionsRaw), |
| 197 | }); |
| 198 | |
| 199 | // R5-R6 specific entries |
| 200 | if (encryption.ownerEncryptionKey) { |
| 201 | dict.set("OE", PdfString.fromBytes(encryption.ownerEncryptionKey)); |
| 202 | } |
| 203 | |
| 204 | if (encryption.userEncryptionKey) { |
| 205 | dict.set("UE", PdfString.fromBytes(encryption.userEncryptionKey)); |
| 206 | } |
| 207 | |
| 208 | if (encryption.permsValue) { |
| 209 | dict.set("Perms", PdfString.fromBytes(encryption.permsValue)); |
| 210 | } |
| 211 | |
| 212 | // Crypt filters (V4+) |
| 213 | if (encryption.cryptFilters && encryption.cryptFilters.size > 0) { |
| 214 | const cfDict = new PdfDict(); |
| 215 | |
| 216 | for (const [name, filter] of encryption.cryptFilters) { |
| 217 | const filterDict = PdfDict.of({ |
| 218 | CFM: PdfName.of(filter.cfm), |
| 219 | }); |
| 220 | |
| 221 | if (filter.authEvent) { |
| 222 | filterDict.set("AuthEvent", PdfName.of(filter.authEvent)); |
| 223 | } |
| 224 | |
| 225 | if (filter.length !== undefined) { |
| 226 | filterDict.set("Length", PdfNumber.of(filter.length)); |
| 227 | } |
| 228 | |
| 229 | cfDict.set(name, filterDict); |
| 230 | } |
| 231 | |
| 232 | dict.set("CF", cfDict); |
| 233 | } |
| 234 | |
| 235 | // Stream and string filters |
| 236 | if (encryption.streamFilter) { |
| 237 | dict.set("StmF", PdfName.of(encryption.streamFilter)); |
| 238 | } |
| 239 | |
| 240 | if (encryption.stringFilter) { |
| 241 | dict.set("StrF", PdfName.of(encryption.stringFilter)); |
| 242 | } |
| 243 | |
| 244 | // EncryptMetadata (only if false, true is default) |
| 245 | if (!encryption.encryptMetadata) { |
no test coverage detected