Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

md5 to integer bits in python

Tags:

python

hash

I'm trying to convert an MD5 hashed value into a a bit integer in python. Does anyone have any idea how I would go about doing this?

I currently go through several ngrams applying a hash to each ngram:

for sentence in range(0,len(doc)):
        for i in range(len(doc[sentence]) - 4 + 1):
            ngram = doc[sentence][i:i + 4]
            hashWord = hashlib.md5()
            hashWord.update(ngram)

Thanks for any help.

like image 211
djcmm476 Avatar asked Nov 03 '12 21:11

djcmm476


People also ask

What is Hexdigest ()?

hexdigest() Like digest() except the digest is returned as a string object of double length, containing only hexadecimal digits. This may be used to exchange the value safely in email or other non-binary environments.

What is the MD5 function in python?

As a python programmer, we need hash functions to check the duplicity of data or files, to check data integrity when you transmit data over a public network, storing the password in a database etc. MD5 - MD5 or message digest algorithm will produce a 128-bit hash value.


1 Answers

If by "into bits", you mean a bit string for instance, then something like:

import hashlib

a = hashlib.md5('alsdkfjasldfjkasdlf')
b = a.hexdigest()
as_int = int(b, 16)
print bin(as_int)[2:]
# 11110000110010001100111010111001011010101011110001010000011010010010100111100
like image 198
Jon Clements Avatar answered Oct 20 '22 12:10

Jon Clements