Skip to content

Instantly share code, notes, and snippets.

@willnix
Created July 4, 2019 23:15
Show Gist options
  • Save willnix/25a0b56e59fdd67a0d2b8923ee38999c to your computer and use it in GitHub Desktop.
Save willnix/25a0b56e59fdd67a0d2b8923ee38999c to your computer and use it in GitHub Desktop.
Python version of hashUnencodedChars
#!/usr/bin/env python3
import hashlib
def hash_unencoded_chars(s: str) -> hashlib.sha256:
""" hash_unencoded_chars - emulates com.google.common.hash.Hashing.sha256().hashUnencodedChars(s)
Google's java library guava comes with the hashUnencodedChars function
which yields different results than most other language's hashing functions.
This function should be compatible.
"""
b = s.encode('utf-16-le')
return hashlib.sha256(b)
if __name__ == "__main__":
print(hash_unencoded_chars('String').hexdigest())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment