1

I am trying to calculate the private data object's hash using sha256 hashing algorithm. The requirement is to calculate the hash of private data object and match it with the hash stored on the ledger but both hashes do not match. I am using Hyperledger fabric v2.3.3

For example, the private data object is:

const data = {
  "assetType": "PART",
  "partNumber": "3415",
  "serialNumber": "312351234123"
}; 

Below is the code executed to save private data:

await stub.putPrivateData('collection_org1', `PVT-${id}`, Buffer.from(JSON.stringify(data)))

The hash calculated using crypto.createHash('sha256').update(JSON.stringify(dataString)).digest('hex')

prints the following hash:
ae2fda8277f53ccd0b9de91ccc2c289142e0030f423966f706686bc1b5fe2e6e

using shasum,

❯ echo -n "{\"assetType\":\"PART\",\"partNumber\":\"3415\",\"serialNumber\":\"312351234123\"}" | shasum -a 256
ae2fda8277f53ccd0b9de91ccc2c289142e0030f423966f706686bc1b5fe2e6e  -

but the hash on the ledger fetched using stub.getPrivateDataHash() returns:
559f3ed03c81003bdb67efb83c3a90b6aac4d0169c40aa74a68fbf43f7eeb699

If I query the private data of the key, it returns the exact above object. Am I doing something wrong here?

Akshay Sood
  • 6,366
  • 10
  • 36
  • 59

1 Answers1

0

I tested this on my system and I'm getting expected result. Both private data hash and the calculated hash is matching. Attaching the code snippet.

// Data JSON
const data = {
"assetType": "PART",
"partNumber": "3415",
"serialNumber": "312351234123"
}

// Store private data
async putData(ctx) {
    await ctx.stub.putPrivateData('_implicit_org_Org1MSP', "KEY123", Buffer.from(JSON.stringify(data)))
}

// Get Hash
async getDataHash(ctx) {
    let dataHash = crypto.createHash('sha256').update(Buffer.from(JSON.stringify(data))).digest('hex')
    let privateDataHashUint = await ctx.stub.getPrivateDataHash('_implicit_org_Org1MSP', "KEY123")
    let resp = { dataHash, privateDataHash: Buffer.from(privateDataHashUint).toString('hex') }
    return resp
}

Following response is received from getDataHash

{"dataHash":"ae2fda8277f53ccd0b9de91ccc2c289142e0030f423966f706686bc1b5fe2e6e","privateDataHash":"ae2fda8277f53ccd0b9de91ccc2c289142e0030f423966f706686bc1b5fe2e6e"}
Basil K Y
  • 490
  • 5
  • 8