Reputation: 89
I'm reading the android docs specifically http://developer.android.com/reference/android/provider/Settings.Secure.html#ANDROID_ID .
I'd like to emulate this function to generate an android_id value in PHP for use in a little test script. It seems all values are 16 characters long and alphanumeric. I want my value to be perfectly valid rather than just generating an alphanumeric 16 character string. Any ideas would be appreciated.
Based on Tom's contribution below can someone confirm that the code,
bin2hex(openssl_random_pseudo_bytes(8));
would return a perfectly valid android_id
Upvotes: 3
Views: 6175
Reputation: 1307
with PHP 7 can be used:
$id = bin2hex(random_bytes(8));
or (PHP 4, PHP 5, PHP 7 < 7.2.0, PECL mcrypt >= 1.0.0)
$id = bin2hex(mcrypt_create_iv($length, MCRYPT_DEV_URANDOM));
an example of use:
function RandomToken($length = 32){
if(!isset($length) || intval($length) <= 8 ){
$length = 32;
}
if (function_exists('random_bytes')) {
return bin2hex(random_bytes($length));
}
if (function_exists('mcrypt_create_iv')) {
return bin2hex(mcrypt_create_iv($length, MCRYPT_DEV_URANDOM));
}
if (function_exists('openssl_random_pseudo_bytes')) {
return bin2hex(openssl_random_pseudo_bytes($length));
}
}
reference:
http://php.net/manual/en/function.random-bytes.php
http://php.net/manual/en/function.mcrypt-create-iv.php
Upvotes: 1
Reputation: 7091
This might now be exactly what you're going for, but I wanted to post this to SO because I haven't been able to find this anywhere else from a quick search. Below is the code from the SettingsProvider package that actually generates the ANDROID_ID for a device.
private boolean ensureAndroidIdIsSet() {
final Cursor c = query(Settings.Secure.CONTENT_URI,
new String[] { Settings.NameValueTable.VALUE },
Settings.NameValueTable.NAME + "=?",
new String[] { Settings.Secure.ANDROID_ID }, null);
try {
final String value = c.moveToNext() ? c.getString(0) : null;
if (value == null) {
final SecureRandom random = new SecureRandom();
final String newAndroidIdValue = Long.toHexString(random.nextLong());
Log.d(TAG, "Generated and saved new ANDROID_ID [" + newAndroidIdValue + "]");
final ContentValues values = new ContentValues();
values.put(Settings.NameValueTable.NAME, Settings.Secure.ANDROID_ID);
values.put(Settings.NameValueTable.VALUE, newAndroidIdValue);
final Uri uri = insert(Settings.Secure.CONTENT_URI, values);
if (uri == null) {
return false;
}
}
return true;
} finally {
c.close();
}
}
Given this function, it could easily be emulated in PHP as Jack and others have pointed out. You're really just creating a 64 bit random number and storing it as a hexadecimal string. There's any number of ways to accomplish this, so I'll leave that part up to the reader.
Upvotes: 2
Reputation: 173572
Do you mean something like this?
$id = bin2hex(openssl_random_pseudo_bytes(8));
It generates a hex representation of a 64 bit random number.
Upvotes: 6
Reputation: 424
A valid hex string contains just numbers and letters from A to F both inclusive. Example "014AEC092FA74D78".
Upvotes: 2