Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have a tbl where all entries for companies that participate in a online project are saved. This table is named "business". Those companies can use their employess to access the online platform i 've created. My idea is to ask them to give by login and registration their corresponding company's id. Of course i do not want to show the MySql business_id there for a match but a alphanumeric code, so if their company's id in the table is equal to 1 for example, then i transform this with sha1 and finally i call the first 6 characters with php's func substr(). Thats what the employee has to enter for our example, just 356a19 (because substr(sha1(1), 6) = 356a19). The matter is how am i going to select all from tbl business where a key from there if transformed like:

SUBSTR(SHA1(business_id, 0, 6))

gives a match with 356a19 (if business_id=1 etc)?

share|improve this question
I'm not sure what you're trying to accomplish here. An SHA1 hash isn't guaranteed to be unique, so it follows that a subset of a hash isn't guaranteed to be unique either. Why not simply allow the customer to choose an alphanumeric code that maps to their business_id if it's that important to conceal? For that matter, exactly why is it important to conceal the business_id? –  Bacon Bits Feb 6 '13 at 8:14
Agree with Bacon Bits, and more: assuming business_id is 1, 2, 3, 4, ..., then a simple hack would be to try the first 6 digits of SHA1(1), SHA1(2), SHA1(3),... The way to handle obscure IDs is to generate them obscurely in the first place -- even while you're INSERTing the customer's data. –  Shlomi Noach Feb 6 '13 at 8:30
If i apply sha1 hash to every business_id that is unique and has auto_increment then every one of them (after sha1(business_id)) will be unique. Func substr(sha1(business_id), 6) does exactly that. Brings the first 6 digits from the sha1 hashed busines_id. Thats what i try to match with the mysql tbl. So your proposal is apart from the primary key business_id to save that hashed in a separate field. If so i think is better to create a random number, extract 6 digits from it and save that to the tbl as business_code or sth like that. Maybe thats better then. –  marios Feb 6 '13 at 12:13
You are both correct and incorrect that SHA1 will give you a unique result when applied to AUTO_INCREMENT values. You are correct because for the first known values it is so. You are incorrect because SHA1 does not guarantee uniqueness. Indeed - it cannot: it always provides output of same length. Do the math of number of possible inputs (limited only by existing hardware) to the number of outputs (very limited). –  Shlomi Noach Feb 6 '13 at 12:29
Also consider you take only first 6 digits. I don't know that uniqueness will remain after that even for small integers. But the thing is, a hacker will very quickly realize his 6 digits stand fora prefix of SHA1 of a small number. –  Shlomi Noach Feb 6 '13 at 12:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.