Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

same uuid based on seed string in js

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();

like image 251
Abhijeet Avatar asked Feb 01 '17 05:02

Abhijeet


1 Answers

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.

like image 171
user2961443 Avatar answered Sep 19 '22 17:09

user2961443