github.com/xraypb/xray-core@v1.6.6/proxy/trojan/config.go (about)

     1  package trojan
     2  
     3  import (
     4  	"crypto/sha256"
     5  	"encoding/hex"
     6  	fmt "fmt"
     7  
     8  	"github.com/xraypb/xray-core/common"
     9  	"github.com/xraypb/xray-core/common/protocol"
    10  )
    11  
    12  // MemoryAccount is an account type converted from Account.
    13  type MemoryAccount struct {
    14  	Password string
    15  	Key      []byte
    16  	Flow     string
    17  }
    18  
    19  // AsAccount implements protocol.AsAccount.
    20  func (a *Account) AsAccount() (protocol.Account, error) {
    21  	password := a.GetPassword()
    22  	key := hexSha224(password)
    23  	return &MemoryAccount{
    24  		Password: password,
    25  		Key:      key,
    26  		Flow:     a.Flow,
    27  	}, nil
    28  }
    29  
    30  // Equals implements protocol.Account.Equals().
    31  func (a *MemoryAccount) Equals(another protocol.Account) bool {
    32  	if account, ok := another.(*MemoryAccount); ok {
    33  		return a.Password == account.Password
    34  	}
    35  	return false
    36  }
    37  
    38  func hexSha224(password string) []byte {
    39  	buf := make([]byte, 56)
    40  	hash := sha256.New224()
    41  	common.Must2(hash.Write([]byte(password)))
    42  	hex.Encode(buf, hash.Sum(nil))
    43  	return buf
    44  }
    45  
    46  func hexString(data []byte) string {
    47  	str := ""
    48  	for _, v := range data {
    49  		str += fmt.Sprintf("%02x", v)
    50  	}
    51  	return str
    52  }