I am building a Drupal website with a lot of user-specific information that will be posted using jQuery/ajax. The information it self is not very sensitive, it is just important to verify that the form-data has not been tampered with tools like Firebug, as well as ensuring the information is really requested from the specified user. In other words, I am trying to figure out the best way to protect the integrity and authenticity of the data when posting with ajax.
Ideally I would like to use some well known message authentication system, like the HMAC algorithm. But since this incorporates a symmetric key, I don't see how I can encrypt the POST data without exposing the secret key in my javascript file (which obviously is visible to anyone).
Please correct me if I have got the wrong idea about how this should work.
For example, info I need to send
field1=x&field2=y&uid=10
...then calculate the hash of the data together with a secret key. Is this possible to do without exposing the hash function in my javascript code?
CHECKSUM: hash(postdata, "secret_key")
... and finally append checksum to original postdata.
field1=x&field2=y&uid=1&c=CHECKSUM
Alternative
An alternative I though of was using the session ID of the logged in user. This however would not check the integrity of the message...
When generating form with PHP, I can generate a hidden input with following
CHECKSUM: hash(session id for the current user, "secretkey")
What I then would post using ajax is
field1=x&field2=y&uid=10&c=CHECKSUM
With this it would be fairly secure to authenticate the appropriate user (again pseudo-code)
ssid = SELECT ssid FROM sessions WHERE uid = $_POST[uid]
if(ssid && hash(ssid, "secretkey") == $_POST[c]) {
//User OK
} else {
//Invalid user
}