forked from vanielf/pdf-lib
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Based on PhakornKiong/pdf-lib#dev/DocEncrypt
- Loading branch information
1 parent
61c640a
commit ac7c7cd
Showing
15 changed files
with
1,033 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
import PDFContext from '../PDFContext'; | ||
import PDFObject from '../objects/PDFObject'; | ||
import PDFDict from '../objects/PDFDict'; | ||
import PDFName from '../objects/PDFName'; | ||
import PDFNumber from '../objects/PDFNumber'; | ||
import PDFHexString from '../objects/PDFHexString'; | ||
import PDFString from '../objects/PDFString'; | ||
|
||
export default class Encryption { | ||
readonly dict: PDFDict; | ||
|
||
static fromDict = (dict: PDFDict): Encryption => | ||
new Encryption(dict); | ||
|
||
protected constructor(dict: PDFDict) { | ||
this.dict = dict; | ||
} | ||
|
||
V(): PDFNumber { | ||
return this.dict.lookup(PDFName.of('V'), PDFNumber); | ||
} | ||
|
||
R(): PDFNumber { | ||
return this.dict.lookup(PDFName.of('R'), PDFNumber); | ||
} | ||
|
||
O(): PDFHexString { | ||
return this.dict.lookup(PDFName.of('O'), PDFHexString); | ||
} | ||
|
||
U(): PDFHexString { | ||
return this.dict.lookup(PDFName.of('U'), PDFHexString); | ||
} | ||
|
||
P(): PDFNumber { | ||
return this.dict.lookup(PDFName.of('P'), PDFNumber); | ||
} | ||
|
||
Filter(): PDFString { | ||
return this.dict.lookup(PDFName.of('Filter'), PDFString); | ||
} | ||
|
||
Length(): PDFNumber | undefined { | ||
return this.dict.lookupMaybe(PDFName.of('Length'), PDFNumber); | ||
} | ||
|
||
// Only when V === 4 | ||
CF(): CryptFilter | undefined { | ||
if (this.dict.has(PDFName.of('CF'))) { | ||
return CryptFilter.fromDict(this.dict.lookup(PDFName.of('CF'), PDFDict)); | ||
} else { | ||
return undefined; | ||
} | ||
} | ||
|
||
StmF(): PDFString | undefined { | ||
return this.dict.lookupMaybe(PDFName.of('StmF'), PDFString); | ||
} | ||
|
||
StrF(): PDFString | undefined { | ||
return this.dict.lookupMaybe(PDFName.of('StrF'), PDFString); | ||
} | ||
|
||
OE(): PDFHexString { | ||
return this.dict.lookup(PDFName.of('U'), PDFHexString); | ||
} | ||
|
||
UE(): PDFHexString { | ||
return this.dict.lookup(PDFName.of('UE'), PDFHexString); | ||
} | ||
|
||
Perms(): PDFHexString { | ||
return this.dict.lookup(PDFName.of('Perms'), PDFHexString); | ||
} | ||
|
||
getAlgorithm() { | ||
return this.V().asNumber(); | ||
} | ||
|
||
setAlgorithm(algorithm: number) { | ||
this.dict.set(PDFName.of('V'), PDFNumber.of(algorithm)); | ||
} | ||
|
||
getRevision() { | ||
return this.V().asNumber(); | ||
} | ||
|
||
setRevision(revision: number) { | ||
this.dict.set(PDFName.of('R'), PDFNumber.of(revision)); | ||
} | ||
|
||
setOwnerPassword(byteArray: Uint8Array) { | ||
this.dict.set(PDFName.of('O'), PDFHexString.fromBytes(byteArray)); | ||
} | ||
|
||
setUserPassword(byteArray: Uint8Array) { | ||
this.dict.set(PDFName.of('U'), PDFHexString.fromBytes(byteArray)); | ||
} | ||
|
||
setPermissionFlags(flags: number) { | ||
this.dict.set(PDFName.of('P'), PDFNumber.of(flags)); | ||
} | ||
|
||
getFilter(): string { | ||
return this.Filter().asString(); | ||
} | ||
|
||
setFilter(filter: string) { | ||
this.dict.set(PDFName.of('Filter'), PDFString.of(filter)); | ||
} | ||
|
||
getLength(): number | undefined { | ||
const length = this.Length(); | ||
if (!length) return undefined; | ||
return length.asNumber(); | ||
} | ||
|
||
setLength(length: number) { | ||
this.dict.set(PDFName.of('Length'), PDFNumber.of(length)); | ||
} | ||
|
||
setCryptFilter(cryptFilter: CryptFilter) { | ||
this.dict.set(PDFName.of('CF'), cryptFilter.dict); | ||
} | ||
|
||
setStreamFilter(streamFilter: string) { | ||
this.dict.set(PDFName.of('StmF'), PDFString.of(streamFilter)); | ||
} | ||
|
||
setStringFilter(stringFilter: string) { | ||
this.dict.set(PDFName.of('StrF'), PDFString.of(stringFilter)); | ||
} | ||
|
||
setOwnerEncryptionKey(byteArray: Uint8Array) { | ||
this.dict.set(PDFName.of('OE'), PDFHexString.fromBytes(byteArray)); | ||
} | ||
|
||
setUserEncryptionKey(byteArray: Uint8Array) { | ||
this.dict.set(PDFName.of('UE'), PDFHexString.fromBytes(byteArray)); | ||
} | ||
|
||
setPermissions(byteArray: Uint8Array) { | ||
this.dict.set(PDFName.of('Perms'), PDFHexString.fromBytes(byteArray)); | ||
} | ||
} | ||
|
||
type CF = { | ||
StdCF: StdCF; | ||
}; | ||
|
||
export class CryptFilter { | ||
readonly dict: PDFDict; | ||
|
||
static fromDict = (dict: PDFDict): CryptFilter => | ||
new CryptFilter(dict); | ||
|
||
static fromObjectWithContext = (object: CF, context: PDFContext): CryptFilter => | ||
new CryptFilter( | ||
PDFDict.fromMapWithContext(new Map<PDFName, PDFObject>([ | ||
[PDFName.of('StdCF'), StandardCryptFilter.fromObjectWithContext(object.StdCF, context).dict], | ||
]), context), | ||
); | ||
|
||
protected constructor(dict: PDFDict) { | ||
this.dict = dict; | ||
} | ||
|
||
StdCF(): StandardCryptFilter { | ||
return StandardCryptFilter.fromDict(this.dict.lookup(PDFName.of('StdCF'), PDFDict)); | ||
} | ||
} | ||
|
||
type StdCF = { | ||
AuthEvent: 'DocOpen'; | ||
CFM: 'AESV2' | 'AESV3'; | ||
Length: number; | ||
}; | ||
|
||
export class StandardCryptFilter { | ||
readonly dict: PDFDict; | ||
|
||
static fromDict = (dict: PDFDict): StandardCryptFilter => | ||
new StandardCryptFilter(dict); | ||
|
||
static fromObjectWithContext = (object: StdCF, context: PDFContext): StandardCryptFilter => | ||
new StandardCryptFilter( | ||
PDFDict.fromMapWithContext(new Map<PDFName, PDFObject>([ | ||
[PDFName.of('AuthEvent'), PDFString.of(object.AuthEvent)], | ||
[PDFName.of('CFM'), PDFString.of(object.CFM)], | ||
[PDFName.of('Length'), PDFNumber.of(object.Length)], | ||
]), context) | ||
); | ||
|
||
protected constructor(dict: PDFDict) { | ||
this.dict = dict; | ||
} | ||
|
||
AuthEvent(): PDFString { | ||
return this.dict.lookup(PDFName.of('AuthEvent'), PDFString); | ||
} | ||
|
||
CFM(): PDFString { | ||
return this.dict.lookup(PDFName.of('CFM'), PDFString); | ||
} | ||
|
||
Length(): PDFNumber { | ||
return this.dict.lookup(PDFName.of('Length'), PDFNumber); | ||
} | ||
} |
Oops, something went wrong.