Guest User

Untitled

a guest
Aug 25th, 2019
1,712
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2.  
  3. import hashlib
  4.  
  5.  
  6. def encode_sha256(text_origin, encoding='utf-8'):
  7.     """
  8.    function to decode the text into string key.
  9.    with sha256 algorithm.
  10.  
  11.    :param `text` is plain text to encode.
  12.    :return a dict of origin keys & output keys.
  13.    :example
  14.        >>> encode_sha256('saya')
  15.        {'origin': '3874c0168198d8950ea30963ac3657f6c5eff842503e1ee0125ab111696d74dc',
  16.         'output': '8879x0668698d8900ez80968zx8607f6x0eff897008e6ee0670zy666696d79dx'}
  17.    """
  18.     text_origin = str(text_origin)
  19.     hash_origin = hashlib.sha256(text_origin.encode(encoding)).hexdigest()
  20.     hash_output = hash_origin
  21.  
  22.     tokens = ['1', '2', '3', '4', '5', 'a', 'b', 'c']
  23.     replacers = ['6', '7', '8', '9', '0', 'z', 'y', 'x']
  24.  
  25.     for (token, replacer) in zip(tokens, replacers):
  26.         hash_output = hash_output.replace(token, replacer)
  27.  
  28.     return dict(origin=hash_origin, output=hash_output)
  29.  
  30.  
  31. def validate_sha256(text_origin, text_encoded):
  32.     """
  33.    function to validate the plain text
  34.    with hashed output of the key, it valid or not.
  35.  
  36.    :param `text_origin` is string plain text to validate.
  37.    :param `text_encoded` is string hashed output from `encode_sha256`.
  38.    :example
  39.        >>> validate_sha256('saya', '8879x0668698d8900ez80968zx8607f6x0eff897008e6ee0670zy666696d79dx')
  40.        True
  41.        >>> validate_sha256('saya', '3874c0168198d8950ea30963ac3657f6c5eff842503e1ee0125ab111696d74dc')
  42.        False
  43.    """
  44.     return encode_sha256(text_origin).get('output') == text_encoded
RAW Paste Data