crypt

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 24, 2026 License: MPL-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package crypt implements the PDF standard security handler (ISO 32000-2 7.6): revisions 2-4 (RC4 and AESV2) and 5-6 (AESV3). It authenticates the user and owner passwords, reproducing the status bits MuPDF's fz_authenticate_password returns, and decrypts strings and streams with per-object keys.

The COS layer installs it as a cos.Decryptor: New builds the handler from the /Encrypt dictionary and tries the empty password at once, and once a password authenticates, the file encryption key decrypts every object loaded after it. All primitives come from the Go standard library. Hostile input yields errors or pass-through data, never a panic.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler is the standard security handler for one open document. It is safe to use only under the COS document's own serialization (the public API's single mutex); it is not independently concurrency-safe.

func New

func New(c *cos.Document, encDict cos.Dict) (*Handler, error)

New builds the standard security handler from encDict (the resolved /Encrypt dictionary) using c for the trailer /ID and any indirectly stored entries, then tries the empty password so documents that need none are immediately usable. It returns an error for encryption schemes it does not implement; the caller then treats the document as encrypted-but-locked.

func (*Handler) Authenticate

func (h *Handler) Authenticate(password string) (user, owner bool)

Authenticate tries password as both the user and the owner password, returning which matched. On success it records the file encryption key so subsequent decryption can proceed. The two booleans map directly onto the public API's UserAuthenticatedMask and OwnerAuthenticatedMask bits.

func (*Handler) DecryptStream

func (h *Handler) DecryptStream(num, gen int, data []byte) []byte

DecryptStream decrypts a stream's raw payload (before its /Filter chain) belonging to object (num, gen), returning it unchanged when no key is yet available or the streams are not encrypted.

func (*Handler) DecryptString

func (h *Handler) DecryptString(num, gen int, data []byte) []byte

DecryptString decrypts a string belonging to object (num, gen), returning it unchanged when no key is yet available or the strings are not encrypted.

func (*Handler) EncryptsMetadata added in v0.2.0

func (h *Handler) EncryptsMetadata() bool

EncryptsMetadata reports whether metadata streams are encrypted along with everything else, i.e. whether the encryption dictionary's /EncryptMetadata entry (default true) is set.

func (*Handler) NeedsPassword

func (h *Handler) NeedsPassword() bool

NeedsPassword reports whether a password is required to use the document: true unless the empty password (tried at open) already authenticated.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL