Random String Generator with Hashing and File Operations

  • Share this:

Code introduction


Generates a random string of specified length containing uppercase and lowercase letters and digits.


Technology Stack : random, string

Code Type : Code function

Code Difficulty : Intermediate


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

def generate_random_string(length=10):
    return ''.join(random.choices(string.ascii_letters + string.digits, k=length))

def hash_password(password):
    salt = os.urandom(16)
    pwdhash = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), salt, 100000)
    return base64.b64encode(salt + pwdhash).decode('utf-8')

def convert_time_to_timestamp(time_str):
    datetime_obj = datetime.datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')
    return int(datetime_obj.timestamp())

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

def random_integer(min_value, max_value):
    return random.randint(min_value, max_value)

def create_directory(path):
    if not os.path.exists(path):
        os.makedirs(path)

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

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

def json_load(file_path):
    with open(file_path, 'r') as file:
        return json.load(file)

def json_dump(data, file_path):
    with open(file_path, 'w') as file:
        json.dump(data, file)                
              
Tags: