I am trying to generate an id that is generated in the backend, in the frontend (not the best of practices, I know). The id is an Md5 hash of several (dynamically generated, i.e, I don't know the hash in advance) strings concatenated together. So what I tried to do is to use the Md5 class (in ts-md5/dist/md5
) to create an Md5 hash:
getId(s1: string, s2: string, s3: string) : string {
return Md5.hashStr(`${s1}/${s2}/${s3}`);
}
But, the hash returned by the backend did not match the hash returned by getId()
.
Here is how I create the hash in the backend (Python):
id = hashlib.md5("some_string_seed".encode("utf-8")).hexdigest()
I thought that maybe the problem is in the encoding, but adding encoding didn't change anything:
encode_utf8( s )
{
return unescape( encodeURIComponent( s ) );
}
return Md5.hashStr(encode_utf8(`${s1}/${s2}/${s3}`));
I tried some basic examples but the hashes just don't match.
How do I make the hashes the same? (cannot modify the backend)
The solution for me was to use crypto-js
, whose md5 hashing algorithm produces the same result as the hashlib
, Python library.