PYTHON hash

is this article helpful?
|
Python replacement for PHP's hash [ edit | history ]
#Python 2.7
import hashlib

algo = 'ripemd160'
data = 'The quick brown fox jumped over the lazy dog.'

h = hashlib.new(algo)
h.update(data)


When you want to get digest as hex string,
result = h.hexdigest()
# result will be '8eb208f7e05d987a9b044a8e98c6b087f15a0bfc'


or as raw binary,
result = h.digest()
# result will be '\x8e\xb2\x08\xf7\xe0]\x98z\x9b\x04J\x8e\x98\xc6\xb0\x87\xf1Z\x0b\xfc'

PHP hash

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

hash

(PHP 5 >= 5.1.2, PECL hash >= 1.1)

hashGenerate a hash value (message digest)

Description

string hash ( string $algo , string $data [, bool $raw_output= false ] )

Parameters

algo

Name of selected hashing algorithm (i.e. "md5", "sha256", "haval160,4", etc..)

data

Message to be hashed.

raw_output

When set to TRUE, outputs raw binary data. Default value (FALSE) outputs lowercase hexits.

Return Values

Returns a string containing the calculated message digest as lowercase hexits unless raw_output is set to true in which case the raw binary representation of the message digest is returned.

Examples

Example #1 A hash() example

<?php
echo hash('ripemd160''The quick brown fox jumped over the lazy dog.');
?>

The above example will output:

ec457d0a974c48d5685a7efa03d137dc8bbde7e3

See Also

  • hash_file() - Generate a hash value using the contents of a given file
  • hash_hmac() - Generate a keyed hash value using the HMAC method
  • hash_init() - Initialize an incremental hashing context
  • md5() - Calculate the md5 hash of a string
  • sha1() - Calculate the sha1 hash of a string