Class: ShadowsocksRuby::Cipher::RC4_MD5
- Inherits:
-
Object
- Object
- ShadowsocksRuby::Cipher::RC4_MD5
- 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
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Instance Method Summary collapse
-
#decrypt(message, iv) ⇒ String
Decrypt message by provided IV.
-
#encrypt(message, iv) ⇒ String
Encrypt message by provided IV.
-
#initialize(password) ⇒ RC4_MD5
constructor
A new instance of RC4_MD5.
-
#iv_len ⇒ Integer
Get the cipher object’s IV length.
-
#random_iv ⇒ String
Generate a random IV for the cipher method.
Constructor Details
#initialize(password) ⇒ RC4_MD5
Returns a new instance of RC4_MD5.
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
#key ⇒ Object (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
39 40 41 42 43 44 45 46 |
# File 'lib/shadowsocks_ruby/cipher/rc4_md5.rb', line 39 def decrypt(, iv) if @decrypt_iv != iv @decrypt_iv = iv key = ::OpenSSL::Digest::MD5.digest(@key + iv) @cipher_decrypt.key = key end @cipher_decrypt.update() << @cipher_decrypt.final end |
#encrypt(message, iv) ⇒ String
Encrypt message by provided IV
29 30 31 32 33 34 35 36 |
# File 'lib/shadowsocks_ruby/cipher/rc4_md5.rb', line 29 def encrypt(, iv) if @encrypt_iv != iv @encrypt_iv = iv key = ::OpenSSL::Digest::MD5.digest(@key + iv) @cipher_encrypt.key = key end @cipher_encrypt.update() << @cipher_encrypt.final end |
#iv_len ⇒ Integer
Get the cipher object’s IV length
49 50 51 |
# File 'lib/shadowsocks_ruby/cipher/rc4_md5.rb', line 49 def iv_len 16 end |
#random_iv ⇒ String
Generate a random IV for the cipher method
24 25 26 |
# File 'lib/shadowsocks_ruby/cipher/rc4_md5.rb', line 24 def random_iv Random.new.bytes(16) end |