Class: Pdfrb::Document::Encryption
- Inherits:
-
Object
- Object
- Pdfrb::Document::Encryption
- Defined in:
- lib/pdfrb/document/encryption.rb
Overview
Encryption facade. Exposes a clean API for encrypting a document (builds the /Encrypt dict, wires the security handler into the writer config) and for checking encryption status.
Usage:
doc.encryption.encrypt!(
user_password: "reader",
owner_password: "owner",
bits: 128,
permissions: %i[print copy]
)
doc.write("encrypted.pdf")
Constant Summary collapse
- PERMISSION_FLAGS =
{ print: 1 << 2, modify: 1 << 3, copy: 1 << 4, annotate: 1 << 5, fill: 1 << 8, extract: 1 << 9, assemble: 1 << 10, print_hq: 1 << 11, }.freeze
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
Class Method Summary collapse
-
.permission_bits(granted) ⇒ Object
Compute the /P integer: all bits set minus the denied ones.
Instance Method Summary collapse
-
#decrypt! ⇒ Object
Remove /Encrypt from the trailer (decrypt use case).
-
#encrypt!(user_password:, owner_password: nil, bits: 128, permissions: PERMISSION_FLAGS.keys) ⇒ Pdfrb::Model::Cos::Dictionary
Encrypt this document with a user + owner password.
-
#encrypted? ⇒ Boolean
Whether the document has an /Encrypt dict in the trailer.
-
#initialize(document) ⇒ Encryption
constructor
A new instance of Encryption.
Constructor Details
#initialize(document) ⇒ Encryption
Returns a new instance of Encryption.
34 35 36 |
# File 'lib/pdfrb/document/encryption.rb', line 34 def initialize(document) @document = document end |
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
32 33 34 |
# File 'lib/pdfrb/document/encryption.rb', line 32 def document @document end |
Class Method Details
.permission_bits(granted) ⇒ Object
Compute the /P integer: all bits set minus the denied ones.
85 86 87 88 89 90 91 |
# File 'lib/pdfrb/document/encryption.rb', line 85 def self.(granted) base = -1 PERMISSION_FLAGS.each do |name, bit| base &= ~bit unless granted.include?(name) end base end |
Instance Method Details
#decrypt! ⇒ Object
Remove /Encrypt from the trailer (decrypt use case). Returns self for chaining; use encrypted? to check the result.
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/pdfrb/document/encryption.rb', line 70 def decrypt! trailer = document.trailer if trailer && trailer[:Encrypt] if trailer.is_a?(Pdfrb::Model::Cos::Dictionary) trailer.value.delete(:Encrypt) else trailer.delete(:Encrypt) end document.config["encryption.handler"] = nil document.config["encryption.password"] = nil end self end |
#encrypt!(user_password:, owner_password: nil, bits: 128, permissions: PERMISSION_FLAGS.keys) ⇒ Pdfrb::Model::Cos::Dictionary
Encrypt this document with a user + owner password.
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/pdfrb/document/encryption.rb', line 48 def encrypt!(user_password:, owner_password: nil, bits: 128, permissions: PERMISSION_FLAGS.keys) case bits when 256 then encrypt_v5(user_password, owner_password || user_password, ) when 128 then encrypt_rc4(user_password, owner_password || user_password, , v: 4, r: 4, length: 128) when 40 then encrypt_rc4(user_password, owner_password || user_password, , v: 2, r: 3, length: 40) else raise Pdfrb::EncryptionError, "unsupported key length: #{bits} (use 40, 128, or 256)" end end |
#encrypted? ⇒ Boolean
Whether the document has an /Encrypt dict in the trailer.
63 64 65 66 |
# File 'lib/pdfrb/document/encryption.rb', line 63 def encrypted? trailer = document.trailer !trailer.nil? && !trailer[:Encrypt].nil? end |