3

I'm new to MongoDB and am wondering how to decide to store some cryptographic hashes. Should I store the binary data itself, as a blob, or should I store its string representation as a string?

My instinct is to just use a string and be done with it, but I'm not sure if this is the best approach. A blob would take up less space, for example.

  • Is there any negative impact, coding-wise, of using a blob (i.e. harder to pass around or reference objects?)
  • Are there any "gotchas" if the _id of a document is a blob?
  • Is there any benefit of using a blob at all?
5
  • 1
    I've used blobs containing the hash of the document as _id without any problems. Commented Feb 22, 2016 at 22:01
  • You should look into MongoDB's encryption options for design patterns.
    – rozar
    Commented Feb 22, 2016 at 23:02
  • Using hex means a 2-fold increase in your data size. Using Base64 or something similar even less. Do you have that many hashes to store that this would be a real concern?
    – 5gon12eder
    Commented Feb 22, 2016 at 23:44
  • @5gon12eder: Hmm, I will have to store a few million of these... they'd be 64 bytes instead of 32 bytes, so it may add up to, say, 100 MB eventually... which is not the worst thing, but it does get noticeable.
    – Claudiu
    Commented Feb 22, 2016 at 23:50
  • @Claudiu If it were just an non indexed field it wouldn't matter that much. But _id is indexed, so the index becomes bigger and potentially slower. Commented Feb 23, 2016 at 8:18

1 Answer 1

2

We've done it both ways and it doesn't really make much of a difference. But we've been going toward blobs on newer projects, which the PostgreSQL (our database of choice) command line shows in hex-coded format by default anyway.

The crypto APIs also tend to pass around the hashes as byte[] so blobs are a nice fit.

Finally, by using byte[] one can make an attempt to zero-out the array contents when done with the array.

Slightly off topic: noted "attempt" to zero-out because in Java objects may be moved around in memory without your control. We can zero-out an array and a previous copy of its contents may remain in physical memory and be available outside the process. We just do what we can and haven't looked into any JNI/sun.misc.Unsafe ways to pin objects.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.