class ShadowsocksRuby::Cipher::Table

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.

Public Class Methods

new(password) click to toggle source

(see OpenSSL#initialize)

# File lib/shadowsocks_ruby/cipher/table.rb, line 15
def initialize password
  @encrypt_table, @decrypt_table = get_table(password)
end

Public Instance Methods

decrypt(message) click to toggle source

(see OpenSSL#decrypt)

# File lib/shadowsocks_ruby/cipher/table.rb, line 25
def decrypt(message)
  translate @decrypt_table, message
end
encrypt(message) click to toggle source

(see OpenSSL#encrypt)

# File lib/shadowsocks_ruby/cipher/table.rb, line 20
def encrypt(message)
  translate @encrypt_table, message
end
iv_len() click to toggle source

(see OpenSSL#iv_len)

returns 0 for Table

# File lib/shadowsocks_ruby/cipher/table.rb, line 32
def iv_len
  0
end
key() click to toggle source

(see OpenSSL#key)

returns nil for Table

# File lib/shadowsocks_ruby/cipher/table.rb, line 39
def key
  nil
end

Private Instance Methods

get_table(key) click to toggle source
# File lib/shadowsocks_ruby/cipher/table.rb, line 45
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
translate(table, buf) click to toggle source
# File lib/shadowsocks_ruby/cipher/table.rb, line 59
def translate(table, buf)
  buf.bytes.map!{|x| table[x]}.pack("C*")
end