Yes, one should use a hmac for hashing multiple inputs, for the reason you explained.
Edit: s/hmac/incremental hashing/
What do you mean by "incremental hashing"? Note that the Init-Update-Finalize API provided by many cryptography libraries doesn't protect against this - calling Update multiple times is equivalent to hashing a concatenated string.
Not quite. HMAC helps to prevent length extensions attacks (if the underlying hash was vulnerable in the first place), and the secret prevents attackers from predicting the hash value (like OP did).
But HMAC doesn't help against ambiguously encoded inputs:
You want a way to unambiguously join the values. Common solutions are:- prepending the length of each field (in a fixed number of bytes);
- encoding the input as JSON or other structured format;
- padding fields to fixed lengths;
- hashing fields individually, then hashing their concatenation;
- use TupleHash, designed specifically for this case: https://www.nist.gov/publications/sha-3-derived-functions-cs...