Random String Password Generator with SHA-256 Hashing

  • Share this:

Code introduction


This function generates a random alphanumeric string of a specified length and hashes the password with SHA-256.


Technology Stack : random, string, hashlib

Code Type : Password Generation and Hashing

Code Difficulty : Intermediate


                
                    
import os
import re
import json
import random
import string
import hashlib
import datetime

def generate_random_string(length=10):
    return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(length))

def hash_password(password):
    return hashlib.sha256(password.encode()).hexdigest()

def read_file(file_path):
    with open(file_path, 'r') as file:
        return file.read()

def find_all_matches(pattern, text):
    return re.findall(pattern, text)

def write_file(file_path, content):
    with open(file_path, 'w') as file:
        file.write(content)

def format_datetime(dt):
    return dt.strftime('%Y-%m-%d %H:%M:%S')

def generate_json_data(data):
    return json.dumps(data, indent=4)

def xxx(arg1, arg2):
    # 代码含义解释:该函数用于生成一个包含指定长度随机字母数字字符串的密码
    password = generate_random_string(arg1)
    hashed_password = hash_password(password)
    return hashed_password