String Encoding and Hashing with Base64 and SHA-256

  • Share this:

Code introduction


This function takes a string as input, encodes it using base64, and then calculates its SHA-256 hash. It returns both the encoded string and the hash value.


Technology Stack : base64, hashlib

Code Type : String encoding and hashing

Code Difficulty : Intermediate


                
                    
import random
import string
import re
import math
import os
import sys
import time
import datetime
import json
import base64
import hashlib
import urllib.parse
import html

def encode_string(input_string):
    """
    将字符串进行编码,包括base64和hash。
    """
    encoded_base64 = base64.b64encode(input_string.encode('utf-8')).decode('utf-8')
    hashed = hashlib.sha256(input_string.encode('utf-8')).hexdigest()
    return encoded_base64, hashed                
              
Tags: