Pythonはランダム文字列とhash暗号化を生成する


#!/usr/bin/env python3
# -*- coding: utf-8 -*-


"""
create_author :       
create_time   : 2019-03-28
program       : *_* symbol(secret key and hash code) generation handler *_*
"""


import time
import hashlib
import random
import string


def secret_key(length=30):
    """
    Generate secret key from alpha and digit.
    :param length: length of secret key.
    :return: [length] long secret key.
    """
    key = ''
    while length:
        key += random.choice(string.ascii_letters + string.digits)
        length -= 1
    return key


def hash_code(*args, **kwargs):
    """
    Generate 64-strings(in hashlib.sha256()) hash code.
    :param args: for any other position args packing.
    :param kwargs: for any other key-word args packing.
    :return: 64-strings long hash code.
    """
    text = ''
    if not args and not kwargs:
        text += time.strftime("%Y%m%d%H%M%s")
    if args:
        for arg in args:
            text += str(arg)
    if kwargs:
        for kwarg in kwargs:
            text += str(kwargs[kwarg])
    return hashlib.sha256(text.encode("utf-8")).hexdigest()


if __name__ == "__main__":
    print(secret_key())
    print(len(hash_code()), ':', hash_code())