Python Forum
Code that generates MD5 hashes from IPv6 addresses giving differant answers?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code that generates MD5 hashes from IPv6 addresses giving differant answers?
#1
I have some code that generates MD5 hashes from IPv6 addresses, then checks them against a list of known MD5 hashes. In trying to speed it up, I profiled it, and found the string conversion was chewing up a lot of CPU time. One must convert IPv6 to string to bytes, then feed that to _hashlib.

So, I attempted to speed it up. Here's some code documenting my attempt:
from _hashlib import openssl_md5 as hashMD5
from ipaddress import IPv6Address as IPv6

starting_ip='2001:4958::'
ip = IPv6(starting_ip)
aa = 208000000000

hashgen = hashMD5((b'%u' % (ip+aa))).hexdigest()
hashgen2 = hashMD5(('%s' % (ip+aa)).encode('utf-8')).hexdigest()

print(hashgen)
print(hashgen2)
Output:
d6f76fb9ca27fdae847af8ea2f3797e2 6e217802558e0534bfb91f694e045f5e
I know the second one (hashgen2) is correct, but why is the first one (hashgen) not returning the correct MD5 hash? If Python 3.5.2 is using Unicode as a default, then specifying the 'b' string literal should implicitly encode it as Unicode, right?

What am I doing wrong?

Ah, figured it out. 'u' is not a Unicode string literal. Apparently it's for an integer.
Reply


Messages In This Thread
Code that generates MD5 hashes from IPv6 addresses giving differant answers? - by PyMD5 - Oct-16-2016, 02:12 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  'answers 2' is not defined on line 27 0814uu 4 790 Sep-02-2023, 11:02 PM
Last Post: 0814uu
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,631 Mar-27-2023, 07:38 AM
Last Post: buran
  Non cryptographic hashes AndrzejB 3 869 Mar-21-2023, 07:36 PM
Last Post: AndrzejB
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 988 Feb-15-2023, 05:34 PM
Last Post: zsousa
  a function to get IP addresses of interfaces Skaperen 2 1,459 May-30-2022, 05:00 PM
Last Post: Skaperen
  Loop through list of ip-addresses [SOLVED] AlphaInc 7 4,099 May-11-2022, 02:23 PM
Last Post: menator01
  Division calcuation with answers to 1decimal place. sik 3 2,155 Jul-15-2021, 08:15 AM
Last Post: DeaD_EyE
  instance methods sharing addresses mim 1 2,265 Mar-28-2021, 05:22 AM
Last Post: deanhystad
  Convert email addresses to VCF format jehoshua 2 4,730 Mar-06-2021, 12:50 AM
Last Post: jehoshua
  Cannot Assign right Answers To Shuffled Questions Boblows 6 2,810 Jan-22-2021, 09:41 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020