user2993328
user2993328

Reputation: 103

How to import users with a SHA1 password to Firebase?

I am currently trying to import passwords which were Hashed using the SHA1 npm package into Google Firebase using their import function.

The old system hashed passwords using the sha1 function once on a string. For example the below code is an example of how a hash is created on it.

const password = sha1("Abc12345");

The above example would produce the hash: dca0a5afd0b457ee36f8862369c7fda58c162b25

The previous system did not have a salt so I just omitted this completely. When I run my import script, it executes successfully but when I try to login with the credentials I get an API response stating INVALID_PASSWORD.

Here is my import script:

const { cert } = require("firebase-admin/app");
const admin = require("firebase-admin");
const csv = require("csvtojson");
const csvFilePath = "Users.csv";
const serviceAccount = require("./xxx");

async function run() {
  const items = await csv().fromFile(csvFilePath);
  const users = [];
  for (item of items) {
    console.log({
      uid: item.id,
      email: item.email,
      password: item.password,
      passwordHash: Buffer.from(item.password),
    });
    users.push({
      uid: item.id,
      email: item.email,
      passwordHash: Buffer.from(item.password),
    });
  }
  admin
    .initializeApp({
      credential: cert(serviceAccount),
      databaseURL:
        "xxx",
    })
    .auth()
    .importUsers(users, {
      hash: {
        algorithm: "SHA1",
        rounds: 80,
      },
    })
    .then((results) => {
      results.errors.forEach((indexedError) => {
        console.log(indexedError.error.message);
        console.log(`Error importing user ${indexedError.index}`);
      });
    })
    .catch((error) => {
      console.log("Error importing users :", error);
    });
}

run();

The console.log outputs:

{
  uid: '100',
  email: '[email protected]',
  password: 'dca0a5afd0b457ee36f8862369c7fda58c162b25',
  passwordHash: <Buffer 64 63 61 30 61 35 61 66 64 30 62 34 35 37 65 65 33 36 66 38 38 36 32 33 36 39 63 37 66 64 61 35 38 63 31 36 32 62 32 35>
}

Upvotes: 1

Views: 411

Answers (1)

user2993328
user2993328

Reputation: 103

I figured it out, I needed to change:

passwordHash: Buffer.from(item.password),

to

passwordHash: Buffer.from(item.password, "hex"),

Additionally, in my case I had to set rounds to 1

Upvotes: 2

Related Questions