Gets the phone id - returns MD5 hash from IMEI and WIFI MAC - Android Phone

Android examples for Phone:Phone ID

Description

Gets the phone id - returns MD5 hash from IMEI and WIFI MAC

Demo Code


//package com.book2s;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.util.Log;

public class Main {
    /**//from  w w  w .  ja  v a2  s.  co m
     * Gets the phone id - returns MD5 hash from IMEI and WIFI MAC
     *
     * @return the phone id
     */
    public static String getPhoneID(Context context) {

        // Get the phone IMEI
        TelephonyManager telephonyManager = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        String imei = telephonyManager.getDeviceId();
        Log.d("HELPER", "IMEI: " + imei);

        // Get the Wifi MAC
        WifiManager wifiMan = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInf = wifiMan.getConnectionInfo();
        String macAddr = wifiInf.getMacAddress();
        Log.d("HELPER", "MAC: " + macAddr);

        return md5(macAddr + imei);

    }

    public static String md5(String s) {
        try {
            // Create MD5 Hash
            MessageDigest digest = java.security.MessageDigest
                    .getInstance("MD5");
            digest.update(s.getBytes());
            byte messageDigest[] = digest.digest();

            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < messageDigest.length; i++)
                hexString.append(Integer
                        .toHexString(0xFF & messageDigest[i]));
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}

Related Tutorials