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).