Search code examples
phpmysqlsqlencryptioncrypt

Crypt() user's ID to be alphanumeric for use in API Request URL


When a user adds an account to their profile that account will be given an Auto-incremented ID as expected by the database.

I use this ID and then use php's crypt($account_id, 'salt_here') function with a custom, static salt to generate a safe, public ID that I don't care if the user sees and then add this to the database for the related account for future use.

This crypted id I want to use in a REST API I'm currently building.

Here's where I am:

  • User uses API to get accounts on profile /v1/accounts/PROFILE_USERNAME
  • Each account result shows crypted_id which I just named as id in the result data (I don't want to show the real ID from the database) "id": "Mq5RFENsi4/rw"
  • User can then loop through the account data from the API request and get the crypted_id and then make another API request (/v1/accountSiblings/ACCOUNT_CRYPTED_ID) (ACCOUNT_CRYPTED_ID = Mq5RFENsi4/rw) for further information about the account, for instance, how many siblings the account has.

The problem I have is that crypt() does not return alphanumeric results so the data is iffy when passed into the URL for another API request especially considering it can return forward-slashes and other characters I don't want.

Is there another 1-way crypt function I can use that is only alphanumeric to be used as the crypted id?

Is there a better way to achieve what I'm trying to do?

Hope I'm clear enough, thanks


Solution

  • It's 4am so bear with me.

    I think it will be just fine for me to simply md5() the crypt's output...

    It doesn't matter if that md5 hash is decrypted because the end result is basically gobbledegook and is a 1-way hash.

    Obvious now that I think about it...