Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: LGPL 

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.util.Base64;

public class Main {
    public static byte[] generateRequestToken(byte[] countryCode, byte[] phoneNumber)
            throws NoSuchAlgorithmException {

        String signature = "MIIDMjCCAvCgAwIBAgIETCU2pDALBgcqhkjOOAQDBQAwfDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFDASBgNVBAcTC1NhbnRhIENsYXJhMRYwFAYDVQQKEw1XaGF0c0FwcCBJbmMuMRQwEgYDVQQLEwtFbmdpbmVlcmluZzEUMBIGA1UEAxMLQnJpYW4gQWN0b24wHhcNMTAwNjI1MjMwNzE2WhcNNDQwMjE1MjMwNzE2WjB8MQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEUMBIGA1UEBxMLU2FudGEgQ2xhcmExFjAUBgNVBAoTDVdoYXRzQXBwIEluYy4xFDASBgNVBAsTC0VuZ2luZWVyaW5nMRQwEgYDVQQDEwtCcmlhbiBBY3RvbjCCAbgwggEsBgcqhkjOOAQBMIIBHwKBgQD9f1OBHXUSKVLfSpwu7OTn9hG3UjzvRADDHj+AtlEmaUVdQCJR+1k9jVj6v8X1ujD2y5tVbNeBO4AdNG/yZmC3a5lQpaSfn+gEexAiwk+7qdf+t8Yb+DtX58aophUPBPuD9tPFHsMCNVQTWhaRMvZ1864rYdcq7/IiAxmd0UgBxwIVAJdgUI8VIwvMspK5gqLrhAvwWBz1AoGBAPfhoIXWmz3ey7yrXDa4V7l5lK+7+jrqgvlXTAs9B4JnUVlXjrrUWU/mcQcQgYC0SRZxI+hMKBYTt88JMozIpuE8FnqLVHyNKOCjrh4rs6Z1kW6jfwv6ITVi8ftiegEkO8yk8b6oUZCJqIPf4VrlnwaSi2ZegHtVJWQBTDv+z0kqA4GFAAKBgQDRGYtLgWh7zyRtQainJfCpiaUbzjJuhMgo4fVWZIvXHaSHBU1t5w//S0lDK2hiqkj8KpMWGywVov9eZxZy37V26dEqr/c2m5qZ0E+ynSu7sqUD7kGx/zeIcGT0H+KAVgkGNQCo5Uc0koLRWYHNtYoIvt5R3X6YZylbPftF/8ayWTALBgcqhkjOOAQDBQADLwAwLAIUAKYCp0d6z4QQdyN74JDfQ2WCyi8CFDUM4CaNB+ceVXdKtOrNTQcc0e+t";
        String classesMd5 = "P3b9TfNFCkkzPoVzZnm+BA=="; // 2.11.395 [*]

        byte[] key2 = Base64.decode(
                "/UIGKU1FVQa+ATM2A0za7G2KI9S/CwPYjgAbc67v7ep42eO/WeTLx1lb1cHwxpsEgF4+PmYpLd2YpGUdX/A2JQitsHzDwgcdBpUf7psX1BU=",
                Base64.DEFAULT);

        byte[] data = concat(
                concat(Base64.decode(signature, Base64.DEFAULT), Base64.decode(classesMd5, Base64.DEFAULT)),
                phoneNumber);

        byte[] opad = new byte[64];
        byte[] ipad = new byte[64];
        for (int i = 0; i < opad.length; i++) {
            opad[i] = 0x5C;
            ipad[i] = 0x36;
        }

        for (int i = 0; i < 64; i++) {
            opad[i] = (byte) ((opad[i] & 0xFF) ^ (key2[i] & 0xFF));
            ipad[i] = (byte) ((ipad[i] & 0xFF) ^ (key2[i] & 0xFF));
        }

        data = concat(ipad, data);

        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(data);
        byte[] firstHash = md.digest();

        data = concat(opad, firstHash);
        md = MessageDigest.getInstance("SHA-1");
        md.update(data);
        byte[] secondHash = md.digest();

        return Base64.encode(secondHash, Base64.DEFAULT | Base64.NO_WRAP);
    }

    public static byte[] concat(byte[] b1, byte[] b2) {
        byte[] ret = new byte[b1.length + b2.length];
        System.arraycopy(b1, 0, ret, 0, b1.length);
        System.arraycopy(b2, 0, ret, b1.length, b2.length);
        return ret;
    }
}