1

I want to generate unique 32-word length hash string for each user in my application, based on their id(each user has a unique id). When I was searching about this I found a similar question in SO. How to generate random SHA1 hash to use as ID in node.js? Gabi Purcaru's answer suggests using crypto module's createHash with the current time and a random string. Instead of that random string, I could use user's unique ID, so that I can make sure the generated string will always be unique. But this always gives me 40 string length hash. But I want it 32 string length. How to use the same approach to generate 32-word length random string?

crypto.createHash('sha1').update(current_date + uid).digest('hex'); //This always gives 40-word length string

I also referred naomik's great answer for the same question. He recommends using crypto.randomBytes(). But how can we ensure that it can never generate same string again?

Community
  • 1
  • 1
RaR
  • 3,075
  • 3
  • 23
  • 48
  • So.. you have a unique id and you need another one.. what for? Why don't you simply hash that unique id that you already have? – Mjh Apr 13 '17 at 12:01
  • @Mjh That uid is of length 5. I want to have a unique 32-word length string. – RaR Apr 13 '17 at 12:03
  • @Mjh How can we make a 32-word string out of 5-word string? – RaR Apr 13 '17 at 12:04
  • You can **hash** that 5-word string. For example, `md5` would produce 32 characters (16 bytes, when displayed as hex number would yield 32 characters). There are many algorithms to choose from, but in all honesty - I see really no point in doing this whatsoever. Since you "want it" for some unclear reason - you can use various methods, from asking your database to generate a `UUID` to hashing random strings or existing info. – Mjh Apr 13 '17 at 12:04
  • Thank you @Mjh. I need to create 32-word unique length string for my application's need. I think I can use `md5`. Thanks again for the quick solution :) – RaR Apr 13 '17 at 12:09
  • No problem, but I don't think it really *is* a solution, it's more like a hack. Be warned, there COULD be problems. Good luck! – Mjh Apr 13 '17 at 12:10
  • you can never be totally sure that a hash of a certain value will not be identical to a hash of a different value later on. But you can be [pretty sure](http://preshing.com/20110504/hash-collision-probabilities/). – code_monk Apr 13 '17 at 12:11
  • This is kind of scary. Then what would be the best way to generate random unique string in node. I thought hashes would be the only solution – RaR Apr 13 '17 at 12:12
  • Thanks for the warning @code_monk. It broke my assumption. What would be the correct and efficient way of generating really unique random strings? – RaR Apr 13 '17 at 12:15
  • if you have no tolerance for collisions, your hash must be equal in size to your input. But if you look at the odds of collision, you will see that they are probably [small enough for your needs](http://www.miketaylor.org.uk/tech/law.html#gsc.tab=0) – code_monk Apr 14 '17 at 01:01

2 Answers2

2
crypto.createHash('sha1').update(current_date + uid).digest('hex').slice(0, 32);

will create what you need

ponury-kostek
  • 7,824
  • 4
  • 23
  • 31
  • This may give same string again, right? What if the uniqueness between two strings was on the last 8 characters? – RaR Apr 13 '17 at 12:05
0

Use md5

let hash = crypto.createHash('md5').update(current_date + uid).digest('hex');

code_monk
  • 9,451
  • 2
  • 42
  • 41