Class: PDF::Writer::ARC4

### Home Classes Methods

### In Files

- lib/pdf/writer/arc4.rb

### Parent

Object

### Methods

- ::new
- #encrypt
- #init
- #prepare

### Files

- ChangeLog

### Class/Module Index

Quicksearch

- PDF
- PDF::Charts
- PDF::Charts::StdDev
- PDF::Charts::StdDev::Label
- PDF::Charts::StdDev::Marker
- PDF::Charts::StdDev::Scale
- PDF::Math
- PDF::QuickRef
- PDF::SimpleTable
- PDF::SimpleTable::Column
- PDF::SimpleTable::Column::Heading
- PDF::TechBook
- PDF::TechBook::TagTocDots
- PDF::TechBook::TagXref
- PDF::Writer
- PDF::Writer::ARC4
- PDF::Writer::Complex
- PDF::Writer::External
- PDF::Writer::FontMetrics
- PDF::Writer::Graphics
- PDF::Writer::Graphics::ImageInfo
- PDF::Writer::Graphics::ImageInfo::Formats
- PDF::Writer::Graphics::ImageInfo::Type
- PDF::Writer::Lang
- PDF::Writer::Lang::EN
- PDF::Writer::OHash
- PDF::Writer::Object
- PDF::Writer::Object::Action
- PDF::Writer::Object::Annotation
- PDF::Writer::Object::Catalog
- PDF::Writer::Object::Contents
- PDF::Writer::Object::Destination
- PDF::Writer::Object::Encryption
- PDF::Writer::Object::Font
- PDF::Writer::Object::FontDescriptor
- PDF::Writer::Object::FontEncoding
- PDF::Writer::Object::Info
- PDF::Writer::Object::Outline
- PDF::Writer::Object::Outlines
- PDF::Writer::Object::Pages
- PDF::Writer::Object::Procset
- PDF::Writer::Object::ViewerPreferences
- PDF::Writer::OffsetReader
- PDF::Writer::PolygonPoint
- PDF::Writer::State
- PDF::Writer::StateStack
- PDF::Writer::StrokeStyle

No matching classes.

# PDF::Writer::ARC4

ARC4 methods A series of function to implement ARC4 encoding in Ruby

### Public Class Methods

new(key) click to toggle source

Initializes the ARC4 encryption with the specified key.

    # File lib/pdf/writer/arc4.rb, line 17
    def initialize(key)
      @key = key
    end

### Public Instance Methods

encrypt(text) click to toggle source

ARC4 encrypt a text string

    # File lib/pdf/writer/arc4.rb, line 48
    def encrypt(text)
      len = text.size
      a = b = 0
      c = @arc4.dup
      out = ""

      text.each_byte do |x|
        a = (a + 1) % 256
        b = (b + c[a].to_i) % 256
        c[a], c[b] = c[b], c[a]
        k = (c[(c[a].to_i + c[b].to_i) % 256]).to_i
        out << ("%c" % (x.to_i ^ k))
      end
      out
    end

init(key) click to toggle source

Initialize the ARC4 encryption.

    # File lib/pdf/writer/arc4.rb, line 28
    def init(key)
      @arc4 = ""

        # Setup the control array
      return if key.empty?

      a = []
      (0..255).each { |ii| a[ii] = "%c" % ii }

      k = (key * 256)[0..255].split(//)

      jj = 0
      @arc4.each_with_index do |el, ii|
        jj = ((jj + el.to_i) + k[ii].to_i) % 256
        a[ii], a[jj] = a[jj], a[ii]
      end
      @arc4 = a.join
    end

prepare(object) click to toggle source

Initialize the encryption for processing a particular object.

    # File lib/pdf/writer/arc4.rb, line 22
    def prepare(object)
      hex = ("%06x" % [object.oid]).scan(/../).reverse
      init(Digest::MD5.digest("#{@key}#{hex.pack('H10')}")[0...10])
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.