【用Python根据用户名和手机号码生成Hash值并创建.cs .h和xlsx文件】

用Python根据用户名和手机号码生成Hash值并创建C Sharp .cs、嵌入式.h和xlsx表格文件


用Python根据用户名和手机号码生成Hash值并创建C Sharp .cs、嵌入式.h和xlsx表格文件,主要是为每个用户创建一个pubkey,并输出C Sharp C#和嵌入式 Keil的工程文件 pubkey.cs和pubkey.h文件。并把用户名和手机号码记录在excel表格中。

def writeinpara():
    global username
    global email
    username=input('请输入用户名:')
    email=input('请输入手机号码:')
    print("username="+username+"; Email="+email)
#生成md5 hashh
def md5_hash(data: str) -> str:
    m = hashlib.md5()
    m.update(data.encode('utf-8'))
    return m.hexdigest()


下面建立目录

def createDir(dirpath,username,email):
    global url
    url=dirpath+username+"_"+email
    if(os.path.exists(dirpath+username+"_"+email)):
        shutil.rmtree(url)  #删除目录,包括目录下的所有文件
    os.mkdir(url,0o777)

下面写入CS文件

    print("writeCSfile =",filepath)
    if os.path.isfile(filepath):
        os.remove(filepath)
    #encoding='utf-8-sig'    
    #with open(filepath, "w", encoding="utf-8") as file:
    with open(filepath, "w", encoding='utf-8-sig') as file:
        file.write(csFileStrBegin+hexdigest+csFileStrEnd)
        file.close()
    #if os.path.isfile(filepath):
    #    print("CS file exist,open it!")
    #    with open(filepath, "w", encoding="utf-8") as file:
    #    file.write(csFileStrBegin+hexdigest+csFileStrEnd)
        #worksheet = workbook.active
    #else:
        #文件不存在,创建新的Exce1文件
    #    print("CS file created!")

写入.h文件

def writeHfile(filepath,username,email,hexdigest):
    lengthint=len(hexdigest)
    lengthstr=str(lengthint)
    print("writeHfile =",filepath)
    if os.path.isfile(filepath):
        os.remove(filepath)
    with open(filepath, "w", encoding="utf-8") as file:
        file.write("#define USER_PUBKEY_LEN   "+lengthstr+"\r")
        file.write("#define USER_PUBKEY   \""+hexdigest+"\"\r")
        file.close()

写入excel文件

def writepubkey(workbook,username,email,hexdigest):
    #sheet = workbook.get_sheet_by_name('sheet1')
    sheet = workbook.active
    #last_row = sheet.max_row + 1
    # 获取当前日期和时间
    now = datetime.datetime.now()

    # 格式化日期和时间
    datevalue = now.strftime("%Y-%m-%d")
    timevalue = now.strftime("%H:%M:%S")
    insertData = [datevalue,timevalue, username,email,hexdigest]
    sheet.append(insertData)
    #sheet.cell(last_row+1,  column=1,value=datevalue)
    #sheet.cell(last_row+1,  column=2,value=timevalue)
    #sheet.cell(last_row+1,  column=3,value=username)
    #sheet.cell(last_row+1,  column=4,value=email)
    #sheet.cell(last_row+1,  column=5,value=hexdigest)
    # 保存Excel文件
    workbook.save(filepath)
    workbook.close()

  

视频:B站 柔贝特三哥

一个脚本工具调用Python生产pubkey和嵌入式.h文件和.cs的c#文件,调用keil和visual studio编译arm和c#的工程,执行文件拷贝功能

你可能感兴趣的:(python,哈希算法,开发语言,c语言,c#)