crypt

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package crypt implements the PDF standard security handler (ISO 32000-1 §7.6.3 and ISO 32000-2 §7.6.4): RC4 and AES stream/string decryption with the file key derived from a supplied user or owner password.

The package is value-agnostic: callers extract the /Encrypt dictionary and the /ID into a Config.

Index

Constants

This section is empty.

Variables

View Source
var ErrPasswordRequired = errors.New("pdf: encrypted document requires a password")

ErrPasswordRequired means the supplied password matched neither the user nor the owner password.

Functions

This section is empty.

Types

type Config

type Config struct {
	Filter      string // /Filter, must be "Standard"
	V, R        int    // /V algorithm, /R revision
	Length      int    // /Length in bits (default 40)
	O, U        []byte // /O, /U password strings
	OE, UE      []byte // /OE, /UE (R6)
	P           int32  // /P permissions
	EncryptMeta bool   // /EncryptMetadata (default true)
	StmF, StrF  string // /StmF, /StrF crypt filter names (V≥4)
	CF          map[string]Filter
	ID          []byte // first element of the trailer /ID array
}

Config carries the /Encrypt dictionary fields the handler needs, already extracted from PDF objects by the caller.

type Decryptor

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

A Decryptor holds the derived file key and per-string/stream methods.

func New

func New(c Config) (*Decryptor, error)

New authenticates the empty password and derives the file key.

func NewWithPassword added in v0.3.0

func NewWithPassword(c Config, password []byte) (*Decryptor, error)

NewWithPassword authenticates password as either the user or owner password and derives the file key.

func (*Decryptor) DecryptStream

func (d *Decryptor) DecryptStream(num, gen int, r io.Reader) io.Reader

DecryptStream wraps r so the stream body decrypts as it is read.

func (*Decryptor) DecryptStreamData

func (d *Decryptor) DecryptStreamData(num, gen int, data []byte) []byte

DecryptStreamData decrypts a fully-read stream body.

func (*Decryptor) DecryptString

func (d *Decryptor) DecryptString(num, gen int, s []byte) []byte

DecryptString decrypts a literal or hex string found in object (num,gen).

type Filter

type Filter struct {
	CFM    string // /CFM: V2, AESV2, AESV3, or Identity
	Length int    // /Length in bytes (crypt-filter convention)
}

Filter is one entry of the /CF crypt-filter dictionary.

type Method

type Method int

Method is the cipher applied to a string or stream.

const (
	Identity Method = iota // no encryption (the Identity crypt filter)
	RC4
	AESV2 // AES-128-CBC
	AESV3 // AES-256-CBC
)

Cipher methods a crypt filter can select.

Jump to

Keyboard shortcuts

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