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?

Bhargav Modi
  • 2,605
  • 3
  • 29
  • 49
Anoop Asok
  • 1,311
  • 1
  • 8
  • 20
  • 1
    possible duplicate of [Create GUID / UUID in JavaScript?](http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript) – Thilo Dec 02 '14 at 11:10
  • 2
    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. – Anoop Asok Dec 02 '14 at 11:15
  • Please see my edited answer. Maybe it will help you. – Liglo App Dec 02 '14 at 12:34
  • thank u, anyway i got an answer somewhat similar to what i expect. cud u please hav a look at that. – Anoop Asok Dec 02 '14 at 12:37
  • 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) – Stijn de Witt Nov 23 '15 at 09:54

4 Answers4

12

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.

Community
  • 1
  • 1
Anoop Asok
  • 1,311
  • 1
  • 8
  • 20
  • 2
    How is this constant for each device? What if the a new plugin is installed? Or you connect a different monitor? – Thilo Dec 03 '14 at 00:50
  • 2
    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. – Anoop Asok Dec 03 '14 at 03:47
  • Have you considered creating a GUID and storing it somewhere (cookie or localStorage for example)? – Thilo Dec 03 '14 at 03:56
  • 1
    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 http://stackoverflow.com/questions/27148646/how-to-implement-cross-device-targeting – Anoop Asok Dec 03 '14 at 04:05
  • 1
    The ability to delete and re-create the tracking id is feature, not a bug. Might even be necessary for legal reasons. – Thilo Dec 03 '14 at 04:07
  • Ok...:-) Is there any any other way to get a unique id. Also can we track the MAC ID of a device? – Anoop Asok Dec 03 '14 at 04:10
  • userAgent is an ID for the browser, not the device. – Marc Aug 27 '18 at 16:24
  • 3
    If you run it on two different devices with the same model, os and browser then it returns the same UID for both! – Amir Mofakhar Mar 17 '19 at 20:59
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.

Community
  • 1
  • 1
Liglo App
  • 3,719
  • 4
  • 30
  • 54
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
Balaji
  • 9,657
  • 5
  • 47
  • 47
-6

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

var uniqueID = "ID-"+(new Date()).getTime().toString();
ArinCool
  • 1,720
  • 1
  • 13
  • 24