timmy ревизій цього gist 9 months ago. До ревизії
4 files changed, 34 insertions
calculate_file_checksum.py(файл створено)
| @@ -0,0 +1,11 @@ | |||
| 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)}") | |
generate_md5_hash.py(файл створено)
| @@ -0,0 +1,6 @@ | |||
| 1 | + | import hashlib | |
| 2 | + | ||
| 3 | + | data = "example data".encode() | |
| 4 | + | md5_hash = hashlib.md5(data).hexdigest() | |
| 5 | + | ||
| 6 | + | print(f"MD5 雜湊值: {md5_hash}") | |
generate_sha256_hash.py(файл створено)
| @@ -0,0 +1,7 @@ | |||
| 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}") | |
hash_password_with_salt.py(файл створено)
| @@ -0,0 +1,10 @@ | |||
| 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()}") | |