Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best approach for generating a new API key?

So with lots of different services around now, Google APIs, Twitter API, Facebook API, etc etc.

Each service has an API key, like:

AIzaSyClzfrOzB818x55FASHvX4JuGQciR9lv7q

All the keys vary in length and the characters they contain, I'm wondering what the best approach is for generating an API key?

I'm not asking for a specific language, just the general approach to creating keys, should they be an encryption of details of the users app, or a hash, or a hash of a random string, etc. Should we worry about hash algorithm (MSD, SHA1, bcrypt) etc?

Edit: I've spoke to a few friends (email/twitter) and they recommended just using a GUID with the dashes stripped.

This seems a little hacky to me though, hoping to get some more ideas.

like image 465
Phill Avatar asked Jan 19 '13 07:01

Phill


People also ask

Which is the recommended way to transmit an API key?

A CARTO API Key is physically a token/code of 12+ random alphanumeric characters. You can pass in the API Key to our APIs either by using the HTTP Basic authentication header or by sending an api_key parameter via the query string or request body. If you use our client library CARTO.


1 Answers

Use a random number generator designed for cryptography. Then base-64 encode the number.

This is a C# example:

var key = new byte[32]; using (var generator = RandomNumberGenerator.Create())     generator.GetBytes(key); string apiKey = Convert.ToBase64String(key); 
like image 187
Edward Brey Avatar answered Sep 19 '22 16:09

Edward Brey