1

I am generating the md5 hash of the any given URL using the following SQL query

select CONVERT(NVARCHAR(50),HASHBYTES('MD5',convert(nvarchar(2000),'https://alpena-mi.geebo.com/jobs-online/view/id/760191457-advanced-medical-support-assistant-/')),1)

Generated Hash 0x9CA5B450B714C6C07BD1904D3080B4E4

Can anyone tell how do I generate same hash for any given URL using python

Jebaseelan Ravi
  • 697
  • 8
  • 22

2 Answers2

3

For generating md5 hash you can use the hashlib library in python. Follow sample code below

import hashlib
print(hashlib.md5('https://alpena-mi.geebo.com/jobs-online/view/id/760191457-advanced-medical-support-assistant-/').hexdigest())

The hashcode will be 5cf4fb86bb9ce08c17c54f9dba061413

In your example, You have used nvarchar to convert the URL string. Instead of nvarchar you can use varchar or substring to generate expected md5 hash. For more reason: Why generated MD5 hash in sql server are not equal?

SELECT CONVERT(NVARCHAR(50),HASHBYTES('MD5',convert(varchar(2000),'https://alpena-mi.geebo.com/jobs-online/view/id/760191457-advanced-medical-support-assistant-/')),2)

SELECT CONVERT(NVARCHAR(50),HASHBYTES('MD5',SUBSTRING('https://alpena-mi.geebo.com/jobs-online/view/id/760191457-advanced-medical-support-assistant-/',0,2000)),2)
Vignesh Ammasi
  • 381
  • 1
  • 4
  • 10
2

Python has a hashlib library. When using URLs, you need to encode it at the same time you are hashing it.

import hashlib

result = hashlib.sha256("https://alpena-mi.geebo.com/jobs-online/view/id/760191457-advanced-medical-support-assistant-/".encode('utf-8')).hexdigest()

print(result)
algorythms
  • 1,547
  • 1
  • 15
  • 28