News for hackers. Stuff that matters.
Re:Blog post id enumeration can lead to unwanted information... by pbreit on Monday February 29, 2016 @01:03AM (#11193857)
Newbie here: for every one of my projects, I go out hunting for a decent way to do record IDs and still am coming up short. I dislike mightily UUIDs for the user-unfriendly lengths. My preference would be for something like airline codes or YouTube IDs: 6-10 alphanumeric IDs. Should I just be generating a hash and actually storing it in the DB after checking for collisions? Or is there a way to encrypt/decrypt to a specific length with a specific set of usable characters?
Re:Blog post id enumeration can lead to unwanted information... by kjksf on Monday February 29, 2016 @01:25AM (#11193911)
You can use http://hashids.org/. It obfuscated the real ids with reversible (but secret and un-guessable) transformation so on the server I deal with numeric ids (like 5) but the browser sees something like vgRg. I wrote up a bit more about this: https://quicknotes.io/n/vgRg-how-to-prevent-competitors-from...
Re:Blog post id enumeration can lead to unwanted information... by dchest on Monday February 29, 2016 @04:56AM (#11194531)
The are not secret and not unguessable, they use some homegrown cipher, calling the result of encryption a "hash" for some reason (http://carnage.github.io/2015/08/cryptanalysis-of-hashids/)
Re:Blog post id enumeration can lead to unwanted information... by yetanothercoder on Monday February 29, 2016 @09:31AM (#11195613)
It's for search/seo reasons.

http://hashids.org/#why-hashids

Re:Blog post id enumeration can lead to unwanted information... by michaelt on Monday February 29, 2016 @11:59AM (#11196563)
I see in your note you say there are many implementations of this idea

Is there a general name for this pattern? I'd like to find some of the other libraries for doing the same thing.

Re:Blog post id enumeration can lead to unwanted information... by imron on Monday February 29, 2016 @01:32AM (#11193922)
The link you provided to the write up is not a valid link.
Re:Blog post id enumeration can lead to unwanted information... by kjksf on Monday February 29, 2016 @03:23AM (#11194245)
Sorry about that, it was a private note. Fixed now.
Re:Blog post id enumeration can lead to unwanted information... by dchest on Monday February 29, 2016 @05:00AM (#11194540)
Just generate a random string (make sure to use the proper generator, e.g. https://gist.github.com/dchest/751fd00ee417c947c252). If the result is shorter than, say 100 bits, you'll have to check for collisions (e.g. unique constraint in database).

Alternatively, if you have 64-bit sequences as IDs (e.g. autoincremented ids in SQL database), you can encrypt then using a real 64-bit block cipher, such as Blowfish or XTEA, with a secret key known only to server. This can give you unsigned 64-bit numbers, which are usable in existing systems (except in JavaScript, where maximum integer has 53 bits), for example:

   1 -> 3491820675944448233
   2 -> 9264994595240464886
If you use base64 encoding, it will give you 11-character strings, e.g. "0AdUgU8OZfo". Make sure to use URL-safe encoding, though (instead of +, / it uses -, _).
Re:Blog post id enumeration can lead to unwanted information... by infogulch on Monday February 29, 2016 @03:36PM (#11198255)
Or even better: base58 encoded[0].

>Compared to Base64, the following similar-looking letters are omitted: 0 (zero), O (capital o), I (capital i) and l (lower case L) as well as the non-alphanumeric characters + (plus) and / (slash).

[0]: https://en.wikipedia.org/wiki/Base58

Re:Blog post id enumeration can lead to unwanted information... by stan_rogers on Monday February 29, 2016 @01:39AM (#11193937)
A base 30 time stamp will give you about the right number of characters, case-insensitive, and without the possibly-ambiguous 0, 1, O, I, L (lower-case ambiguous) and Q (possibly upper-case ambiguous) using 23456789ABCDEFGHJKMNPRSTUVWXYZ as valid characters. Adding back the 0 and 1 (that is, anybody listening to a user on a support call, or any code, would automatically interpret I or L as 1 and O or Q as 0) gives you base 32 and a shorter ID code. If milliseconds are going to be too collision-prone (that is, you can't recuperate the time you'll occasionally have to advance), a short entity code (user or server name abbreviation, f'rinstance) prepended with a dash should make it memorable and user-friendly.
Re:Blog post id enumeration can lead to unwanted information... by gmac on Monday February 29, 2016 @01:53AM (#11193975)
I suggest you remove the remaining vowels from that, giving base 27. Then you have no worries about accidentally generating obscenities.
Re:Blog post id enumeration can lead to unwanted information... by stan_rogers on Monday February 29, 2016 @02:12AM (#11194027)
Oddly, nobody ever seems to worry about that with hashes, encryption output or GUIDs... but if it's a worry, then, sure, hey, why not. (By the way, that system has been in regular use for more than 20 years in commercial software.)
Re:Blog post id enumeration can lead to unwanted information... by zo1 on Monday February 29, 2016 @02:21AM (#11194057)
Thank you for that! I could have had a very front-facing "dirty" bug make it into production soon.
Re:Blog post id enumeration can lead to unwanted information... by dsr_ on Monday February 29, 2016 @08:20AM (#11195250)
GDDMNSHTFCK.
Re:Blog post id enumeration can lead to unwanted information... by gmac on Wednesday March 09, 2016 @07:26AM (#11252530)
Bless you!
Re:Blog post id enumeration can lead to unwanted information... by mappu on Monday February 29, 2016 @01:35AM (#11193928)
In the past i've seen a 32/64bit block cipher used (e.g. Skip32) with a hardcoded key, as an int->int reversable obfuscating transform.
Re:Blog post id enumeration can lead to unwanted information... by mcao on Monday February 29, 2016 @01:26AM (#11193914)
You can simply use the database integer identity column and convert it into a series of alpha numeric characters. With a little effort, you can generate randomized, unique values. I've written a library that does exactly that, https://github.com/mikecao/hashkit.
Re:Blog post id enumeration can lead to unwanted information... by schoen on Monday February 29, 2016 @01:04AM (#11193858)
Re:Blog post id enumeration can lead to unwanted information... by ars on Monday February 29, 2016 @03:10AM (#11194190)
Easiest way is simply XOR the database auto-increment ID with a fixed number, then truncate to (AND to) however many bits seem reasonable for your site.

Not super secure, but good enough for this purpose.

Unless you have a long ID people can always simply try every ID, no matter what format you use. So fancy encryption on the ID isn't all that useful.

If security is important then generate a random 64bit number and store that, rather than let the database auto-generate a number.

Re:Blog post id enumeration can lead to unwanted information... by JoshTriplett on Monday February 29, 2016 @03:32AM (#11194274)
> Easiest way is simply XOR the database auto-increment ID with a fixed number, then truncate to (AND to) however many bits seem reasonable for your site.

XOR has a trivial known-plaintext attack, and you don't even need to determine the entire key to get something useful. If you can create a few posts in order and obtain a series of sequential items, you can obtain the low-order bits of the number XORed in, at which point you can easily browse around for adjacent items from any post.

Re:Blog post id enumeration can lead to unwanted information... by ars on Monday February 29, 2016 @03:52AM (#11194320)
Like I said, not secure. But still useful for certain cases.
Re:Blog post id enumeration can lead to unwanted information... by dchest on Monday February 29, 2016 @09:37AM (#11195639)
Such as? This doesn't hide IDs, as parent poster mentions. You could as well just use IDs without any transforms.