MCPcopy Create free account
hub / github.com/PasarGuard/node / EnsureBase64Password

Function EnsureBase64Password

common/helper.go:39–65  ·  view source on GitHub ↗
(password string, method string)

Source from the content-addressed store, hash-verified

37}
38
39func EnsureBase64Password(password string, method string) string {
40 // First check if it's already a valid base64 string
41 decodedBytes, err := base64.StdEncoding.DecodeString(password)
42 if err == nil {
43 // It's already base64, now check if length is appropriate
44 if (strings.Contains(method, "aes-128-gcm") && len(decodedBytes) == 16) ||
45 ((strings.Contains(method, "aes-256-gcm") || strings.Contains(method, "chacha20-poly1305")) && len(decodedBytes) == 32) {
46 // Already correct length
47 return password
48 }
49 }
50
51 // Hash the password to get a consistent byte array
52 hasher := sha256.New()
53 hasher.Write([]byte(password))
54 hashBytes := hasher.Sum(nil)
55
56 // Resize based on method
57 var keyBytes []byte
58 if strings.Contains(method, "aes-128-gcm") {
59 keyBytes = hashBytes[:16] // First 16 bytes for AES-128
60 } else {
61 keyBytes = hashBytes[:32] // First 32 bytes for AES-256 or ChaCha20
62 }
63
64 return base64.StdEncoding.EncodeToString(keyBytes)
65}
66
67// GrpcCodeToHTTP maps gRPC codes to HTTP status codes.
68func GrpcCodeToHTTP(code codes.Code) int {

Callers 1

checkShadowsocks2022Function · 0.92

Calls

no outgoing calls

Tested by

no test coverage detected