Skip to content

Instantly share code, notes, and snippets.

@sawaYch
Last active October 26, 2018 22:11
Show Gist options
  • Save sawaYch/b2260f2bcb0266fe12668431ed0d6742 to your computer and use it in GitHub Desktop.
Save sawaYch/b2260f2bcb0266fe12668431ed0d6742 to your computer and use it in GitHub Desktop.
Is there any better way to keep api key private if you push code over here :/ ?

Method 1 (demo using C++) - Encapsulate your api key with class

Create a api key vault class

Like this :

#ifndef API_KEY_VAULT
#define API_KEY_VAULT

#include <string>
using namespace std;

class apiKeyVault{
private:
    static string api_key;
public:
    apiKeyVault(){}
    ~apiKeyVault(){}
    static string get_apiKey(){
        return api_key;
    }
    
};

string apiKeyVault::api_key = "_please_put_your_api_key_here_";

#endif

Call the static member function

...
int main(){
  ...
  ...
  string request("GET /api/latest?access_key="+ apiKeyVault::get_apiKey() +"&format=1\r\n\r\n");
  ...
  ...
}

Added api key vault class in .gitignore

Added class source code to .gitignore, so that it wont push to remote server.

...
apiKey.*
*.exe
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment