calculate_file_checksum.py
· 329 B · Python
原始檔案
import hashlib
def file_checksum(file_path):
sha256 = hashlib.sha256()
with open(file_path, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
sha256.update(chunk)
return sha256.hexdigest()
file_path = "example.txt"
print(f"{file_path} 的 SHA-256 雜湊值: {file_checksum(file_path)}")
| 1 | import hashlib |
| 2 | |
| 3 | def file_checksum(file_path): |
| 4 | sha256 = hashlib.sha256() |
| 5 | with open(file_path, "rb") as f: |
| 6 | for chunk in iter(lambda: f.read(4096), b""): |
| 7 | sha256.update(chunk) |
| 8 | return sha256.hexdigest() |
| 9 | |
| 10 | file_path = "example.txt" |
| 11 | print(f"{file_path} 的 SHA-256 雜湊值: {file_checksum(file_path)}") |
| 12 |
generate_md5_hash.py
· 125 B · Python
原始檔案
import hashlib
data = "example data".encode()
md5_hash = hashlib.md5(data).hexdigest()
print(f"MD5 雜湊值: {md5_hash}")
| 1 | import hashlib |
| 2 | |
| 3 | data = "example data".encode() |
| 4 | md5_hash = hashlib.md5(data).hexdigest() |
| 5 | |
| 6 | print(f"MD5 雜湊值: {md5_hash}") |
| 7 |
generate_sha256_hash.py
· 181 B · Python
原始檔案
import hashlib
data = "Hello, World!".encode() # 轉換為位元組
hash_object = hashlib.sha256(data)
hash_hex = hash_object.hexdigest()
print(f"SHA-256 雜湊值: {hash_hex}")
| 1 | import hashlib |
| 2 | |
| 3 | data = "Hello, World!".encode() # 轉換為位元組 |
| 4 | hash_object = hashlib.sha256(data) |
| 5 | hash_hex = hash_object.hexdigest() |
| 6 | |
| 7 | print(f"SHA-256 雜湊值: {hash_hex}") |
| 8 |
hash_password_with_salt.py
· 262 B · Python
原始檔案
import hashlib
import os
password = "mypassword".encode()
salt = os.urandom(16) # 產生隨機鹽值
hashed_password = hashlib.pbkdf2_hmac("sha256", password, salt, 100000)
print(f"雜湊後的密碼: {hashed_password.hex()}")
print(f"鹽值: {salt.hex()}")
| 1 | import hashlib |
| 2 | import os |
| 3 | |
| 4 | password = "mypassword".encode() |
| 5 | salt = os.urandom(16) # 產生隨機鹽值 |
| 6 | |
| 7 | hashed_password = hashlib.pbkdf2_hmac("sha256", password, salt, 100000) |
| 8 | |
| 9 | print(f"雜湊後的密碼: {hashed_password.hex()}") |
| 10 | print(f"鹽值: {salt.hex()}") |
| 11 |