import hashlib
import struct


def _hash_dimensions(dimensions_json):
  """Returns a 32 bits int that is a hash of the dimensions specified.

  dimensions_json must already be encoded as json.

  The return value is guaranteed to be non-zero so it can be used as a key id in
  a ndb.Key.
  """
  digest = hashlib.md5(dimensions_json).digest()
  # Note that 'L' means C++ unsigned long which is (usually) 32 bits and
  # python's int is 64 bits.
  return int(struct.unpack('<L', digest[:4])[0]) or 1