Search code examples
fingerprintjs2

how to add username on fingerprint2 hash


i've to edit a web application already existing that i don't know at all. into a web page this code create an unique number:

Fingerprint2.get(function (components) {
        try {
            var values = components.map(function (component) { return component.value });
            var murmur = Fingerprint2.x64hash128(values.join(''), 31);
            $("#id-fingerprint").val(murmur);
        } catch (err) {

        }
    });

our customers are working on cloned pc, same hardware and software, so it happens two different users generate the same unique number. my purpose is to add username to existing code.

is this a good way to do it?

Fingerprint2.get(function (components) {
        try {
            var values = components.map(function (component) { return component.value });
            var username = $("#username").val();
            values.push(username);
            var murmur = Fingerprint2.x64hash128(values.join(''), 31);
            $("#id-fingerprint").val(murmur);
        } catch (err) {

        }
    });

Solution

  • You can add a custom component, you can learn more in the Extending FingerprintJS guidelines. Simply put, you just need to add your id into the components object.

    const result = await fp.get()
    const components = {
      ...result.components,
      foo: username
    }
    
    const visitorId = FingerprintJS.hashComponents(components)