imi kim
imi kim

Reputation: 375

Firestore rules hashing returns identity

In my rule, I want to ensure that the data.id field is the hash of the doc id. The following is not working.

function hashGood(a, b) {
  let x = hashing.sha256(a);
  return x == b;
}

match /users/{userId} {
  allow create: if request.auth != null && hashGood(userId, request.resource.data.id);
}

I have also tried hashing.sha256(a.toUtf8()).toBase64() and variations thereof.

When I try to create /users/B in the 'Rules Playground', it shows x == "B" . But x should be the hash of "B", not equal to "B".

What I am doing wrong?

Edit: I am using rules_version = '2';

Upvotes: 1

Views: 288

Answers (1)

imi kim
imi kim

Reputation: 375

hashing.sha256(a).toHexString() worked for me.

Though I would like to still understand why let x = hashing.sha256(a) prints x=<value of a>

Upvotes: 3

Related Questions