{"id":3878,"date":"2022-12-19T11:41:19","date_gmt":"2022-12-19T11:41:19","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=3878"},"modified":"2025-01-16T08:26:39","modified_gmt":"2025-01-16T08:26:39","slug":"what-is-the-crypto-createhash-method","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/what-is-the-crypto-createhash-method\/","title":{"rendered":"Comprehensive Guide to Node.js crypto.createHash() Method and Examples"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_56_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/metaschool.so\/articles\/what-is-the-crypto-createhash-method\/#How_to_use_the_createHash_function\" title=\"How to use the createHash() function?\">How to use the createHash() function?<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>In Node.js, the \u2063<code>crypto.createHash()<\/code> method is a built-in method in the <code>crypto<\/code> module that creates and returns a hash object that can be used to generate a hash of data using a specific hashing algorithm.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-use-createhash-to-generate-a-hash-of-some-data-using-the-sha256-algorithm\"><span class=\"ez-toc-section\" id=\"How_to_use_the_createHash_function\"><\/span>How to use the createHash() function?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To use this method, we first import the <code>crypto<\/code> module and then create a Hash instance where we specify which hashing algorithm we want to use. Some of the common algorithms supported by this method include\u2063 <code>md5<\/code>, <code>sha1<\/code>, <code><a href=\"https:\/\/emn178.github.io\/online-tools\/sha256.html\" target=\"_blank\" rel=\"noreferrer noopener\">sha256<\/a><\/code>, <code>sha512<\/code>. We can also update the data to generate a new hash digest (aka the output produced by the hash function).<\/p>\n\n\n\n<p>Let&#8217;s understand how the createHash() method works using an example.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"const crypto = require('crypto');\n\nconst data = 'metaschool is cool';\nconst hash = crypto.createHash('sha256');\n\nhash.update(data);\n\nconst digest = hash.digest('hex');\n\nconsole.log(digest); \" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">const<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4FC1FF\">crypto<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #DCDCAA\">require<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&#39;crypto&#39;<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">const<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4FC1FF\">data<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #CE9178\">&#39;metaschool is cool&#39;<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">const<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4FC1FF\">hash<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #9CDCFE\">crypto<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">createHash<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&#39;sha256&#39;<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #9CDCFE\">hash<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">update<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">data<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">const<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4FC1FF\">digest<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #9CDCFE\">hash<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">digest<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&#39;hex&#39;<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #9CDCFE\">console<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">log<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">digest<\/span><span style=\"color: #D4D4D4\">); <\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>crypto<\/code> module provides cryptographic functionalities in Node.js.<\/li>\n\n\n\n<li><code>crypto.createHash('sha256')<\/code> creates a <code>Hash<\/code> instance configured to use the SHA-256 algorithm.<\/li>\n\n\n\n<li><code>update()<\/code> method feeds data into the hash object. This method can be called multiple times if the data is streamed or in chunks.<\/li>\n\n\n\n<li><code>digest('hex')<\/code> method calculates the hash of the data provided to the <code>update()<\/code> method and returns it in the specified encoding format, in this case, hexadecimal.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">The More You Know<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Choose a hashing algorithm that meets your security requirements. For instance, SHA-256 is generally more secure than MD5 and SHA-512 is more secure than SHA-256.<\/li>\n\n\n\n<li>The <code>update()<\/code> method accepts strings, <code>Buffer<\/code>, <code>TypedArray<\/code>, or <code>DataView<\/code> objects. When providing a string, you can specify the encoding (e.g., <code>'utf8'<\/code>).<\/li>\n\n\n\n<li>Once <code>digest()<\/code> is called, the <code>Hash<\/code> object cannot be used again. If you want to hash more data, you need to create a new <code>Hash<\/code> instance.<\/li>\n\n\n\n<li>Hash algorithms are designed to be one-way functions, which means that it is computationally impossible to generate the original data from the hash. <\/li>\n\n\n\n<li>A hash collision is when two different pieces of input data produce the same hash digest. Although these are extremely rare for most hash algorithms, hash collisions can still happen.<\/li>\n<\/ul>\n\n\n\n<p><strong>Related Reading:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/metaschool.so\/articles\/hashing-in-blockchain\">Complete Guide to Hashing in Blockchain for 2023<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/metaschool.so\/articles\/immutable-blockchain\">Immutable<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/metaschool.so\/articles\/what-is-nonce-in-blockchain\">What is Nonce in Blockchain &amp; How is it Used?<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/metaschool.so\/articles\/how-to-generate-a-random-number-in-solidity\">How to Generate a Random Number in Solidity? \u2014 A Comprehensive Guide<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":11591,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[292],"tags":[294],"class_list":["post-3878","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-answers","tag-programming-concepts"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/3878","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=3878"}],"version-history":[{"count":13,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/3878\/revisions"}],"predecessor-version":[{"id":11894,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/3878\/revisions\/11894"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/11591"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=3878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=3878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=3878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}