Created
February 16, 2020 23:31
-
-
Save chtzvt/c3d7d34e5476f53c3a9fe28268bfd3f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import hashlib | |
class CardHasher(): | |
def __init__(self, **kwargs): | |
if 'alg' in kwargs: | |
self.hasher = kwargs['alg'] | |
else: | |
self.hasher = hashlib.sha256 | |
if 'salt' in kwargs: | |
self.salt = kwargs['salt'] | |
else: | |
self.salt = "" | |
self.truncate = kwargs.get('truncate') | |
def hash(self, data): | |
alg = self.hasher() | |
alg.update(self.salt) | |
alg.update(data) | |
if self.truncate is None: | |
return base64.urlsafe_b64encode(alg.digest()) | |
return base64.urlsafe_b64encode(alg.digest()[:self.truncate]) | |
def main(): | |
str = "cheesed52dc7324" | |
salt = "lol" | |
trunclen = 15 | |
hasher1 = CardHasher(alg=hashlib.sha256, salt=salt) | |
hasher2 = CardHasher(salt=salt) | |
hasher3 = CardHasher(salt=salt, truncate=trunclen) | |
print(hasher1.hash(str) + '\n') | |
print(hasher1.hash(str) + '\n') | |
print(hasher2.hash(str) + '\n') | |
print(hasher3.hash(str) + '\n') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment