2

I need to determine if objects are super/subsets of one another.

Given a function hash that generates fixed-length string hashes from arbitrary objects:

const fooHash = hash({foo: "foo"}) 
const barHash = hash({bar: "bar"}) 

const fooBarHash = hash({foo: "foo", bar: "bar"})

Is there a combine function, such that:

combine(fooHash, barHash) === fooBarHash

?

If yes, how would the combine function work?

3
  • By combine you probably mean $.extend(obj1,obj2)? Commented Dec 20, 2015 at 18:21
  • 2
    Note that OP is asking for a function that reads "string hashes". Commented Dec 20, 2015 at 18:23
  • The combine function I am searching for should operate on the hashes, not on the objects. Commented Dec 20, 2015 at 18:23

1 Answer 1

1

If the hashing function is reversible, then you can have the following combine function:

function combine(obj1hash, obj2hash) {
  return hash(merge(unhash(obj1hash), unhash(obj2hash)));
}

hash and unhash can be JSON.stringify and JSON.parse. merge is any function that merges two js objects like this one.

Sign up to request clarification or add additional context in comments.

9 Comments

if it's not possible to reverse the action, it's not doable. On the other hand, you can get away without converting back to POJO.
That would work, but JSON.stringify will break on cyclical objects. Also, I didn't say so, but by hashes, I intended "fixed-size string hashes". For my use case, I need to avoid serializing the entire object, because I want to quickly compare a lot of objects. I'll update my question accordingly :)
Yeah I thought about that. As mentioned it's probably not possible. This is a cryptography-related question and not a javascript one.
I'm not an expert in cryptography, but I'd say 90% yes it's not possible.
@anatol Let me suggest something else. What you need is a function contains(hash(obj1), hash(parent)) which returns true when parent is contained within obj1. This might have a better chance of finding a solution.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.