-3

I try to use PBKDF2 but each way I get a different result. Can you tell me what the problem is?

  1. Node.js
"use strict";

const pbkdf2 = require("pbkdf2");   // npm i pbkdf2
const sha1 = require("sha1");       // npm i sha1

const y = pbkdf2.pbkdf2Sync(
    "toto",
    "toto",
    1000,
    8 * 10,
    "sha512"
)
    .slice(0, 10)
    .toString("base64")
;

console.log("y =", y);  // X7AxWzPOJiXvfg==
  1. Online PBKDF2

https://8gwifi.org/pbkdf.jsp

xAeOzSOt1+xNmA==

  1. Postgres
CREATE EXTENSION IF NOT EXISTS "pgcrypto";

SELECT encode(
    PBKDF2(
        'toto',
        'toto',
        1000,
        10,
        'sha512'
    ),
    'base64'
);

-- ybQxw9hGrIkkXA==

Thank you in advance!

user3515941
  • 141
  • 1
  • 8
  • What is it you're trying to _do_? – AKX Apr 28 '23 at 11:55
  • I encrypt passwords in Node.js and also need to use `PBKDF2` in Postgres. It is important to get the same results for the same input, something I can't do with a simple test. – user3515941 Apr 28 '23 at 12:47
  • Use test vectors for testing, e.g. [here](https://github.com/brycx/Test-Vector-Generation/blob/master/PBKDF2/pbkdf2-hmac-sha2-test-vectors.md). Regarding the website: The salt must be entered Base64 encoded (then you get the same result as that of the NodeJS code). – Topaco Apr 28 '23 at 13:23

1 Answers1

0

Thank you for your answer AK X.

The link https://github.com/brycx/Test-Vector-Generation/blob/master/PBKDF2/pbkdf2-hmac-sha2-test-vectors.md was very helpful.

The function I was using in postgres was not good, this one seems to work: PBKDF2 function in PostgreSQL.

user3515941
  • 141
  • 1
  • 8
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 04 '23 at 00:54