Sha256: ddaf5bde699369340d026c8302c5e8e63402916287492fe860a21a27418766c1

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 KB

Contents

require 'openssl'

module ShadowsocksRuby
  module Cipher

    # Implementation of the RC4_MD5 cipher method.
    #
    # Normally you should use {ShadowsocksRuby::Cipher#build} to get an
    # instance of this class.

    class RC4_MD5
      attr_reader :key

      # (see OpenSSL#initialize)
      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

      # (see OpenSSL#random_iv)
      def random_iv
        Random.new.bytes(16)
      end

      # (see OpenSSL#encrypt)
      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

      # (see OpenSSL#decrypt)
      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

      # (see OpenSSL#iv_len)
      def iv_len
        16
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
shadowsocks_ruby-0.1.2 lib/shadowsocks_ruby/cipher/rc4_md5.rb
shadowsocks_ruby-0.1.1 lib/shadowsocks_ruby/cipher/rc4_md5.rb
shadowsocks_ruby-0.1.0 lib/shadowsocks_ruby/cipher/rc4_md5.rb