From: Ian Leitch Date: 2007-08-17T04:32:53+09:00 Subject: Re: shorten SHA1-hash ------=_Part_55488_4715349.1187292761991 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline You could use String#hash instead. For strings more than 6 characters (or something), it'll return a negative number, so it's a little hacky, but: file_contents.hash.to_s.tr('-', '0') I'm not saying this is a nice approach, but it is pretty short ;) Anyone with more knowledge of String#hash care to chime in? Also, I doubt this is very cross platform friendly, just take a look at all the ifdefs in the C hash implementation. On 16/08/07, Peter Skovgaard wrote: > > In one of my railsapplications I have system for up/downloading files. > Since I don't want people to browse all the files, but anyone should > be able to link to his/her files each file has it's own unique URL - > all pretty standard. > > Right now the URL just is /download/sha-sum-of-the-file (since I have > the SHA anyway), but the length of the url is bugging me. > > Now - the problem: The SHA1-sum is, as usually, just represented in > base16 which makes it 40 chars long. However, in an url we have at > least a-zA-Z0-9 (and also some special characters) which gives us the > opportunity to represent the SHA-sum in at least base62 which should > make it about half the size. > > I know that I in this case just could store an extra little random > string in my database and link to /download/little-string, but that's > not the point here :) I would just like to hear you: How few > (printable) characters can you shorten a SHA-sum down to? > -- > Posted via http://www.ruby-forum.com/. > > ------=_Part_55488_4715349.1187292761991--