Menu

Our API keys are 240 bits, shown once, stored as a hash, and one GET route needs a write key
📰

Our API keys are 240 bits, shown once, stored as a hash, and one GET route needs a write key

DEV Community: nextjs·Daniel Pertu·1 day ago
#oHEeIwtP
#devtonextjs#devto#dev#hash#request
Long article (539 words) — no AI summary yet.
Reading 0:00
15s threshold

Customers can read their results and invite candidates from their own systems through a small JSON API. The interesting part was not the endpoints, it was deciding what a key is allowed to be, and what we are willing to store.

The reference is public if you want to follow along: the API docs .

A key is 240 bits, shown once

A key looks like cpa_ followed by 40 characters from 30 random bytes:

export function generateApiKey ( ) { const key = `${API_KEY_PREFIX}${randomBytes(30).toString('base64url')}` ; return { key , prefix : key . slice ( 0 , VISIBLE_LENGTH ) , hash : hashApiKey ( key ) } ; }

Enter fullscreen mode

Exit fullscreen mode

The owner sees the full key once, when it is created. We store only its SHA-256 hash, plus the first twelve characters in the clear so a person can recognise a key in the list. A copy of the database is therefore no use for calling the API.

A plain SHA-256 is fine here, and a slow password hash would be wrong.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.