<p>I'm writing a web app that points to external links. I'm looking to create a non-sequential, non-guessable id for each document that I can use in the URL. I did the obvious thing: treating the url as a string and str#crypt on it, but that seems to choke on any non-alphanumberic characters, like the slashes, dots and underscores.</p>

<p>Any suggestions on the best way to solve this problem?</p>

<p>Thanks!</p>

[{'answer_id': 67900, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Use <a href="http://ruby-doc.org/stdlib/libdoc/digest/rdoc/index.html" rel="nofollow noreferrer">Digest::MD5</a> from Ruby\'s standard library:</p>\n\n<pre><code>Digest::MD5.hexdigest(my_url)\n</code></pre>\n'}, {'answer_id': 68028, 'author': 'manveru', 'author_id': 8367, 'author_profile': 'https://Stackoverflow.com/users/8367', 'pm_score': 6, 'selected': True, 'text': '<p>Depending on how long a string you would like you can use a few alternatives:</p>\n\n<pre><code>require \'digest\'\nDigest.hexencode(\'http://foo-bar.com/yay/?foo=bar&amp;a=22\')\n# "687474703a2f2f666f6f2d6261722e636f6d2f7961792f3f666f6f3d62617226613d3232"\n\nrequire \'digest/md5\'\nDigest::MD5.hexdigest(\'http://foo-bar.com/yay/?foo=bar&amp;a=22\')\n# "43facc5eb5ce09fd41a6b55dba3fe2fe"\n\nrequire \'digest/sha1\'\nDigest::SHA1.hexdigest(\'http://foo-bar.com/yay/?foo=bar&amp;a=22\')\n# "2aba83b05dc9c2d9db7e5d34e69787d0a5e28fc5"\n\nrequire \'digest/sha2\'\nDigest::SHA2.hexdigest(\'http://foo-bar.com/yay/?foo=bar&amp;a=22\')\n# "e78f3d17c1c0f8d8c4f6bd91f175287516ecf78a4027d627ebcacfca822574b2"\n</code></pre>\n\n<p>Note that this won\'t be unguessable, you may have to combine it with some other (secret but static) data to salt the string:</p>\n\n<pre><code>salt = \'foobar\'\nDigest::SHA1.hexdigest(salt + \'http://foo-bar.com/yay/?foo=bar&amp;a=22\')\n# "dbf43aff5e808ae471aa1893c6ec992088219bbb"\n</code></pre>\n\n<p>Now it becomes much harder to generate this hash for someone who doesn\'t know the original content and has no access to your source.</p>\n'}, {'answer_id': 68954, 'author': 'webmat', 'author_id': 6349, 'author_profile': 'https://Stackoverflow.com/users/6349', 'pm_score': 2, 'selected': False, 'text': '<p>I would also suggest looking at the different algorithms in the digest namespace. To make it harder to guess, rather than (or in addition to) salting with a secret passphrase, you can also use a precise dump of the time:</p>\n\n<pre><code>require \'digest/md5\'\ndef hash_url(url)\n  Digest::MD5.hexdigest("#{Time.now.to_f}--#{url}")\nend\n</code></pre>\n\n<p>Since the result of any hashing algorithm is not guaranteed to be unique, don\'t forget to check for the uniqueness of your result against previously generated hashes before assuming that your hash is usable. The use of Time.now makes the retry trivial to implement, since you only have to call until a unique hash is generated.</p>\n'}]
['https://Stackoverflow.com/questions/67890', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/10461/']