config.go 1.0 KB

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