Royal
Royal

Reputation: 407

How to create a hash token which output the same hash when the input is the same?

I am really getting a headache of this, so hopefully someone can help me.

I want to create an hashToken to save and secure some data. It is not sensitive data, so the security level doesn't need to be that high.

The scenario is like this. I receive data on the backend, which is something like a token already: 9ECB531EC6AE02CB0274336690E76A0A39511429859537BE242147B65B1824DD.

The next input is a different token, but the third input can be the same token as above. So when I receive a hashtoken that I already received earlier, this needs to be the same encryptedText as the one that alreadt existst.

I tried to save it via the crypto module of nodejs, like this:

const iv = randomBytes(16);
const password = 'VerySecurePasswordHere';
const key = (await promisify(scrypt)(password, 'salt', 32)) as Buffer;
const cipher = createCipheriv(
      'aes-256-ctr',
       key,
       iv
    );

const tokenToEncrypt =
      '9ECB531EC6AE02CB0274336690E76A0A39511429859537BE242147B65B1824DD';

const encryptedText = Buffer.concat([
      cipher.update(tokenToEncrypt),
      cipher.final(),
]);

output of encrypted text = 
0e2e913119d2699be2f3c6e5c7da8f03258207f19f3aea85014c9ce648730bea9faddb0c7dad1f49f73c5ff86051c6758d3573d4c28c972b859f8e4290c835c4

So this basically works, but two things are not working for my scenario:

  1. The encrypted text is too long. I want the output to have the same length/style as the input tokenToEncrypt.

  2. I am using cloud instances to run the code, so whenever the container goes idle and boots up again, the output of the same tokenToEncrypt will be totally different. This needs to be the same encrypted text as the one that is already saved in the database.

Do you have any tips for this to push me in the right direction? I tried also bCrypt, but then I also got a very long hashToken, and not in the same style as the input token.

Upvotes: 0

Views: 1153

Answers (1)

tyazeez
tyazeez

Reputation: 335

Check out this SO answer to a similar question, I'm sure you'll find it very useful.

Upvotes: 1

Related Questions