Class: Shadowsocks::Crypto

Inherits:
Object
  • Object
show all
Includes:
Table
Defined in:
lib/shadowsocks/crypto.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Table

#get_table, #merge, #merge_sort, #translate

Methods included from Ext

binary_path

Constructor Details

#initialize(options = {}) ⇒ Crypto



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/shadowsocks/crypto.rb', line 31

def initialize(options = {})
  @password = options[:password]
  @method   = options[:method].downcase
  @iv_sent  = false
  if method == 'table'
    @encrypt_table = options[:encrypt_table]
    @decrypt_table = options[:decrypt_table]
  else
    if method_supported.nil?
      raise "Encrypt method not support"
    end
  end

  if method != 'table'
    @cipher = get_cipher(1, SecureRandom.hex(32))
  end
end

Instance Attribute Details

#bytes_to_key_resultsObject

Returns the value of attribute bytes_to_key_results.



9
10
11
# File 'lib/shadowsocks/crypto.rb', line 9

def bytes_to_key_results
  @bytes_to_key_results
end

#cipherObject

Returns the value of attribute cipher.



9
10
11
# File 'lib/shadowsocks/crypto.rb', line 9

def cipher
  @cipher
end

#decrypt_tableObject

Returns the value of attribute decrypt_table.



9
10
11
# File 'lib/shadowsocks/crypto.rb', line 9

def decrypt_table
  @decrypt_table
end

#encrypt_tableObject

Returns the value of attribute encrypt_table.



9
10
11
# File 'lib/shadowsocks/crypto.rb', line 9

def encrypt_table
  @encrypt_table
end

#iv_sentObject

Returns the value of attribute iv_sent.



9
10
11
# File 'lib/shadowsocks/crypto.rb', line 9

def iv_sent
  @iv_sent
end

#methodObject

Returns the value of attribute method.



9
10
11
# File 'lib/shadowsocks/crypto.rb', line 9

def method
  @method
end

#passwordObject

Returns the value of attribute password.



9
10
11
# File 'lib/shadowsocks/crypto.rb', line 9

def password
  @password
end

Instance Method Details

#decrypt(buf) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/shadowsocks/crypto.rb', line 63

def decrypt buf
  return buf if buf.length == 0
  if method == 'table'
    translate @decrypt_table, buf
  else
    if @decipher.nil?
      decipher_iv_len = get_cipher_len[1]
      decipher_iv     = buf[0..decipher_iv_len ]
      @iv             = decipher_iv
      @decipher       = get_cipher(0, @iv)
      buf             = buf[decipher_iv_len..-1]
      return buf if buf.length == 0
    end
    @decipher.update(buf)
  end
end

#encrypt(buf) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/shadowsocks/crypto.rb', line 49

def encrypt buf
  return buf if buf.length == 0
  if method == 'table'
    translate @encrypt_table, buf
  else
    if iv_sent
      @cipher.update(buf)
    else
      @iv_sent = true
      @cipher_iv + @cipher.update(buf)
    end
  end
end

#method_supportedObject Also known as: get_cipher_len



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/shadowsocks/crypto.rb', line 12

def method_supported
  case method
  when 'aes-128-cfb'      then [16, 16]
  when 'aes-192-cfb'      then [24, 16]
  when 'aes-256-cfb'      then [32, 16]
  when 'bf-cfb'           then [16, 8 ]
  when 'camellia-128-cfb' then [16, 16]
  when 'camellia-192-cfb' then [24, 16]
  when 'camellia-256-cfb' then [32, 16]
  when 'cast5-cfb'        then [16, 8 ]
  when 'des-cfb'          then [8,  8 ]
  when 'idea-cfb'         then [16, 8 ]
  when 'rc2-cfb'          then [16, 8 ]
  when 'rc4'              then [16, 0 ]
  when 'seed-cfb'         then [16, 16]
  end
end