Node.js hash.digest() Method - GeeksforGeeks (2024)

Table of Contents
javascript javascript

Improve Article

Save Article

Like Article

_saurabh_jaiswalproficient65 published articles
  • Read
  • Discuss
  • Improve Article

    Save Article

    Like Article

    The hash.digest( ) method is an inbuilt function of the crypto module’s Hash class. This is used to create the digest of the data which is passed when creating the hash. For example, when we create a hash we first create an instance of Hash using crypto.createHash() and then we update the hash content using the update( ) function but till now we did not get the resulting hash value, So to get the hash value we use the digest function which is offered by the Hash class.

    This function takes a string as an input which defines the type of the returning value for example hex or base64. If you leave this field you will get Buffer as a result.

    Syntax:

    hash.digest([encoding])

    Parameter: This function takes the following one parameter:

    • encoding: This method takes an optional parameter that defines the type of returning output. You can use ‘hex’ or ‘base64’ as a parameter.

    Module Installation: Install the required module using the following command:

    npm install crypto

    Return Value: This function returns a String when the parameter is passed and returns a Buffer object when no parameter is passed. Suppose we passed parameter base64 then the return value will be a string of base64 encoding.

    Example 1: Generating hash values of the string GeeksForGeeks in the form of a hex and base64.

    Filename: index.js

    javascript

    // Importing the crypto library

    const crypto = require("crypto")

    // Defining the algorithm

    let algorithm = "sha256"

    // Defining the key

    let key = "GeeksForGeeks"

    // Creating the digest in hex encoding

    let digest1 = crypto.createHash(algorithm).update(key).digest("hex")

    // Creating the digest in base64 encoding

    let digest2 = crypto.createHash(algorithm).update(key).digest("base64")

    // Printing the digests

    console.log("In hex Encoding : \n " + digest1 + "\n")

    console.log("In base64 encoding: \n " + digest2)

    Run the index.js file using the following command:

    node index.js

    Output:

    In hex Encoding : 0112e476505aab51b05aeb2246c02a11df03e1187e886f7c55d4e9935c290adeIn base64 encoding: ARLkdlBaq1GwWusiRsAqEd8D4Rh+iG98VdTpk1wpCt4= 

    Example 2: Creating a digest by not passing the encoding key.

    Filename: index.js

    javascript

    // Importing the crypto library

    const crypto = require("crypto")

    // Defining the algorithm

    let algorithm = "sha256"

    // Defining the key

    let key = "GeeksForGeeks"

    // Creating the digest in hex encoding

    let digest = crypto.createHash(algorithm).update(key).digest()

    // Printing the digests

    console.log(digest)

    Run the index.js file using the following command:

    node index.js

    Output:

    <Buffer 01 12 e4 76 50 5a ab 51 b0 5a eb 22 46c0 2a 11 df 03 e1 18 7e 88 6f 7c 55 d4 e9 93 5c 29 0a de>

    Reference: https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding


    Last Updated :11 Apr, 2023

    Like Article

    Save Article

    Node.js hash.digest() Method - GeeksforGeeks (2024)
    Top Articles
    Latest Posts
    Article information

    Author: Clemencia Bogisich Ret

    Last Updated:

    Views: 5569

    Rating: 5 / 5 (60 voted)

    Reviews: 91% of readers found this page helpful

    Author information

    Name: Clemencia Bogisich Ret

    Birthday: 2001-07-17

    Address: Suite 794 53887 Geri Spring, West Cristentown, KY 54855

    Phone: +5934435460663

    Job: Central Hospitality Director

    Hobby: Yoga, Electronics, Rafting, Lockpicking, Inline skating, Puzzles, scrapbook

    Introduction: My name is Clemencia Bogisich Ret, I am a super, outstanding, graceful, friendly, vast, comfortable, agreeable person who loves writing and wants to share my knowledge and understanding with you.