Search code examples
javascriptnode.jsnpmhashsha

how to create a sha256 from an object in javascript?


https://www.npmjs.com/package/object-hash

This package only supports SHA1.

Is there any other way that can work to create the SHA256 from an object?


Solution

  • Your statement "This package only supports SHA1" is wrong. The npm package object-hash supports multiple hash algorithms, depending on your environment.

    The code

    const hash = require('object-hash');
    
    const obj = {};
    
    console.log(hash(obj, { algorithm: 'sha256' }));
    

    works with Node.js 16.20.0. You can get a list of supported algorithms with:

    const crypto = require('crypto');
    
    console.log(crypto.getHashes());
    

    I'm using CJS because you've used it in your other questions. Of course, you can do the same with ESM.