same uuid based on seed string in js
Asked Answered
G

4

15

I found this answer Is there any way to generate the same UUID based on the seed string in JDK or some library? but this is the java way.

I need to the same, but in javascript, i can use any npm module or any library or custom function.

You can use UUID this way to get always the same UUID for your input String:

String aString="JUST_A_TEST_STRING";
String result = UUID.nameUUIDFromBytes(aString.getBytes()).toString();

Gonadotropin answered 1/2, 2017 at 5:14 Comment(3)
i tried to find all the libraries avail at npmrc, but no one actually does the trick, I just want to create a hash out of the input string and covert that to uuid formatGonadotropin
what's wrong with UUID.fromBytes?Silvana
@Silvana runkit.com/587dbce018e01000132892ea/5891724b4e76630014221ea8 i tried, but isn't workingGonadotropin
P
29

Current accepted solution will only work in NodeJS environment per github page of uuid-1345:

Un-Features:

  • Does not work in the browser due to the use of NodeJS's crypto module.

If you are looking for a solution that will work in the browser, you should use a more popular uuid library.

const uuidv5 = require('uuid/v5');

// ... using a custom namespace
//
// Note: Custom namespaces should be a UUID string specific to your application!
// E.g. the one here was generated using this modules `uuid` CLI.
const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
uuidv5('Hello, World!', MY_NAMESPACE); // ⇨ '630eb68f-e0fa-5ecc-887a-7c7a62614681'

The UUID will stay consistent as long as you pass it the same namespace.

Hope that helps.

Petrology answered 26/11, 2018 at 11:37 Comment(1)
the uuid lib has over a 32millon weakly downloads so this is definitely a preferable solutionIndwell
T
10

Easiest way:

const getUuid = require('uuid-by-string')

const uuidHash = getUuid('Hello world!')
// d3486ae9-136e-5856-bc42-212385ea7970

https://www.npmjs.com/package/uuid-by-string

Tilney answered 13/11, 2021 at 21:13 Comment(0)
G
4

Finally this came to my rescue

var UUID = require('uuid-1345');

UUID.v5({
    namespace: UUID.namespace.url,
    name: "test"
});
Gonadotropin answered 1/2, 2017 at 6:31 Comment(3)
don't forget to accept your answer so question is shown as answered.Misfire
Note that the above won't work in the browser. From their github page: "Does not work in the browser due to the use of NodeJS's crypto module."Petrology
Is it not possible to do the same for v4? If not, why?Hanselka
E
0

Here is working example:

const crypto = require('crypto');

function generateUuidBySeed(seedString) {
    const hash = crypto.createHash('sha256').update(seedString).digest('hex');

    // UUID version 4 consists of 32 hexadecimal digits in the form:
    // 8-4-4-4-12 (total 36 characters including hyphens)
    const uuid = [
        hash.substring(0, 8),
        hash.substring(8, 4),
        '4' + hash.substring(12, 3), // Set the version to 4
        '8' + hash.substring(15, 3), // Set the variant to 8 (RFC 4122)
        hash.substring(18, 12),
    ].join('-');

    return uuid;
}

// Example usage:
const seedString = 'your-seed-string';
const uuid = generateUuidBySeed(seedString);
console.log(uuid, '===>', '4400db7b-baad-4821-890a-93f9d14c358f');
Eri answered 9/2, 2024 at 12:25 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.