Class: Origami::Encryption::RC4

Inherits:
Object
  • Object
show all
Defined in:
lib/origami/encryption.rb

Overview

Class wrapper for the RC4 algorithm.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ RC4

Creates and initialises a new RC4 generator using given key



543
544
545
# File 'lib/origami/encryption.rb', line 543

def initialize(key)
    @key = key
end

Class Method Details

.decrypt(key, data) ⇒ Object

Decrypts data using the given key



536
537
538
# File 'lib/origami/encryption.rb', line 536

def RC4.decrypt(key, data)
    RC4.new(key).decrypt(data)
end

.encrypt(key, data) ⇒ Object

Encrypts data using the given key



529
530
531
# File 'lib/origami/encryption.rb', line 529

def RC4.encrypt(key, data)
    RC4.new(key).encrypt(data)
end

Instance Method Details

#cipher(data) ⇒ Object Also known as: encrypt, decrypt

Encrypt/decrypt data with the RC4 encryption algorithm



550
551
552
553
554
555
556
557
558
# File 'lib/origami/encryption.rb', line 550

def cipher(data)
    return '' if data.empty?

    rc4 = OpenSSL::Cipher::RC4.new.encrypt
    rc4.key_len = @key.length
    rc4.key = @key

    rc4.update(data) + rc4.final
end