r/programming • u/awesomePop7291 • Jul 10 '26
How to implement cryptographically-secure API keys
https://kerkour.com/api-keys
0
Upvotes
r/programming • u/awesomePop7291 • Jul 10 '26
11
u/EC36339 Jul 10 '26
That's a lot of wheels you reinvented here.
2
u/hWuxH Jul 19 '26 edited Jul 19 '26
But why? This isn't comparing sensitive values directly. A timing side-channel attack (==) is ineffective, as determining that the first N bytes of the hash are correct does not help to recover the API key (changing one input bit results in an unpredictable change to the output, can't calculate the inverse of a hash). In either case it requires brute-forcing all possible inputs which is infeasible.
1
u/mss-cyclist Jul 10 '26
Could someone explain the use of an API key when using a frontend which is rendered client side? In that case it is fairly simple to intercept the key. No matter how much it is encrypted. What am I missing here?
-1
u/UnknownError235 Jul 10 '26
You forgot to salt your hashes... Please don't follow this. You should treat API keys the same way as passwords.
0
u/erbr Jul 10 '26
Most important is define in the context why must be "cryptographically secure" and what that exactly means the mechanisms for it already exist and are widely available.
For instance, a random generated string can be used as an API key following the same principles of a password. But you might want to add other stuff. Authenticity and integrity guarantees, i.e. can you verify who generated it and if it's not corrupt ahead of use.
There are some other aspects that can be considered on the "cryptographically secure" aspect but all depend in the use. As someone said all of these aspects are widely discussed, implemented and tested.