Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import hashlib
- def encode_sha256(text_origin, encoding='utf-8'):
- """
- function to decode the text into string key.
- with sha256 algorithm.
- :param `text` is plain text to encode.
- :return a dict of origin keys & output keys.
- :example
- >>> encode_sha256('saya')
- {'origin': '3874c0168198d8950ea30963ac3657f6c5eff842503e1ee0125ab111696d74dc',
- 'output': '8879x0668698d8900ez80968zx8607f6x0eff897008e6ee0670zy666696d79dx'}
- """
- text_origin = str(text_origin)
- hash_origin = hashlib.sha256(text_origin.encode(encoding)).hexdigest()
- hash_output = hash_origin
- tokens = ['1', '2', '3', '4', '5', 'a', 'b', 'c']
- replacers = ['6', '7', '8', '9', '0', 'z', 'y', 'x']
- for (token, replacer) in zip(tokens, replacers):
- hash_output = hash_output.replace(token, replacer)
- return dict(origin=hash_origin, output=hash_output)
- def validate_sha256(text_origin, text_encoded):
- """
- function to validate the plain text
- with hashed output of the key, it valid or not.
- :param `text_origin` is string plain text to validate.
- :param `text_encoded` is string hashed output from `encode_sha256`.
- :example
- >>> validate_sha256('saya', '8879x0668698d8900ez80968zx8607f6x0eff897008e6ee0670zy666696d79dx')
- True
- >>> validate_sha256('saya', '3874c0168198d8950ea30963ac3657f6c5eff842503e1ee0125ab111696d74dc')
- False
- """
- return encode_sha256(text_origin).get('output') == text_encoded
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement