Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Get MD5 of a String? A Python One-Liner

#1
How to Get MD5 of a String? A Python One-Liner

<div><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>
<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>
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe 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>
</div>
</figure>
<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’t been corrupted. However, you shouldn’t use it as a protection against malicious corruption due to its vulnerability. With modern hardware and algorithms, it’s easy to crack!</p>
<p><em><strong>Problem</strong>: How to generate an MD5 sum from a string?</em></p>
<p>Example: Say, you have the following string text:</p>
<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>
<p>And you want to convert it to the MD5 hash value:</p>
<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>
<p>We’ll discuss some methods to accomplish this next.</p>
<h2>Method 1: hashlib.md5() — Multi-Liner</h2>
<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>
<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()
import hashlib m = hashlib.md5()
text = 'hello world'
m.update(text.encode('utf-8')) print(m.hexdigest())
# 5eb63bbbe01eeed093cb22bb8f5acdc3</pre>
<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>
<h2>Method 2: hashlib.md5() — Trivial One-Liner</h2>
<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>
<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
import hashlib; m = hashlib.md5(); m.update(text.encode('utf-8'));print(m.hexdigest())
# 5eb63bbbe01eeed093cb22bb8f5acdc3</pre>
<p>We used the standard technique to one-linerize flat code snippets without indented code blocks. Learn more in our related tutorial.</p>
<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>
<h2>Method 3: Improved One-Liner</h2>
<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>
<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
import hashlib as h;print(h.md5(b'hello world').hexdigest())
# 5eb63bbbe01eeed093cb22bb8f5acdc3</pre>
<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—I don’t think it can be made even more concise! <img src="https://s.w.org/images/core/emoji/13.0.0/72x72/1f609.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h2>Python One-Liners Book</h2>
<p><strong>Python programmers will improve their computer science skills with these useful one-liners.</strong></p>
<figure class="wp-block-image size-medium is-resized"><a href="https://www.amazon.com/gp/product/B07ZY7XMX8" target="_blank" rel="noopener noreferrer"><img 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/uplo...00x277.jpg 300w, https://blog.finxter.com/wp-content/uplo...68x708.jpg 768w" sizes="(max-width: 512px) 100vw, 512px" /></a></figure>
<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 “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.</p>
<p>The book’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’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments. You’ll also learn how to:</p>
<p><strong>•</strong>&nbsp;&nbsp;Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution<br /><strong>•</strong>&nbsp;&nbsp;Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics<br /><strong>•</strong>&nbsp;&nbsp;Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning<br /><strong>•</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>•</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>
<p>By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.</p>
<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>
<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>
</div>


https://www.sickgaming.net/blog/2020/11/...one-liner/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016