Android一次性密码(OTP)用户注册/开立账户

时间:2014-03-14 08:59:10

标签: android one-time-password

我想在 android 应用程序中实现 OTP功能。 在此应用程序中,注册后用户将收到一次性密码密钥。在验证OTP 之后,用户将能够使用该OTP 成功注册/打开帐户。我需要做些什么呢?

3 个答案:

答案 0 :(得分:10)

我实现了一种非常简单的OTP方法..

  1. 活动生成一个随机的5位数字,并通过短信网关将其发送到手机号码。
  2. 收到短信后,由Broadcast Reciever读取短信正文,并将短信代码复制到OTP EditText。
  3. 如果活动生成的随机代码和通过短信发送的代码相同,那么用户应该获得进一步的访问权。

答案 1 :(得分:7)

答案 2 :(得分:2)

正如@Vipin所提到的,最好的方法就是实现自己:

首先,您必须生成一个4位数(或任何您想要的)PIN码,例如:

int range = 9;  // to generate a single number with this range, by default its 0..9
int length = 4; // by default length is 4

public int generateRandomNumber() {
    int randomNumber;

    SecureRandom secureRandom = new SecureRandom();
    String s = "";
    for (int i = 0; i < length; i++) {
        int number = secureRandom.nextInt(range);
        if (number == 0 && i == 0) { // to prevent the Zero to be the first number as then it will reduce the length of generated pin to three or even more if the second or third number came as zeros
            i = -1;
            continue;
        }
        s = s + number;
    }

    randomNumber = Integer.parseInt(s);

    return randomNumber;
}

然后,您必须将此号码保存在某处保存,例如在您的偏好设置中:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("OTP_PIN", randomNumber);
editor.commit();

下一步,将使用正确的短信网关将该OTP发送到相应的电话号码,对我来说,我使用clickATell与我们的php服务器发送消息,api documentation非常清楚。如果您想直接从应用程序发送消息,SMSgateway可以提供帮助。

最后一步,是验证SMS收到的代码是存储在设备首选项中的代码,这非常简单直接,您所要做的只是提供{{1对于允许他输入手机接收的代码的用户,如果代码与设备首选项中保存的OTP匹配,则让他通过应用程序,否则,显示正确的错误消息。

一个优雅的举动: 不是强制性的,但最好是,因为很多应用程序可以提供短信听众来收听即将发送的消息,从收到的消息中获取代码,在代码验证中显示EditText,验证它,如果是,则去通过应用程序。

manifest.xml

中的

editText
听众:

<receiver
    android:name=".Services.SmsListener"
    android:exported="true"
    android:permission="android.permission.BROADCAST_SMS">
    <intent-filter android:priority="999">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

接收者:

public class SmsListener extends BroadcastReceiver {

    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("messageBody", intent.getAction());
        if (intent.getAction().equals(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)) {
            try {
                String messageBody = "";
                for (SmsMessage smsMessage : Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
                    messageBody = smsMessage.getMessageBody();
                }
                Intent messageReceived = new Intent(SVPreferences.SMS_RECEIVED);
                messageReceived.putExtra("sms", messageBody);
                context.sendBroadcast(messageReceived); // when receiving it somewhere in your app, subString the additional text and leave only the code, then place it in the editText and do your verification
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}