Sha256: 0b0078e47b70472a4e2184c579a4915363cbbd88ae9aefa09af609c045218981

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 KB

Contents

require 'openssl'

module ShadowsocksRuby
  module Cipher

    # Implementation of the Table cipher method.
    #
    # Note: this cipher method have neither IV or key, so may be
    # incompatible with protocols which needs IV or key.
    # 
    # Normally you should use {ShadowsocksRuby::Cipher#build} to get an
    # instance of this class.
    class Table
      # (see OpenSSL#initialize)
      def initialize password
        @encrypt_table, @decrypt_table = get_table(password)
      end

      # (see OpenSSL#encrypt)
      def encrypt(message)
        translate @encrypt_table, message
      end

      # (see OpenSSL#decrypt)
      def decrypt(message)
        translate @decrypt_table, message
      end

      # (see OpenSSL#iv_len)
      #
      # returns 0 for Table
      def iv_len
        0
      end

      # (see OpenSSL#key)
      #
      # returns nil for Table
      def key
        nil
      end

      private

      def get_table(key)
        table = [*0..255]
        a = ::OpenSSL::Digest::MD5.digest(key).unpack('Q<')[0]

        (1...1024).each do |i|
          table.sort! { |x, y| a % (x + i) - a % (y + i) }
        end

        decrypt_table = Array.new(256)
        table.each_with_index {|x, i| decrypt_table[x] = i}

        [table, decrypt_table]
      end

      def translate(table, buf)
        buf.bytes.map!{|x| table[x]}.pack("C*")
      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/table.rb
shadowsocks_ruby-0.1.1 lib/shadowsocks_ruby/cipher/table.rb
shadowsocks_ruby-0.1.0 lib/shadowsocks_ruby/cipher/table.rb