title: Hex digest for Python's built-in hash function
body: <p>I need to create an identifier token from a set of nested configuration values.
  The token can be part of a URL, so – to make processing easier – it should contain only hexadecimal digits (or something similar).
  The config values are nested tuples with elements of hashable types like <code>int</code>, <code>bool</code>, <code>str</code> etc.</p>
  
  <p>My idea was to use the built-in <code>hash()</code> function, as this will continue to work even if the config structure changes.
  This is my first attempt:</p>
  
  <pre><code>def token(config):
      h = hash(config)
      return '{:X}'.format(h)
  </code></pre>
  
  <p>This will produce tokens of variable length, but that doesn't matter.
  What bothers me, though, is that the token might contain a leading minus sign, since the return value of <code>hash()</code> is a signed integer.</p>
  
  <p>As a way to avoid the sign, I thought of the following work-around, which is adding a constant to the hash value.
  This constant should be half the size of the range the value of <code>hash()</code> can take (which is platform-dependent, eg. different for 32-/64-bit systems):</p>
  
  <pre><code>HALF_HASH_RANGE = 2**(sys.hash_info.width-1)
  </code></pre>
  
  <p>Is this a sane and portable solution?
  Or will I shoot myself in the foot with this?</p>
  
  <p>I also saw suggestions for using <code>struct.pack()</code> (which returns a <code>bytes</code> object, on which one can call the <code>.hex()</code> method), but it also requires knowing the range of the hash value in advance (for the choice of the right format character).</p>
  
  <p><strong>Addendum:</strong><br>
  Encryption strength or collisions by chance are not an issue.
  The drawback of the <code>hashlib</code> library in this scenario is that it requires writing a converter that traverses the input structure and converts everything into a <code>bytes</code> representation, which is cumbersome.</p>