0

How to simplely convert a string to SHA256 string in Node.js. And convert a SHA256 string to a normal string?

const shajs = require('sha.js');



console.log('starts');
const code = 'WEASDSAEWEWAEAWEAWEWA';
const normal = 'anne';
const encrypted = shajs('sha256')
    .update(normal)
    .digest('hex');
const unencrypted = shajs('sha256')
    .read(normal)
    .toString('hex');
console.log(normal);
console.log(encrypted);
console.log(unencrypted);
console.log('end');

Where should i put the HASH CODE?

shaochuancs
  • 15,342
  • 3
  • 54
  • 62
annety
  • 35
  • 2
  • 6
  • What do you mean by hash code? – Mureinik Jul 07 '19 at 20:53
  • 1
    Do you understand that SHA256 is a one-way hash? You can't convert it back to a normal string. Also, in node.js see https://nodejs.org/api/crypto.html#crypto_class_hash which is built-in. – jfriend00 Jul 07 '19 at 21:14
  • In the other language i used SHA256 i've create a HASH CODE to transform the passwords #define HASH_CODE = 'WEASDSAEWEWAEAWEAWEWA'; SHA256_PassHash(inputtext, HASH_CODE, password, 64); – annety Jul 07 '19 at 22:09

1 Answers1

1

SHA256 is a one-way hash function which means you can only convert a string into a hash value not its reverse. To check password, you need to rehash plain password and compare it with the one you already stored in database.

agtabesh
  • 548
  • 3
  • 15