{"id":1307,"date":"2021-02-01T09:20:50","date_gmt":"2021-02-01T09:20:50","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=1307"},"modified":"2021-02-01T09:20:52","modified_gmt":"2021-02-01T09:20:52","slug":"golang-hash","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2021\/02\/01\/golang-hash\/","title":{"rendered":"GoLang: Hash"},"content":{"rendered":"<p>Previously I talked about files, encoding in <code>golang<\/code>. In this LTD (learn to do) I will talk about <code>hash<\/code> package in general.<\/p>\n<p><blockquote class=\"wp-embedded-content\" data-secret=\"nmsuLb0GYG\"><a href=\"https:\/\/blog.samarthya.me\/wps\/2021\/01\/30\/golang-encoding\/\">GoLang: encoding<\/a><\/blockquote><iframe class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;GoLang: encoding&#8221; &#8212; Samarthya\" src=\"https:\/\/blog.samarthya.me\/wps\/2021\/01\/30\/golang-encoding\/embed\/#?secret=94ChAjvYxV#?secret=nmsuLb0GYG\" data-secret=\"nmsuLb0GYG\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n\n\n<h2 class=\"wp-block-heading\">Help<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/golang.org\/pkg\/hash\/#Hash\">https:\/\/golang.org\/pkg\/hash\/#Hash<\/a><\/li><li><a href=\"https:\/\/golang.org\/pkg\/crypto\/#Hash.New\">https:\/\/golang.org\/pkg\/crypto\/#Hash.New<\/a><\/li><li><a href=\"https:\/\/golang.org\/src\/crypto\/crypto.go?s=364:378#L6\">https:\/\/golang.org\/src\/crypto\/crypto.go?s=364:378#L6<\/a><\/li><li><a href=\"https:\/\/golang.org\/pkg\/crypto\/#pkg-subdirectories\">https:\/\/golang.org\/pkg\/crypto\/#pkg-subdirectories<\/a><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-pullquote has-background has-luminous-vivid-orange-background-color is-style-solid-color\"><blockquote class=\"has-text-color has-white-color\"><p>Package crypto collects common cryptographic constants.<\/p><cite>Official golang documentation<\/cite><\/blockquote><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>const (\n    MD4         Hash = 1 + iota \/\/ import golang.org\/x\/crypto\/md4\n    MD5                         \/\/ import crypto\/md5\n    SHA1                        \/\/ import crypto\/sha1\n    SHA224                      \/\/ import crypto\/sha256\n    SHA256                      \/\/ import crypto\/sha256\n    SHA384                      \/\/ import crypto\/sha512\n    SHA512                      \/\/ import crypto\/sha512\n    MD5SHA1                     \/\/ no implementation; MD5+SHA1 used for TLS RSA\n    RIPEMD160                   \/\/ import golang.org\/x\/crypto\/ripemd160\n    SHA3_224                    \/\/ import golang.org\/x\/crypto\/sha3\n    SHA3_256                    \/\/ import golang.org\/x\/crypto\/sha3\n    SHA3_384                    \/\/ import golang.org\/x\/crypto\/sha3\n    SHA3_512                    \/\/ import golang.org\/x\/crypto\/sha3\n    SHA512_224                  \/\/ import crypto\/sha512\n    SHA512_256                  \/\/ import crypto\/sha512\n    BLAKE2s_256                 \/\/ import golang.org\/x\/crypto\/blake2s\n    BLAKE2b_256                 \/\/ import golang.org\/x\/crypto\/blake2b\n    BLAKE2b_384                 \/\/ import golang.org\/x\/crypto\/blake2b\n    BLAKE2b_512                 \/\/ import golang.org\/x\/crypto\/blake2b\n\n)<\/code><\/pre>\n\n\n\n<p>Let&#8217;s try out a simple MD5 hasher.<\/p>\n\n\n\n<figure class=\"wp-block-pullquote has-background has-luminous-vivid-orange-background-color is-style-solid-color\"><blockquote class=\"has-text-color has-white-color\"><p>Package md5 implements the MD5 hash algorithm as defined in RFC 1321<\/p><cite>package <code>md5<\/code><\/cite><\/blockquote><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>fmt.Printf(\" Is BLAKE2b_256 available: %t\\n\", crypto.BLAKE2b_256.Available())\nfmt.Printf(\" Is MD5 available: %t\\n\", crypto.MD5.Available())\n\nvar hasher hash.Hash\n\/\/ Creates a new MD5 hasher\nhasher = md5.New()\n\nio.WriteString(hasher, \"And Leon's getting laaarger!\")\nfmt.Printf(\"%x\\n\", hasher.Sum(nil))<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">New()<\/h2>\n\n\n\n<p>New returns a new hash.Hash computing the MD5 checksum.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sum()<\/h2>\n\n\n\n<p>Sum returns the MD5 checksum of the data.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fmt.Printf(\" Is MD5 available: %t\\n\", crypto.MD5.Available())\n\tfmt.Printf(\" Is SHA1 available: %t\\n\", crypto.SHA1.Available())\n\tfmt.Printf(\" Is SHA256 available: %t\\n\", crypto.SHA256.Available())\nvar hasher hash.Hash\n\n\/\/ Creates a new MD5 hasher\nhasher = md5.New()\n\n\/\/ Version 1\nio.WriteString(hasher, \"And Leon's getting laaarger!\")\nfmt.Printf(\"%x\\n\", hasher.Sum(nil))\nfmt.Printf(\"Size: %d\\n\", hasher.Size())\n\n\/\/ Version 2\nvar data = &#91;]byte(\"Data for which the md5 hash will be computed\")\n\nfmt.Printf(\"%x\\n\", hasher.Sum(data))\nfmt.Printf(\"Size: %d\\n\", hasher.Size())\n\nsha1 := sha1.New()\nfmt.Printf(\"%x\\n\", sha1.Sum(nil))\nfmt.Printf(\"Size: %d\\n\", sha1.Size())\n\nsha256 := sha256.New()\nfmt.Printf(\"%x\\n\", sha256.Sum(nil))\nfmt.Printf(\"Size: %d\\n\", sha256.Size())<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Previously I talked about files, encoding in golang. In this LTD (learn to do) I will talk about hash package in general. GoLang: encoding \u00a0 \u00a0 Help https:\/\/golang.org\/pkg\/hash\/#Hash https:\/\/golang.org\/pkg\/crypto\/#Hash.New https:\/\/golang.org\/src\/crypto\/crypto.go?s=364:378#L6 https:\/\/golang.org\/pkg\/crypto\/#pkg-subdirectories Package crypto collects common cryptographic constants. Official golang documentation Let&#8217;s try out a simple MD5 hasher. Package md5 implements the MD5 hash algorithm as [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1309,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[137,34],"tags":[155,23,156],"class_list":["post-1307","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-golang","category-technical","tag-crypto","tag-golang","tag-hash","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=1307"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1307\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/1309"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=1307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=1307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=1307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}