Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate unique id for each device

I want to generate a unique id for each device. Currently I am using fingerprint.js for this. My code is:

var fingerprint = new Fingerprint().get();

But I want to generate unique id with out using any plugins. Can any one help me please?

like image 584
Anoop Asok Avatar asked Dec 02 '14 11:12

Anoop Asok


2 Answers

Friends,

At last I found the answer. This code will generate unique id for each device(in a browser) all the time. But this Id will also generate a new id if the application is opened in different browser but in same device. uid is the generated unique id.

var navigator_info = window.navigator;
var screen_info = window.screen;
var uid = navigator_info.mimeTypes.length;
uid += navigator_info.userAgent.replace(/\D+/g, '');
uid += navigator_info.plugins.length;
uid += screen_info.height || '';
uid += screen_info.width || '';
uid += screen_info.pixelDepth || '';
console.log(uid);

Thank you all for supporting me.

like image 90
Anoop Asok Avatar answered Oct 23 '22 16:10

Anoop Asok


For example like this:

function generateUUID(){
    var d = new Date().getTime();
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = (d + Math.random()*16)%16 | 0;
        d = Math.floor(d/16);
        return (c=='x' ? r : (r&0x3|0x8)).toString(16);
    });
    return uuid;
};

More on the topic: Create GUID / UUID in JavaScript?

Edit: In your comment you say, you want to generate the same id per device at any time. For such tasks, building hashes is a way to go. Get any property / properties of your device, which are unique for this device (whatever it is, it is difficult to say without example). Than build a hash out of them, for example:

var uniqueId = someHashFunction(device.property1 + device.property2 + ...);

There are plenty of hashing functions on the internet, as an example you can have a look at this one: http://phpjs.org/functions/md5/ This will return a unique value for given properties.

like image 26
Liglo App Avatar answered Oct 23 '22 16:10

Liglo App