(password string, method string)
| 37 | } |
| 38 | |
| 39 | func 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. |
| 68 | func GrpcCodeToHTTP(code codes.Code) int { |
no outgoing calls
no test coverage detected