github.com/database64128/shadowsocks-go@v1.10.2-0.20240315062903-143a773533f1/ss2022/crypto_test.go (about) 1 package ss2022 2 3 import ( 4 "crypto/rand" 5 "strconv" 6 ) 7 8 func newRandomCipherConfigTupleNoEIH(method string, enableUDP bool) (clientCipherConfig *ClientCipherConfig, userCipherConfig UserCipherConfig, err error) { 9 keySize, err := PSKLengthForMethod(method) 10 if err != nil { 11 return 12 } 13 psk := make([]byte, keySize) 14 if _, err = rand.Read(psk); err != nil { 15 return 16 } 17 clientCipherConfig, err = NewClientCipherConfig(psk, nil, enableUDP) 18 if err != nil { 19 return 20 } 21 userCipherConfig, err = NewUserCipherConfig(psk, enableUDP) 22 return 23 } 24 25 func newRandomCipherConfigTupleWithEIH(method string, enableUDP bool) (clientCipherConfig *ClientCipherConfig, identityCipherConfig ServerIdentityCipherConfig, userLookupMap UserLookupMap, err error) { 26 keySize, err := PSKLengthForMethod(method) 27 if err != nil { 28 return 29 } 30 31 iPSK := make([]byte, keySize) 32 if _, err = rand.Read(iPSK); err != nil { 33 return 34 } 35 iPSKs := [][]byte{iPSK} 36 37 var uPSK []byte 38 userLookupMap = make(UserLookupMap, 7) 39 for i := range 7 { 40 uPSK = make([]byte, keySize) 41 if _, err = rand.Read(uPSK); err != nil { 42 return 43 } 44 45 uPSKHash := PSKHash(uPSK) 46 var c *ServerUserCipherConfig 47 c, err = NewServerUserCipherConfig(strconv.Itoa(i), uPSK, enableUDP) 48 if err != nil { 49 return 50 } 51 52 userLookupMap[uPSKHash] = c 53 } 54 55 clientCipherConfig, err = NewClientCipherConfig(uPSK, iPSKs, enableUDP) 56 if err != nil { 57 return 58 } 59 identityCipherConfig, err = NewServerIdentityCipherConfig(iPSK, enableUDP) 60 return 61 }