Class PDF::Writer::ARC4
In: lib/pdf/writer/arc4.rb
Parent: Object
Transaction::Simple SimpleTable TechBook Complex Action Procset FontDescriptor FontEncoding Destination Info Catalog Encryption Contents Pages Outline Outlines Annotation Page Font ViewerPreferences Image Hash OHash QuickRef FontMetrics ARC4 StrokeStyle PolygonPoint ImageInfo StdDev lib/pdf/simpletable.rb lib/pdf/techbook.rb lib/pdf/writer.rb lib/pdf/quickref.rb Math lib/pdf/writer/fontmetrics.rb lib/pdf/writer/ohash.rb lib/pdf/writer/arc4.rb lib/pdf/writer/strokestyle.rb lib/pdf/writer/graphics.rb lib/pdf/writer/object.rb lib/pdf/writer/object/image.rb External EN Lang OffsetReader lib/pdf/writer/graphics/imageinfo.rb Graphics lib/pdf/writer/object/outlines.rb lib/pdf/writer/object/destination.rb lib/pdf/writer/object/viewerpreferences.rb lib/pdf/writer/object/fontencoding.rb lib/pdf/writer/object/page.rb lib/pdf/writer/object/contents.rb lib/pdf/writer/object/procset.rb lib/pdf/writer/object/pages.rb lib/pdf/writer/object/info.rb lib/pdf/writer/object/encryption.rb lib/pdf/writer/object/catalog.rb lib/pdf/writer/object/outline.rb lib/pdf/writer/object/fontdescriptor.rb lib/pdf/writer/object/action.rb lib/pdf/writer/object/font.rb lib/pdf/writer/object/annotation.rb Object Writer lib/pdf/charts/stddev.rb Charts PDF dot/m_33_0.png

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

Methods

encrypt   init   new   prepare  

Public Class methods

Initializes the ARC4 encryption with the specified key.

[Source]

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

Public Instance methods

ARC4 encrypt a text string

[Source]

    # File lib/pdf/writer/arc4.rb, line 48
48:   def encrypt(text)
49:     len = text.size
50:     a = b = 0
51:     c = @arc4.dup
52:     out = ""
53: 
54:     text.each_byte do |x|
55:       a = (a + 1) % 256
56:       b = (b + c[a].to_i) % 256
57:       c[a], c[b] = c[b], c[a]
58:       k = (c[(c[a].to_i + c[b].to_i) % 256]).to_i
59:       out << ("%c" % (x.to_i ^ k))
60:     end
61:     out
62:   end

Initialize the ARC4 encryption.

[Source]

    # File lib/pdf/writer/arc4.rb, line 28
28:   def init(key)
29:     @arc4 = ""
30: 
31:       # Setup the control array
32:     return if key.empty?
33: 
34:     a = []
35:     (0..255).each { |ii| a[ii] = "%c" % ii }
36: 
37:     k = (key * 256)[0..255].split(//)
38: 
39:     jj = 0
40:     @arc4.each_with_index do |el, ii|
41:       jj = ((jj + el.to_i) + k[ii].to_i) % 256
42:       a[ii], a[jj] = a[jj], a[ii]
43:     end
44:     @arc4 = a.join
45:   end

Initialize the encryption for processing a particular object.

[Source]

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

[Validate]