class ShadowsocksRuby::Cipher::RC4_MD5

Implementation of the RC4_MD5 cipher method.

Normally you should use {ShadowsocksRuby::Cipher#build} to get an instance of this class.

Attributes

key[R]

Public Class Methods

new(password) click to toggle source

(see OpenSSL#initialize)

# File lib/shadowsocks_ruby/cipher/rc4_md5.rb, line 15
def initialize password
  @key = ShadowsocksRuby::Cipher.bytes_to_key(password, 16, 16)
  @cipher_encrypt = ::OpenSSL::Cipher.new('rc4').encrypt
  @cipher_decrypt = ::OpenSSL::Cipher.new('rc4').decrypt
  @encrypt_iv = nil
  @decrypt_iv = nil
end

Public Instance Methods

decrypt(message, iv) click to toggle source

(see OpenSSL#decrypt)

# File lib/shadowsocks_ruby/cipher/rc4_md5.rb, line 39
def decrypt(message, iv)
  if @decrypt_iv != iv
    @decrypt_iv = iv
    key = ::OpenSSL::Digest::MD5.digest(@key + iv)
    @cipher_decrypt.key = key
  end
  @cipher_decrypt.update(message) << @cipher_decrypt.final
end
encrypt(message, iv) click to toggle source

(see OpenSSL#encrypt)

# File lib/shadowsocks_ruby/cipher/rc4_md5.rb, line 29
def encrypt(message, iv)
  if @encrypt_iv != iv
    @encrypt_iv = iv
    key = ::OpenSSL::Digest::MD5.digest(@key + iv)
    @cipher_encrypt.key = key
  end
  @cipher_encrypt.update(message) << @cipher_encrypt.final
end
iv_len() click to toggle source

(see OpenSSL#iv_len)

# File lib/shadowsocks_ruby/cipher/rc4_md5.rb, line 49
def iv_len
  16
end
random_iv() click to toggle source

(see OpenSSL#random_iv)

# File lib/shadowsocks_ruby/cipher/rc4_md5.rb, line 24
def random_iv
  Random.new.bytes(16)
end