Class: ShadowsocksRuby::Cipher::RC4_MD5

Inherits:
Object
  • Object
show all
Defined in:
lib/shadowsocks_ruby/cipher/rc4_md5.rb

Overview

Implementation of the RC4_MD5 cipher method.

Normally you should use #build to get an instance of this class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(password) ⇒ RC4_MD5

Returns a new instance of RC4_MD5.

Parameters:

  • password (String)

    Password


15
16
17
18
19
20
21
# 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

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key


12
13
14
# File 'lib/shadowsocks_ruby/cipher/rc4_md5.rb', line 12

def key
  @key
end

Instance Method Details

#decrypt(message, iv) ⇒ String

Decrypt message by provided IV

Parameters:

  • message (String)
  • iv (String)

Returns:

  • (String)

    Decrypted Message


39
40
41
42
43
44
45
46
# 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) ⇒ String

Encrypt message by provided IV

Parameters:

  • message (String)
  • iv (String)

Returns:

  • (String)

    Encrypted Message


29
30
31
32
33
34
35
36
# 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_lenInteger

Get the cipher object's IV length

Returns:

  • (Integer)

49
50
51
# File 'lib/shadowsocks_ruby/cipher/rc4_md5.rb', line 49

def iv_len
  16
end

#random_ivString

Generate a random IV for the cipher method

Returns:

  • (String)

    random IV of the length of the cipher method


24
25
26
# File 'lib/shadowsocks_ruby/cipher/rc4_md5.rb', line 24

def random_iv
  Random.new.bytes(16)
end