Generate unique id for each device
Asked Answered
D

4

5

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?

Dehiscence answered 2/12, 2014 at 11:3 Comment(5)
possible duplicate of Create GUID / UUID in JavaScript?Bouffe
Please do not duplicate this as I want to generate unique id all the time. The question mentioned here generate unique id all the time, but the id generated will not be same all the time. I want same id all the time.Dehiscence
Please see my edited answer. Maybe it will help you.Johannessen
thank u, anyway i got an answer somewhat similar to what i expect. cud u please hav a look at that.Dehiscence
Summary of the question: I am using Open Source project fingerprintjs for generating fingerprints, but I want to do this without this library... How can it be done? How about reading the library... or better yet, just use it? Your attempt at doing this yourself will most likely not be as good (especially since you need to ask here how it's done)Outdate
D
13

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.

Dehiscence answered 2/12, 2014 at 12:35 Comment(8)
How is this constant for each device? What if the a new plugin is installed? Or you connect a different monitor?Bouffe
It will change. I know that this is not a perfect solution. In the case of 'Create GUID / UUID in JavaScript?', the id will change on each reload. But i do not want to change it in each reload. Therefore i asked this question. Solution perfect than this solution is really appreciable.Dehiscence
Have you considered creating a GUID and storing it somewhere (cookie or localStorage for example)?Bouffe
I am avoiding cookies as it can be flushed. Moreover this code is a part of cross browser tracking project in which we are implementing the deterministic method to track the device. For more information please look at #27149146Dehiscence
The ability to delete and re-create the tracking id is feature, not a bug. Might even be necessary for legal reasons.Bouffe
Ok...:-) Is there any any other way to get a unique id. Also can we track the MAC ID of a device?Dehiscence
userAgent is an ID for the browser, not the device.Skeg
If you run it on two different devices with the same model, os and browser then it returns the same UID for both!Soft
J
1

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.

Johannessen answered 2/12, 2014 at 11:6 Comment(0)
L
0

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
Loos answered 16/9, 2020 at 9:20 Comment(0)
C
-7

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

var uniqueID = "ID-"+(new Date()).getTime().toString();
Cyprian answered 2/12, 2014 at 11:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.