I need to generate a MAC signature for data on a website, but it's in a HTML templating system where there's only a few functions available. The only hashing function available is md5. Ideally, I'd use a real hmac function, but since that's not an option here, is it secure to have something like the below?
secret_key = '...';
data = '{"user_id": 123, "timestamp": 12345}';
signature = md5(data + secret_key);
I know that hmac-md5 is considered secure, and I know length extension attacks are possible if you use the construction md5(secret_key + data), but is the reverse, md5(data + secret_key) also insecure? It seems like it shouldn't be vulnerable to length extension since the key gets added to the end. Or is this not an issue anyway for the case where data is a JSON string like above, since you can't add characters on to the end of a valid JSON string and have it still be valid?
The specific use-case is that I have a CMS that I'm theming using liquid templates, and the template needs to make requests to a API that I control. I need to know who the user is that's making the request, and verify that they are in fact logged-in on the CMS when the request arrives at my API. My solution is to use the above md5 signature system to sign some info about the user in the Liquid template compilation, and then on my API server I can verify that signature since the secret key is shared between both my API server and the liquid template, so I know the data hasn't been tampered with. The CMS I'm using is a hosted cloud service and provides no other way to verify the identify of the logged-in user thats available to javascript running on the page (at least that I know of).
{% data | append: "...secret_key..." | md5 %}, but that runs on the server. Only the resulting value appears in the compiled HTML, the key never shows up in the client. – David Chanin Dec 29 '20 at 01:00hash(key + data)instead ofhash(data + key)for collision resistance, as long as length extension isn't an issue. length extension is an issue formd5, but since I'm using a JSON-encoded string as the data, I think that should be resistant to length extension anyway because you can't add meaningful characters to the end of a JSON string and still have it be valid JSON? So tldr it's basically OK? – David Chanin Dec 29 '20 at 01:08hmac_sha1orhmac_sha256function available, orsha256function available? From the looks of the documentation, most Liquid template implementations (e.g. Shopify, Tines, Braze, etc.) have those functions as string filters. – Polynomial Dec 29 '20 at 01:08