Skip to content

Instantly share code, notes, and snippets.

@johnlpage
Created February 7, 2022 16:12
Show Gist options
  • Save johnlpage/ea0cb95fd29f29e100665f3417e51582 to your computer and use it in GitHub Desktop.
Save johnlpage/ea0cb95fd29f29e100665f3417e51582 to your computer and use it in GitHub Desktop.
var sign = (doc,fields) => {
let toSign=""
fields.forEach(f => { toSign += parseFloat(doc[f]) + "." } )
toSign = toSign.replace(/NaN/,'.')
print(`Signing: ${toSign}`)
iv = 56781234
for(c=0;c<toSign.length;c++) {
v = parseInt(toSign[c])
if(isNaN(v)) v=10; //Dots etc
v += 1; //No point multiplying 0 by anything
print(v)
iv = (iv + ( 227 ** ((c+1)%6) * v)) % 1048576
}
return iv
}
doc = {_id: "test", x:1,y:1,s:5.5}
doc.cksum = sign(doc)
try { db.readonlyfields.insertOne( doc) } catch(e) {print(e.errmsg)}
{ "acknowledged" : true, "insertedId" : "test" }
try { db.readonlyfields.updateOne( {_id:"test"}, {$set:{b:2}}) } catch(e) {print(e.errmsg)}
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
try { db.readonlyfields.updateOne( {_id:"test"}, {$set:{x:2}}) } catch(e) {print(e.errmsg)}
Document failed validation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment