使用 Python 中的 uuid3() 和 uuid5() 生成雜湊識別符號
通用唯一識別符號是一個 32 位十六進位制數字,能夠在給定的名稱空間中保證值是唯一的。此識別符號在跟蹤程式建立的物件或在 Python 中需要處理具較大識別符號值的物件或資料時很有用。UUID 類定義了可建立此類值的函式。
語法
uuid3(namespace, string) uuid3 usesMD5 hash value to create the identifier. Uuid5(namespace, string) Uuid5 uses SHA-1 hash value to create the identifier. The namespace can be – NAMESPACE_DNS : Used when name string is fully qualified domain name. NAMESPACE_URL : Used when name string is a URL.
在以下示例中,我們可以看到,可以選擇一個初始字串,以便進一步用於建立 uuid。
示例
import uuid
# A given string
str1 = "www.tutorialspoint.com"
str2 = "https://tutorialspoint.tw"
print("Using uuid3, the generated ID is :\n",
uuid.uuid3(uuid.NAMESPACE_URL, str1))
print("Using uuid3, the generated ID is :\n",
uuid.uuid3(uuid.NAMESPACE_DNS, str2))
print("Using uuid5, the generated ID is :\n ",
uuid.uuid5(uuid.NAMESPACE_URL, str1))
print("Using uuid5, the generated ID is :\n",
uuid.uuid5(uuid.NAMESPACE_DNS, str2))執行以上程式碼,得出以下結果
輸出
Using uuid3, the generated ID is : e5051d13-d1a5-381a-bc21-5017b275a7f2 Using uuid3, the generated ID is : de365612-734a-38e3-abc4-6e3ffc7d61db Using uuid5, the generated ID is : a064f94e-5ff6-51e4-88e2-e2163a79abce Using uuid5, the generated ID is : b9761e0a-0ef3-5fd3-9ec4-86b6e073e61b
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP