{"id":25548,"date":"2024-03-16T05:11:04","date_gmt":"2024-03-16T05:11:04","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/the-functionality-and-usage-of-messagedigest\/"},"modified":"2024-03-22T06:26:26","modified_gmt":"2024-03-22T06:26:26","slug":"the-functionality-and-usage-of-messagedigest","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/the-functionality-and-usage-of-messagedigest\/","title":{"rendered":"The functionality and usage of MessageDigest"},"content":{"rendered":"<p>MessageDigest is a class in Java that offers an irreversible hashing algorithm used to calculate the digest (or hash value) of a given input data.<\/p>\n<p>The main functions of the MessageDigest class include:<\/p>\n<ol>\n<li>Various hash algorithms are available: MessageDigest class supports various hash algorithms such as MD5, SHA-1, and SHA-256. You can use the getInstance method to obtain a MessageDigest instance for a specific algorithm.<\/li>\n<li>Calculate the hash value: Input data can be provided to a MessageDigest instance by invoking the update method. Then, by calling the digest method, the hash value of the input data can be calculated. The digest method returns a byte array representing the computed hash value.<\/li>\n<li>Support for multiple input data: The update method can be called multiple times to input multiple chunks of data into the MessageDigest instance. When the digest method is finally called to calculate the hash value, all input data will be merged together for calculation.<\/li>\n<li>Reuse instance: MessageDigest instances do not automatically reset to their initial state after computing a hash value. The MessageDigest instance can be reset by calling the reset method for reuse.<\/li>\n<\/ol>\n<p>Here is a simple example code demonstrating how to use the MessageDigest class to calculate the SHA-256 hash value of an input string.<\/p>\n<pre class=\"post-pre\"><code>import java.security.MessageDigest;\r\nimport java.security.NoSuchAlgorithmException;\r\n\r\npublic class MessageDigestExample {\r\n    public static void main(String[] args) {\r\n        String input = \"Hello, World!\";\r\n        \r\n        try {\r\n            \/\/ \u83b7\u53d6 SHA-256 \u54c8\u5e0c\u7b97\u6cd5\u7684 MessageDigest \u5b9e\u4f8b\r\n            MessageDigest md = MessageDigest.getInstance(\"SHA-256\");\r\n            \r\n            \/\/ \u5c06\u8f93\u5165\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\uff0c\u5e76\u8f93\u5165\u5230 MessageDigest \u5b9e\u4f8b\u4e2d\r\n            md.update(input.getBytes());\r\n            \r\n            \/\/ \u8ba1\u7b97\u54c8\u5e0c\u503c\u5e76\u83b7\u53d6\u7ed3\u679c\r\n            byte[] digest = md.digest();\r\n            \r\n            \/\/ \u5c06\u5b57\u8282\u6570\u7ec4\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\r\n            StringBuilder sb = new StringBuilder();\r\n            for (byte b : digest) {\r\n                sb.append(String.format(\"%02x\", b));\r\n            }\r\n            String hashedInput = sb.toString();\r\n            \r\n            System.out.println(\"Input: \" + input);\r\n            System.out.println(\"Hashed Input: \" + hashedInput);\r\n        } catch (NoSuchAlgorithmException e) {\r\n            e.printStackTrace();\r\n        }\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>The code above retrieves an instance of the SHA-256 hash algorithm using the MessageDigest.getInstance method. It then converts the input string into a byte array and inputs it into the MessageDigest instance by calling the update method. Finally, it retrieves the computed hash value by calling the digest method, and converts it into a hexadecimal string for output.<\/p>\n<p>It should be noted that the MessageDigest class is not thread-safe, so if you need to use it in a multi-threaded environment, you should create a separate MessageDigest instance for each thread.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MessageDigest is a class in Java that offers an irreversible hashing algorithm used to calculate the digest (or hash value) of a given input data. The main functions of the MessageDigest class include: Various hash algorithms are available: MessageDigest class supports various hash algorithms such as MD5, SHA-1, and SHA-256. You can use the getInstance [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-25548","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>The functionality and usage of MessageDigest - Blog - Silicon Cloud<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/the-functionality-and-usage-of-messagedigest\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The functionality and usage of MessageDigest\" \/>\n<meta property=\"og:description\" content=\"MessageDigest is a class in Java that offers an irreversible hashing algorithm used to calculate the digest (or hash value) of a given input data. The main functions of the MessageDigest class include: Various hash algorithms are available: MessageDigest class supports various hash algorithms such as MD5, SHA-1, and SHA-256. You can use the getInstance [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/the-functionality-and-usage-of-messagedigest\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-16T05:11:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-22T06:26:26+00:00\" \/>\n<meta name=\"author\" content=\"Liam\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Liam\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/the-functionality-and-usage-of-messagedigest\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/the-functionality-and-usage-of-messagedigest\/\"},\"author\":{\"name\":\"Liam\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/23786905eb7b377f45ddb01c17da7671\"},\"headline\":\"The functionality and usage of MessageDigest\",\"datePublished\":\"2024-03-16T05:11:04+00:00\",\"dateModified\":\"2024-03-22T06:26:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/the-functionality-and-usage-of-messagedigest\/\"},\"wordCount\":305,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/the-functionality-and-usage-of-messagedigest\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/the-functionality-and-usage-of-messagedigest\/\",\"name\":\"The functionality and usage of MessageDigest - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-16T05:11:04+00:00\",\"dateModified\":\"2024-03-22T06:26:26+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/the-functionality-and-usage-of-messagedigest\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/the-functionality-and-usage-of-messagedigest\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/the-functionality-and-usage-of-messagedigest\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"The functionality and usage of MessageDigest\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/23786905eb7b377f45ddb01c17da7671\",\"name\":\"Liam\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g\",\"caption\":\"Liam\"},\"sameAs\":[\"http:\/\/Wilson\"],\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/liamwilson\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"The functionality and usage of MessageDigest - Blog - Silicon Cloud","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/the-functionality-and-usage-of-messagedigest\/","og_locale":"en_US","og_type":"article","og_title":"The functionality and usage of MessageDigest","og_description":"MessageDigest is a class in Java that offers an irreversible hashing algorithm used to calculate the digest (or hash value) of a given input data. The main functions of the MessageDigest class include: Various hash algorithms are available: MessageDigest class supports various hash algorithms such as MD5, SHA-1, and SHA-256. You can use the getInstance [&hellip;]","og_url":"https:\/\/www.silicloud.com\/blog\/the-functionality-and-usage-of-messagedigest\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-16T05:11:04+00:00","article_modified_time":"2024-03-22T06:26:26+00:00","author":"Liam","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Liam","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/the-functionality-and-usage-of-messagedigest\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/the-functionality-and-usage-of-messagedigest\/"},"author":{"name":"Liam","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/23786905eb7b377f45ddb01c17da7671"},"headline":"The functionality and usage of MessageDigest","datePublished":"2024-03-16T05:11:04+00:00","dateModified":"2024-03-22T06:26:26+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/the-functionality-and-usage-of-messagedigest\/"},"wordCount":305,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/the-functionality-and-usage-of-messagedigest\/","url":"https:\/\/www.silicloud.com\/blog\/the-functionality-and-usage-of-messagedigest\/","name":"The functionality and usage of MessageDigest - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-16T05:11:04+00:00","dateModified":"2024-03-22T06:26:26+00:00","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/the-functionality-and-usage-of-messagedigest\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/the-functionality-and-usage-of-messagedigest\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/the-functionality-and-usage-of-messagedigest\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"The functionality and usage of MessageDigest"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/23786905eb7b377f45ddb01c17da7671","name":"Liam","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g","caption":"Liam"},"sameAs":["http:\/\/Wilson"],"url":"https:\/\/www.silicloud.com\/blog\/author\/liamwilson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/25548","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=25548"}],"version-history":[{"count":1,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/25548\/revisions"}],"predecessor-version":[{"id":59659,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/25548\/revisions\/59659"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=25548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=25548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=25548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}