Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
hashmessages handling collisions
let getMessageHash = function(hash, fn) {
dm.settings.log("getmessage hash", hash);
dm.rcmsg.get("m:"+ hash, function(err, data) {
if (err) {
dm.settings.log('message hash error', err);
}
if (!data) {
dm.settings.log("message hash not found?!", hash)
return fn(null,{message: 'messagehash error', message_text: 'messagehash error', hash: hash})
}
var j = JSON.parse(data);
dm.settings.log("messagehash", j);
return fn(err, j);
});
}
let saveMessageHash = function(mj, fn) {
var hash = crypto.createHash('sha256').update(mj.message_text).digest("hex");
dm.rcmsg.exists("m:" + hash, function(err, exists) {
if (exists) {
dm.rcmsg.get("m:" +hash, function(err, existing) {
var j = JSON.parse(existing);
if (j.message_text === mj.message_text) {
//dm.settings.log("hash found")
return fn(null, hash);
} else {
dm.settings.log("sha256 collision");
dm.rcmsg.incr("c:" + hash, function(err, id) {
var newhash = hash + id.toString();
var data = {message_text: mj.message_text, message: mj.message };
console.log("saving hash", newhash, data);
dm.rcmsg.set("m:" + newhash, JSON.stringify(data), function(err, done) {
return fn(null, newhash);
});
})
}
})
} else {
var data = {message_text: mj.message_text, message: mj.message};
console.log("saving message", hash, data);
dm.rcmsg.set("m:" + hash, JSON.stringify(data), function(err, done) {
return fn(null, hash);
})
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment