2011-08-29 81 views
2

我的應用使用設備的電話號碼來獲取有關用戶聯繫人的有意義的信息。 它運行在Android和iOS上。獲取非手機iDevices和Android平板電腦的唯一ID

在手機上,我只是使用10位(無國家代碼)電話號碼作爲用戶的唯一標識符。這甚至可以在Android模擬器(它有自己的不存在的編號)上工作,儘管iPhone模擬器返回一個空白數字(我可以輕易忽略這種情況)。

現在我開始在非手機設備上進行測試。儘管他們仍有聯繫人,但他們不再有電話號碼。一個簡單的方法是在iOS中使用UDID,以及Android中的任何等效內容。但是,我有兩個問題需要解決:

  1. UDID不統一。我需要一個10個字符的鍵。有沒有辦法將n個字符散列成10個字符,避免碰撞? (我可以忍受非常低的機率碰撞)

  2. 即使是一個更大的問題:我剛剛讀了Apple is blocking UDID access as of iOS 5。我又用什麼來替代保留一個10個字符的密鑰呢?

謝謝你的時間。

回答

2

你可以使用你希望的任何散列方法;如果您要求結果只有10個字符,您可以輕鬆地將較大的哈希值減少到10個字符 - 您可以將結果剪裁爲10個字符,您可以將XOR 11到20與第1到第10個進行XOR(騎自行車,如果您的散列有超過20個字符)等等。

Android的更大挑戰是要找到一個唯一的設備ID - 有一個Google的機制,但它依賴於OEM合規性,並且合規性在所有設備上不統一。儘管如此,Is there a unique Android device ID?包含了一些很好的信息。

+0

感謝您的想法。結束與MAC地址建議,但Android鏈接肯定幫助。 –

0

看看這可能會有所幫助。這是來自Localytics開源SDK的

/** 
* Gets a 1-way hashed value of the device's unique ID. This value is 
* encoded using a SHA-256 one way hash and cannot be used to determine what 
* device this data came from. 
* 
* @param appContext 
*   The context used to access the settings resolver 
* @return An 1-way hashed identifier unique to this device or null if an 
*   ID, or the hashing algorithm is not available. 
*/ 
public static String getGlobalDeviceId(final Context appContext) { 
    String systemId = System.getString(appContext.getContentResolver(), 
      System.ANDROID_ID); 
    if (systemId == null) { 
     return null; 
    } 

    try { 
     MessageDigest md = MessageDigest.getInstance("SHA-256"); 
     byte[] digest = md.digest(systemId.getBytes()); 
     BigInteger hashedNumber = new BigInteger(1, digest); 
     return new String(hashedNumber.toString(16)); 

    } catch (NoSuchAlgorithmException e) { 
     return null; 
    } 
}