README

#@bobzhang/mbtpdf/core/pdfcryptprimitives

Cryptographic primitives for PDF encryption and decryption.

#Overview

This package provides cryptographic algorithms required for PDF document encryption and decryption, including symmetric ciphers (ARC4, AES), hash functions (MD5, SHA-2 family), and key derivation routines used by the PDF security handler.

#Types

#Encryption

Represents the encryption algorithm types supported by PDF:

///|
pub(all) enum Encryption {
ARC4(Int, Int) // (bits, /R revision)
AESV2 // AES-128 CBC (PDF 1.5+)
AESV3(Bool) // AES-256, Bool indicates ISO mode (revision 6) vs revision 5
}

Convenience helper:

///|
pub fn Encryption::r_and_keylength(self : Encryption) -> (Int, Int)

This returns the PDF security handler revision (/R) and the key length in bits implied by the variant, so callers don't have to repeat the same match.

#PdfCryptPrimitives

Cryptography helper context.

pub struct PdfCryptPrimitives { ... }
pub fn PdfCryptPrimitives::new() -> PdfCryptPrimitives

#Methods

#PdfCryptPrimitives::crypt

ARC4 stream cipher encryption/decryption. The same function performs both operations since ARC4 is symmetric.

pub fn PdfCryptPrimitives::crypt(
self : PdfCryptPrimitives,
key : Array[Int],
data : @pdfio.MutableBytes
) -> @pdfio.MutableBytes

#PdfCryptPrimitives::md5

MD5 message digest, returning 16 raw bytes.

pub fn PdfCryptPrimitives::md5(
self : PdfCryptPrimitives,
data : @pdfio.MutableBytes
) -> @pdfio.MutableBytes

#PdfCryptPrimitives::sha256

SHA-256 digest of input data.

pub fn PdfCryptPrimitives::sha256(
self : PdfCryptPrimitives,
input : @pdfio.Input
) -> String raise

#PdfCryptPrimitives::sha384

SHA-384 digest of input data.

pub fn PdfCryptPrimitives::sha384(
self : PdfCryptPrimitives,
input : @pdfio.Input
) -> String raise

#PdfCryptPrimitives::sha512

SHA-512 digest of input data.

pub fn PdfCryptPrimitives::sha512(
self : PdfCryptPrimitives,
input : @pdfio.Input
) -> String raise

#PdfCryptPrimitives::aes_encrypt_data

AES encryption in CBC mode. Returns IV concatenated with ciphertext.

pub fn PdfCryptPrimitives::aes_encrypt_data(
self : PdfCryptPrimitives,
nk : Int,
key : Array[Int],
data : @pdfio.MutableBytes,
firstblock? : Array[Int]
) -> @pdfio.MutableBytes

  • nk: Key length (4 for AES-128, 8 for AES-256)
  • firstblock: Optional IV (random if not provided)

#PdfCryptPrimitives::aes_decrypt_data

AES decryption in CBC mode. Expects IV as first 16 bytes of input.

pub fn PdfCryptPrimitives::aes_decrypt_data(
self : PdfCryptPrimitives,
nk : Int,
key : Array[Int],
data : @pdfio.MutableBytes,
remove_padding? : Bool
) -> @pdfio.MutableBytes

  • nk: Key length (4 for AES-128, 8 for AES-256)
  • data: IV + ciphertext
  • remove_padding: Remove PKCS7 padding (default: true)

#PdfCryptPrimitives::aes_encrypt_data_ecb / PdfCryptPrimitives::aes_decrypt_data_ecb

AES encryption/decryption in ECB mode (used for specific PDF operations).

#PdfCryptPrimitives::find_hash

Apply PDF Algorithm 3.1 to derive the per-object encryption key.

pub fn PdfCryptPrimitives::find_hash(
self : PdfCryptPrimitives,
crypt_type : Encryption,
obj : Int,
gen : Int,
key : Array[Int],
keylength : Int
) -> Array[Int]

#PdfCryptPrimitives::decrypt_stream_data

Decrypt or encrypt stream data using the appropriate algorithm based on encryption settings.

pub fn PdfCryptPrimitives::decrypt_stream_data(
self : PdfCryptPrimitives,
crypt_type : Encryption,
encrypt : Bool,
file_encryption_key : String?,
obj : Int,
gen : Int,
key : Array[Int],
keylength : Int,
r : Int,
data : @pdfio.MutableBytes
) -> @pdfio.MutableBytes raise

  • encrypt: true for encryption, false for decryption
  • r: Security handler revision

#PDF Encryption Support

This package supports PDF encryption standards:
  • 40-bit RC4 (PDF 1.1): ARC4(40, ...)
  • 128-bit RC4 (PDF 1.4): ARC4(128, ...)
  • 128-bit AES (PDF 1.5): AESV2
  • 256-bit AES (PDF 1.7 Extension 3 / PDF 2.0): AESV3

#
Encryption

pub(all) enum Encryption {
ARC4(Int, Int)
AESV2
AESV3(Bool)
}

Types of encryption.
impl Debug for Encryption

#
Encryption::r_and_keylength

fn Encryption::r_and_keylength(self : Encryption) -> (Int, Int)

Return the PDF "revision" (/R) and key length (in bits) implied by the encryption variant.

  • ARC4(bits, r) uses the explicit values from the parsed encryption dict.
  • AESV2 is always revision 4 with a 128-bit key.
  • AESV3(is_iso) is revision 5 (or 6 in ISO mode) with a 256-bit key.

This is a small convenience API to avoid scattering the same match in multiple packages.

#
PdfCryptPrimitives

pub struct PdfCryptPrimitives {
unit : Unit
}

#
PdfCryptPrimitives::aes_decrypt_data

fn PdfCryptPrimitives::aes_decrypt_data(_self : PdfCryptPrimitives, nk : Int, key : Array[Int], data : Array[Byte], remove_padding? : Bool) -> Array[Byte]

[aes_decrypt_data nk key data] decrypts AES data for the given key length, key, and data. If [remove_padding] is true, padding is removed.

#
PdfCryptPrimitives::aes_decrypt_data_ecb

fn PdfCryptPrimitives::aes_decrypt_data_ecb(_self : PdfCryptPrimitives, nk : Int, key : Array[Int], data : Array[Byte], remove_padding? : Bool) -> Array[Byte]

As [aes_decrypt_data] but in ECB mode.

#
PdfCryptPrimitives::aes_encrypt_data

fn PdfCryptPrimitives::aes_encrypt_data(_self : PdfCryptPrimitives, nk : Int, key : Array[Int], data : Array[Byte], firstblock? : Array[Int]) -> Array[Byte]

[aes_encrypt_data nk key data] encrypts with AES (CBC), returning IV+data.

#
PdfCryptPrimitives::aes_encrypt_data_ecb

fn PdfCryptPrimitives::aes_encrypt_data_ecb(_self : PdfCryptPrimitives, nk : Int, key : Array[Int], data : Array[Byte]) -> Array[Byte]

[aes_encrypt_data_ecb nk key data] encrypts data in ECB mode.

#
PdfCryptPrimitives::crypt

fn PdfCryptPrimitives::crypt(_self : PdfCryptPrimitives, key : Array[Int], data : Array[Byte]) -> Array[Byte]

ARC4 encryption/decryption given a key and some data. The same function performs encryption and decryption.

#
PdfCryptPrimitives::decrypt_stream_data

fn PdfCryptPrimitives::decrypt_stream_data(self : PdfCryptPrimitives, crypt_type : Encryption, encrypt : Bool, file_encryption_key : String?, obj : Int, gen : Int, key : Array[Int], keylength : Int, r : Int, data : Array[Byte]) -> Array[Byte] raise

Decrypt or encrypt stream data using the active crypt settings.

#
PdfCryptPrimitives::find_hash

fn PdfCryptPrimitives::find_hash(self : PdfCryptPrimitives, crypt_type : Encryption, obj : Int, gen : Int, key : Array[Int], keylength : Int) -> Array[Int]

Given an object number, generation number, input key and key length in bits, apply Algorithm 3.1 to obtain the hash to be used by the encryption function.

#
PdfCryptPrimitives::md5

fn PdfCryptPrimitives::md5(_self : PdfCryptPrimitives, data : Array[Byte]) -> Array[Byte]

MD5 digest of bytes, returning 16 raw bytes.

#
PdfCryptPrimitives::new

#
PdfCryptPrimitives::sha256

SHA-256 digest of the input stream.

#
PdfCryptPrimitives::sha384

SHA-384 digest of the input stream.

#
PdfCryptPrimitives::sha512

SHA-512 digest of the input stream.

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io