Skip to content
Advertisement

How can I mimick OpenLDAP’s slappasswd using NodeJS?

My goal is to use NodeJS to create LDAP password hashes that are similar to what comes out of the slappasswd command-line tool.

Here’s how LDAP passwords can be produced with command-line:

JavaScript

The result is a base64 encoded, salted SHA1 password.

Here’s what I tried initially to recreate it:

JavaScript

But, I got a much longer string than what the slappasswd command produced and I’m not sure why.

JavaScript

I did some digging around on the net and found this on an LDAP password generator web page:

JavaScript

The web page produces a string that is the same length as what comes out of slappasswd, so I assume it’s an accurate recreation of the slappasswd logic.

Using this information, my next attempt looks like this:

JavaScript

However, I get errors.

First, there is TypeError: Cannot read properties of undefined (reading 'WordArray')

If I replace let salt = with let salt = btoa(0xA5) from my first attempt code, I then get the error: ReferenceError: sha1 is not defined

My feeling is that I’ve got the import wrong somehow.

I’m trying to do the ES6 module equivalent of var CryptoJS = require("crypto-js");, but failing somewhere.

So my question is two-fold:

  1. Can my first attempt be made to produce a string length similar to what slappassword outputs?
  2. If not, what can I do to fix the errors I’m getting in the second attempt?

Ideally, I’d like to understand where I went wrong in my first attempt rather than simply copying and pasting someone else’s code (second attempt) without fully grasping it.

Advertisement

Answer

Here is alternative of python/php implementations for NodeJS.

Import Crypto module

JavaScript

It will be used to create LDAP password hashes (SSHA)

JavaScript

It will be used to verify hash

JavaScript

Test it together

JavaScript

Hope it help you. Please let me know.

Try Now

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement