i know this post is too old, but this is for people that`ll visit this page in the future:
what the user "Harsh Shah" said is wrong..
you SHOULD NOT generate a random number on the device itself!
this compromise the whole verification,
- you ask the user for phone number, send it to the server.
- as a response for that request, you generate a random number and save with the user number on there user records on db, and send SMS with that random number, the RESPONSE for the request should be SENT-OK, NOT-SENT.
- the device gets the SMS and send it back to the server to compare with the existed db record.
data exchange:
- phone sends('012345567')-> server respond('SENT-OK');
on background:
server: [generate number, sends it in SMS, save in db for user 01234567, eg: 123123]
[phone reads the SMS eg as '123123']
- phone sends('01234567','123123')->server responds('AUTH-OK');
on background:
server: [check db for record user 01234567, compare the random number generated in step 1 with the number the user sent).
if you generate the number in the user phone, any script-kiddie can hack your authentication by extracting this number from the memory/storage (so easy to do) and spoofing an sms containing it (super easy to do as well)...
you might think this is a rare case, but this is a major hole in the security, you can authenticate yourself as any existing user and steal data from them if you do it the way "Harsh Shah" said...