PYTHON md5

is this article helpful?
|
Python replacement for PHP's md5 [ edit | history ]
# Python 2.5+
import hashlib
hashlib.md5(b"The quick brown fox jumped over the lazy dog's back").hexdigest()

# pre-2.5, removed in Python 3
import md5
md5.md5("The quick brown fox jumped over the lazy dog's back").hexdigest()


Or a more compatible function:
import hashlib

def md5(s, raw_output=False):
    """Calculates the md5 hash of a given string"""
    res = hashlib.md5(s.encode())
    if raw_output:
        return res.digest()
    return res.hexdigest()

md5('hello world')

PHP md5

PHP original manual for md5 [ show | php.net ]

md5

(PHP 4, PHP 5)

md5Calculate the md5 hash of a string

Description

string md5 ( string $str [, bool $raw_output= false ] )

Calculates the MD5 hash of str using the » RSA Data Security, Inc. MD5 Message-Digest Algorithm, and returns that hash.

Parameters

str

The string.

raw_output

If the optional raw_output is set to TRUE, then the md5 digest is instead returned in raw binary format with a length of 16. Defaults to FALSE.

Return Values

Returns the hash as a 32-character hexadecimal number.

Changelog

Version Description
5.0.0 The raw_output parameter was added.

Examples

Example #1 A md5() example

<?php
$str 
'apple';

if (
md5($str) === '1f3870be274f6c49b3e31a0c6728957f') {
    echo 
"Would you like a green or red apple?";
    exit;
}
?>

See Also

  • md5_file() - Calculates the md5 hash of a given file
  • sha1_file() - Calculate the sha1 hash of a file
  • crc32() - Calculates the crc32 polynomial of a string
  • sha1() - Calculate the sha1 hash of a string
  • hash() - Generate a hash value (message digest)