I am building one application,in which I want to send unique code or referral code to each user.So I want to generate random strings from user mobile number.Each and every random string should be unique for each mobile number.How should I do that ? Is there any library for javascript or any good links for that ?
Asked
Active
Viewed 650 times
-2
-
1You can just MD5 encode the phone numbers. See: http://stackoverflow.com/questions/415953/how-can-i-generate-an-md5-hash – Bob Brinks Jun 21 '16 at 10:02
-
you can write your own logic for that, there is no specific answer for your question. – Pranav Totla Jun 21 '16 at 10:03
-
So to make it clear: You want to create a unique string from a phone number that 1) is unique amongst all of those strings and 2) will be the same if recreated for the same phone number? – devnull69 Jun 21 '16 at 10:03
-
@devnull69 yes u are right . – Udit Kumawat Jun 21 '16 at 10:04
-
MD5 is not unique, it is just relatively unlikely that you create double MD5 hashes for two different phone numbers. But maybe that's already enough for the use case – devnull69 Jun 21 '16 at 10:05
-
I think MD5 is not the correct solution .... I want more robust way to perform this – Udit Kumawat Jun 21 '16 at 10:08
-
Use encryption... with a once randomly generated key. – le_m Jun 21 '16 at 10:08
-
is there any popular hash method in javascript to generate random string from your input string ? – Udit Kumawat Jun 21 '16 at 10:12
1 Answers
0
In Node Js there are lots of packages available. You can also just use raw code like.
String.prototype.hashCode = function() {
var hash = 0, i, chr, len;
if (this.length === 0) return hash;
for (i = 0, len = this.length; i < len; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
This rudimentary hashing function will output a hash.

Laurence Stevens
- 1
- 1