{"id":120334,"date":"2020-11-06T17:15:08","date_gmt":"2020-11-06T17:15:08","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=16479"},"modified":"2020-11-06T17:15:08","modified_gmt":"2020-11-06T17:15:08","slug":"how-to-get-md5-of-a-string-a-python-one-liner","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/11\/06\/how-to-get-md5-of-a-string-a-python-one-liner\/","title":{"rendered":"How to Get MD5 of a String? A Python One-Liner"},"content":{"rendered":"<p><strong>Rapid Answer<\/strong>: The following one-liner calculates the MD5 from the string <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">'hello world'<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import hashlib as h;print(h.md5(b'hello world').hexdigest())<\/pre>\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" title=\"How to Get MD5 of a String? A Python One-Liner\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/yfRg1xlS4PY?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<p><strong>Background<\/strong>: <strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/MD5\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/en.wikipedia.org\/wiki\/MD5\">MD5 message-digest <\/a><\/strong>is a vulnerable cryptographic algorithm to map a string to a 128-bit hash value. You can use it as a checksum on a given text to ensure that the message hasn&#8217;t been corrupted. However, you shouldn&#8217;t use it as a protection against malicious corruption due to its vulnerability. With modern hardware and algorithms, it&#8217;s easy to crack!<\/p>\n<p><em><strong>Problem<\/strong>: How to generate an MD5 sum from a string?<\/em><\/p>\n<p>Example: Say, you have the following string text:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">text = 'hello world'<\/pre>\n<p>And you want to convert it to the MD5 hash value:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">5eb63bbbe01eeed093cb22bb8f5acdc3<\/pre>\n<p>We&#8217;ll discuss some methods to accomplish this next.<\/p>\n<h2>Method 1: hashlib.md5() &#8212; Multi-Liner<\/h2>\n<p>The hashlib library provides a function <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">md5()<\/code> that creates an object that can calculate the hash value of a given text for you via the method <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">update()<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Method 1: hashlib.md5()\nimport hashlib m = hashlib.md5()\ntext = 'hello world'\nm.update(text.encode('utf-8')) print(m.hexdigest())\n# 5eb63bbbe01eeed093cb22bb8f5acdc3<\/pre>\n<p>Make sure to encode the string as a Unicode string with the <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">string.encode('utf-8')<\/code> method. Otherwise, Python will throw an error. <\/p>\n<h2>Method 2: hashlib.md5() &#8212; Trivial One-Liner<\/h2>\n<p>As a <a href=\"https:\/\/blog.finxter.com\/python-one-line-x\/\" title=\"Python One Line X\" target=\"_blank\" rel=\"noreferrer noopener\">one-liner<\/a>, the code looks unreadable:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Method 2: One-Liner\nimport hashlib; m = hashlib.md5(); m.update(text.encode('utf-8'));print(m.hexdigest())\n# 5eb63bbbe01eeed093cb22bb8f5acdc3<\/pre>\n<p>We used the standard technique to one-linerize flat code snippets without indented code blocks. Learn more in our related tutorial.<\/p>\n<p><strong>Related Tutorial: <\/strong><a href=\"https:\/\/blog.finxter.com\/how-to-write-multiple-statements-on-a-single-line-in-python\/\" title=\"How to Write Multiple Statements on a Single Line in Python?\" target=\"_blank\" rel=\"noreferrer noopener\">How to One-Linerize Code?<\/a><\/p>\n<h2>Method 3: Improved One-Liner<\/h2>\n<p>You can slightly improve the code by using the <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">b'...'<\/code> string instead of the <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">encode()<\/code> function to make it a Unicode string:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Method 3: One-Liner\nimport hashlib as h;print(h.md5(b'hello world').hexdigest())\n# 5eb63bbbe01eeed093cb22bb8f5acdc3<\/pre>\n<p>I also initialized the <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">md5<\/code> object with the Unicode string directly rather than using the <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">update()<\/code> method. The one-liner now has minimum number of characters&#8212;I don&#8217;t think it can be made even more concise! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/13.0.0\/72x72\/1f609.png\" alt=\"\ud83d\ude09\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h2>Python One-Liners Book<\/h2>\n<p><strong>Python programmers will improve their computer science skills with these useful one-liners.<\/strong><\/p>\n<figure class=\"wp-block-image size-medium is-resized\"><a href=\"https:\/\/www.amazon.com\/gp\/product\/B07ZY7XMX8\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/3D_cover-1024x944.jpg\" alt=\"Python One-Liners\" class=\"wp-image-10007\" width=\"512\" height=\"472\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/3D_cover-scaled.jpg 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/3D_cover-300x277.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/3D_cover-768x708.jpg 768w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/a><\/figure>\n<p><a href=\"https:\/\/amzn.to\/2WAYeJE\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/amzn.to\/2WAYeJE\"><em>Python One-Liners<\/em> <\/a>will teach you how to read and write &#8220;one-liners&#8221;: concise statements of useful functionality packed into a single line of code. You&#8217;ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.<\/p>\n<p>The book&#8217;s five chapters cover tips and tricks, regular expressions, machine learning, core data science topics, and useful algorithms. Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You&#8217;ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments. You&#8217;ll also learn how to:<\/p>\n<p><strong>\u2022<\/strong>&nbsp;&nbsp;Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution<br \/><strong>\u2022<\/strong>&nbsp;&nbsp;Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics<br \/><strong>\u2022<\/strong>&nbsp;&nbsp;Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning<br \/><strong>\u2022<\/strong>&nbsp;&nbsp;Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy\/nongreedy operators<br \/><strong>\u2022<\/strong>&nbsp;&nbsp;Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting<\/p>\n<p>By the end of the book, you&#8217;ll know how to write Python at its most refined, and create concise, beautiful pieces of &#8220;Python art&#8221; in merely a single line.<\/p>\n<p><strong><a href=\"https:\/\/amzn.to\/2WAYeJE\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/amzn.to\/2WAYeJE\"><em>Get your Python One-Liners Now!!<\/em><\/a><\/strong><\/p>\n<p>The post <a href=\"https:\/\/blog.finxter.com\/how-to-get-md5-of-a-string-a-python-one-liner\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Get MD5 of a String? A Python One-Liner<\/a> first appeared on <a href=\"https:\/\/blog.finxter.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Finxter<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rapid Answer: The following one-liner calculates the MD5 from the string &#8216;hello world&#8217;: import hashlib as h;print(h.md5(b&#8217;hello world&#8217;).hexdigest()) Background: MD5 message-digest is a vulnerable cryptographic algorithm to map a string to a 128-bit hash value. You can use it as a checksum on a given text to ensure that the message hasn&#8217;t been corrupted. However, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-120334","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/120334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=120334"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/120334\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=120334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=120334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=120334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}