Class: HexaPDF::Encryption::RubyARC4
- Inherits:
-
Object
- Object
- HexaPDF::Encryption::RubyARC4
- Includes:
- ARC4
- Defined in:
- lib/hexapdf/encryption/ruby_arc4.rb
Overview
Pure Ruby implementation of the general encryption algorithm ARC4.
Since this algorithm is implemented in pure Ruby, it is not very fast. Therefore the FastARC4 class based on OpenSSL should be used when possible.
For reference: This implementation is about 250 times slower than the FastARC4 version.
See: PDF2.0 s7.6.3
Instance Method Summary collapse
-
#initialize(key) ⇒ RubyARC4
constructor
Creates a new ARC4 object using the given encryption key.
-
#process(data) ⇒ Object
(also: #decrypt, #encrypt)
Processes the given data.
Methods included from ARC4
Constructor Details
#initialize(key) ⇒ RubyARC4
Creates a new ARC4 object using the given encryption key.
55 56 57 58 |
# File 'lib/hexapdf/encryption/ruby_arc4.rb', line 55 def initialize(key) initialize_state(key) @i = @j = 0 end |
Instance Method Details
#process(data) ⇒ Object Also known as: decrypt, encrypt
Processes the given data.
Since this is a symmetric algorithm, the same method can be used for encryption and decryption.
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/hexapdf/encryption/ruby_arc4.rb', line 64 def process(data) result = data.dup.force_encoding(Encoding::BINARY) di = 0 while di < result.length @i = (@i + 1) % 256 @j = (@j + @state[@i]) % 256 @state[@i], @state[@j] = @state[@j], @state[@i] result.setbyte(di, result.getbyte(di) ^ @state[(@state[@i] + @state[@j]) % 256]) di += 1 end result end |