logoalt Hacker News

My adventure in designing API keys

76 pointsby vjay15last Sunday at 1:17 PM58 commentsview on HN

Comments

bob1029today at 7:26 AM

I don't understand the need for this level of engineering. It appears we are going for an opaque bearer token here. The checksum is pointless because an entire 512 bit token still fits in an x86 cache line. Comparing the whole sequence won't show up in any profiler session you will ever care about.

If you want aspects of the token to be inspectable by intermediaries, then you want json web tokens or a similar technology. You do not want to conflate these ideas. JWTs would solve the stated database concern. All you need to store in a JWT scheme are the private/public keys. Explicit tracking of the session is not required.

show 2 replies
weitendorftoday at 7:49 AM

Hey OP, sorry for the negativity, I think most of these commenters right now are pretty off-base. My company is building a lot of API infrastructure and I thought this was a great write up!

show 1 reply
randomint64today at 8:03 AM

While it's true that API keys are basically prefix + base32Encode(ID + secret), you will want a few more things to make secure API keys: at least versioning and hashing metadata to avoid confused deputy attacks.

Here is a detailed write-up on how to implement production API keys: https://kerkour.com/api-keys

show 2 replies
Savagemantoday at 8:01 AM

Side note: the slug prefix is not primarily intended for the end-user / developer to figure out which kind of key it is, but for security scanners to detect when they are committed to code / leaked and invalidate them.

show 1 reply
calraintoday at 6:38 AM

I don't like giving away any information what-so-ever in an API key, and would lean towards a UUIDv7 string, just trying to avoid collisions.

Even the random hex with checksum component seems overkill to me, either the API key is correct or it isn't.

show 2 replies
ramchiptoday at 7:44 AM

The purpose of the checksum is to help secret scanners avoid false positives, not to optimize the (extremely rare) case where an API key has a typo

show 2 replies
hk__2today at 10:49 AM

> I didn't proceed with this approach since I don't want the API keys to have any info regarding the account, but hey it is all just a matter of preference and opinion.

Well I would have done that and saved half the blog post.

vjay15last Sunday at 1:17 PM

Hello everyone this is my third blog, I am still a junior learning stuff ^_^

show 2 replies
petterroeatoday at 9:26 AM

A bit over-engineered, but it was fun to read about observations on industry standard API keys. I agree it would be nice with more discussion around API keys and qualities one would want from them.

pdhborgestoday at 8:45 AM

I don't even understand what approach 3 is doing. They ended up hashing the random part of the API key with an hash function that produces a small hash and stored that in the metashard server is that it?

show 1 reply
tlonnytoday at 8:38 AM

Presumably because API keys are n bytes of random data vs. a shitty user-generated password we don’t have to bother using a salt + can use something cheap to compute like SHA256 vs. a multi-round bcrypt-like?

show 1 reply
matjatoday at 10:10 AM

What if the "slug" was a prefix for the API key revocation URL, so the API key was actually a valid URL that revoked itself if fetched/clicked? :)

ameliustoday at 9:41 AM

It's a bit confusing that the "Random hex" example contains characters such as "q" and "p".

show 1 reply
dhruv3006today at 7:03 AM

Hey - this was a great blog ! I liked how you used the birthday paradox here.

PS : I too am working on a APIs.Take a look here : https://voiden.md/

usernametaken29today at 7:22 AM

I know sometimes people just like to try things out, but for the love of god do not implement encryption related functionality yourself. Use JWT tokens and OpenSSL or another established library to sign them. This problem is solved. Not essentially solved, solved. Creating your own API key system has a high likelihood of fucking things up for good!

show 2 replies
grugdev42today at 9:20 AM

Everything about this is over engineered. Just KISS.

sneaktoday at 9:43 AM

This is a very good example of premature optimization.

MORPHOICEStoday at 7:03 AM

[dead]

adaptittoday at 8:53 AM

[dead]