nonerth
nonerth

Reputation: 569

Hash small strings into numbers only

My primary key 'is' a string - therefore I want to hash it so I can use the hashed number instead of a string..

I have in total 800 fixed strings (meaning very few and really really low security risk on hashing) so its very unlikely I'll encounter collisions with a simple hash function/method. I considered md5 but since its going to be the primary key in my sqlite table I want to avoid any strings in it. Do you have anything in mind?

Cheers

Upvotes: 0

Views: 1389

Answers (1)

Jason Livesay
Jason Livesay

Reputation: 6377

Normally you would use something like an auto-increment field but if you want a hash you could try this:

const crc32 = require("crc32")
const key = "lskdjfasoif";
const idNum = parseInt(crc32(key),16);

Upvotes: 2

Related Questions