class 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 17 def initialize(key) 18 @key = key 19 end
Public Instance Methods
encrypt(text)
click to toggle source
ARC4
encrypt a text string
# File lib/pdf/writer/arc4.rb 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
init(key)
click to toggle source
Initialize the ARC4
encryption.
# File lib/pdf/writer/arc4.rb 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
prepare(object)
click to toggle source
Initialize the encryption for processing a particular object.
# File lib/pdf/writer/arc4.rb 22 def prepare(object) 23 hex = ("%06x" % [object.oid]).scan(/../).reverse 24 init(Digest::MD5.digest("#{@key}#{hex.pack('H10')}")[0...10]) 25 end