最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Generate unique id for each device - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Dec 2, 2014 at 11:20 Bhargav Modi 2,6553 gold badges32 silver badges50 bronze badges asked Dec 2, 2014 at 11:03 Anoop AsokAnoop Asok 1,3371 gold badge8 silver badges20 bronze badges 5
  • 1 possible duplicate of Create GUID / UUID in JavaScript? – Thilo Commented Dec 2, 2014 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 Commented Dec 2, 2014 at 11:15
  • Please see my edited answer. Maybe it will help you. – Liglo App Commented Dec 2, 2014 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 Commented Dec 2, 2014 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 Commented Nov 23, 2015 at 9:54
Add a ment  | 

4 Answers 4

Reset to default 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.

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 ment 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/functions/md5/ This will return a unique value for given properties.

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

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

var uniqueID = "ID-"+(new Date()).getTime().toString();
发布评论

评论列表(0)

  1. 暂无评论