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

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