Anoop Asok
Anoop Asok

Reputation: 1337

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?

Upvotes: 5

Views: 22439

Answers (4)

Anoop Asok
Anoop Asok

Reputation: 1337

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.

Upvotes: 13

Balaji
Balaji

Reputation: 10917

for node users this package helps to generate unique id install:

npm i node-machine-id

code:

import {machineId, machineIdSync} from 'node-machine-id';
 
// Asyncronous call with async/await or Promise
 
async function getMachineId() {
    let id = await machineId();
    ...
}
 
machineId().then((id) => {
    ...
})
 
// Syncronous call
 
let id = machineIdSync()
// id = c24b0fe51856497eebb6a2bfcd120247aac0d6334d670bb92e09a00ce8169365
let id = machineIdSync({original: true})
// id = 98912984-c4e9-5ceb-8000-03882a0485e4

Upvotes: 0

Liglo App
Liglo App

Reputation: 3819

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.

Upvotes: 1

ArinCool
ArinCool

Reputation: 1738

You can use timestamp for a unique ID and append some static text to it:

var uniqueID = "ID-"+(new Date()).getTime().toString();

Upvotes: -7

Related Questions