I am saving go structs in mongo using mgo. I wish to save them with a hash of that struct (and a secret) to determine whether they have been tampered with (and I do not wish the mongo db itself to have the secret).
Currently I am hashing the structs by serializing them using gob
whose ordering of struct components is well-defined. This works great, save when I go to reread the struct from mango, things have changed - to be precise the time values in mongo have truncated accuracy compared to go - therefore the hashes do not match up.
My planned work around for this is simply to marshall and unmarshall the struct from BSON before calculating the hash, i.e.:
- Marshal struct to BSON
- Unmarshal struct from BSON (thereby losing precision on time)
- Marshall struct to gob and hash resultant
[]byte
- Put hash in struct
- Save struct to mongo
Now, that's more than a little circuitous.
If I could guaranteed that the BSON itself always preserved order of components in structs, I could:
- Marshal struct to BSON
- hash resultant
byte[]
- Put hash in struct
- Save struct to mongo
Which would be less nasty (albeit that it would still require converting to BSON twice).
Any ideas?