You can download this code by clicking the button below.
This code is now available for download.
This function generates a random string of a specified length, then converts the string to hexadecimal, calculates its MD5 hash, gets the current time, and finally stores the result in JSON format.
Technology Stack : os, random, string, json, hashlib, datetime
Code Type : Code generation
Code Difficulty : Intermediate
import os
import random
import string
import json
import hashlib
import time
import datetime
def generate_random_string(length):
letters = string.ascii_letters
return ''.join(random.choice(letters) for i in range(length))
def xxx(arg1, arg2):
# 生成一个指定长度的随机字符串
random_string = generate_random_string(arg1)
# 将字符串转换为十六进制
hex_string = random_string.encode('utf-8').hex()
# 计算字符串的MD5哈希值
md5_hash = hashlib.md5(hex_string.encode('utf-8')).hexdigest()
# 获取当前时间并格式化
now = datetime.datetime.now()
formatted_time = now.strftime('%Y-%m-%d %H:%M:%S')
# 将结果存储为JSON格式
result = {
"random_string": random_string,
"hex_string": hex_string,
"md5_hash": md5_hash,
"formatted_time": formatted_time
}
return json.dumps(result)
# 调用函数并打印结果
print(xxx(10, 16))