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

Rapid Answer: The following one-liner calculates the MD5 from the string 'hello world':

import hashlib as h;print(h.md5(b'hello world').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’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!

Problem: How to generate an MD5 sum from a string?

Example: Say, you have the following string text:

text = 'hello world'

And you want to convert it to the MD5 hash value:

5eb63bbbe01eeed093cb22bb8f5acdc3

We’ll discuss some methods to accomplish this next.

Method 1: hashlib.md5() — Multi-Liner


The hashlib library provides a function md5() that creates an object that can calculate the hash value of a given text for you via the method update():

# Method 1: hashlib.md5()
import hashlib m = hashlib.md5()
text = 'hello world'
m.update(text.encode('utf-8')) print(m.hexdigest())
# 5eb63bbbe01eeed093cb22bb8f5acdc3

Make sure to encode the string as a Unicode string with the string.encode('utf-8') method. Otherwise, Python will throw an error.

Method 2: hashlib.md5() — Trivial One-Liner


As a one-liner, the code looks unreadable:

# Method 2: One-Liner
import hashlib; m = hashlib.md5(); m.update(text.encode('utf-8'));print(m.hexdigest())
# 5eb63bbbe01eeed093cb22bb8f5acdc3

We used the standard technique to one-linerize flat code snippets without indented code blocks. Learn more in our related tutorial.

Related Tutorial: How to One-Linerize Code?

Method 3: Improved One-Liner


You can slightly improve the code by using the b'...' string instead of the encode() function to make it a Unicode string:

# Method 3: One-Liner
import hashlib as h;print(h.md5(b'hello world').hexdigest())
# 5eb63bbbe01eeed093cb22bb8f5acdc3

I also initialized the md5 object with the Unicode string directly rather than using the update() method. The one-liner now has minimum number of characters—I don’t think it can be made even more concise! ?

Python One-Liners Book


Python programmers will improve their computer science skills with these useful one-liners.

Python One-Liners

Python One-Liners 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.

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:

  Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution
  Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics
  Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning
  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
  Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting

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.

Get your Python One-Liners Now!!

The post How to Get MD5 of a String? A Python One-Liner first appeared on Finxter.



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



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Show Don’t Tell: Commenting Python Best Practices xSicKxBot 0 3 Today, 03:41 AM
Last Post: xSicKxBot
  [Tut] How to Catch and Print Exception Messages in Python xSicKxBot 0 6 11-23-2020, 11:45 PM
Last Post: xSicKxBot
  [Tut] Python next() xSicKxBot 0 7 11-22-2020, 08:17 AM
Last Post: xSicKxBot
  [Tut] Python Math Domain Error (How to Fix This Stupid Bug) xSicKxBot 0 8 11-21-2020, 08:01 AM
Last Post: xSicKxBot
  [Tut] Python Int to String with Leading Zeros xSicKxBot 0 18 11-17-2020, 03:22 AM
Last Post: xSicKxBot
  [Tut] Python Function Call Inside List Comprehension xSicKxBot 0 23 11-16-2020, 02:42 AM
Last Post: xSicKxBot
  [Tut] Python TypeError: Object is Not Subscriptable (How to Fix This Stupid Bug) xSicKxBot 0 21 11-13-2020, 12:04 AM
Last Post: xSicKxBot
  [Tut] How to Generate Text Automatically With Python? A Guide to the DeepAI API xSicKxBot 0 31 11-10-2020, 06:23 AM
Last Post: xSicKxBot
  [Tut] Overriding in Python xSicKxBot 0 39 10-29-2020, 04:49 AM
Last Post: xSicKxBot
  [Tut] How to Install PIL/Pillow in Python? A Helpful Illustrated Guide xSicKxBot 0 27 10-27-2020, 12:00 AM
Last Post: xSicKxBot

Forum Jump:

Become a Patron!
[-]
Upcoming Events

[-]
Latest Threads
News - Pokémon Teases ‘Very Special’ 25t...
Last Post: xSicKxBot
Today 05:15 PM
» Replies: 0
» Views: 2
Xbox Wire - 7 Tips to Help You Run Your ...
Last Post: xSicKxBot
Today 05:15 PM
» Replies: 0
» Views: 2
News - Video: The world generation tech ...
Last Post: xSicKxBot
Today 05:15 PM
» Replies: 0
» Views: 3
News - Don’t Miss: A PC game for console...
Last Post: xSicKxBot
Today 05:15 PM
» Replies: 0
» Views: 3
News - Ghost Of Tsushima Combat Designer...
Last Post: xSicKxBot
Today 05:15 PM
» Replies: 0
» Views: 3
(Indie Deal) BloodRayne, BigBen, Dear Vi...
Last Post: xSicKxBot
Today 03:41 PM
» Replies: 0
» Views: 3
[1.06] BO4 Zombie Trainer (Call of duty ...
Last Post: Lucas_Beshtia
Today 01:51 PM
» Replies: 29
» Views: 1136
Mobile - Black Desert Mobile update reve...
Last Post: xSicKxBot
Today 10:15 AM
» Replies: 0
» Views: 3
News - Nintendo Download: 26th November ...
Last Post: xSicKxBot
Today 10:12 AM
» Replies: 0
» Views: 3
News - Nintendo Download: 26th November ...
Last Post: xSicKxBot
Today 10:12 AM
» Replies: 0
» Views: 3

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020