Skip to Content

Mysql search with md5 hashed uuid

Posted on

In my recent project, my users table contains id field with format: 077789a2-a9c6-4bbf-84c3-64c6d016c3a1

For security reasons, clients don’t know exactly user’s id, but they know md5 hashed id. For example, with above id, client will know 453c1c1cb938accbdb360b448788981f string as id.

With users search function, client will send md5 hashed id to server. And the code below is which we use to search user.

  SELECT * from users
  WHERE MD5(
    CONCAT(
      SUBSTR(HEX(users.id), 1, 8),
      '-',
      SUBSTR(HEX(users.id), 9, 4),
      '-',
      SUBSTR(HEX(users.id), 13, 4),
      '-',
      SUBSTR(HEX(users.id), 17, 4),
      '-',
      SUBSTR(HEX(users.id), 21)
    )
  ) = '453c1c1cb938accbdb360b448788981f'

Reference

comments powered by Disqus