Computer >> Máy Tính >  >> Lập trình >> Python

Tạo mã băm bằng cách sử dụng uuid3 () và uuid5 () trong Python

Số nhận dạng duy nhất phổ biến là một số thập lục phân 32 bit có thể đảm bảo một giá trị duy nhất trong một không gian tên nhất định. Điều này giúp theo dõi các đối tượng được tạo bởi một chương trình hoặc nơi mà python cần xử lý đối tượng hoặc dữ liệu cần giá trị lớn của mã định danh. Lớp UUID xác định các hàm có thể tạo các giá trị này.

Cú pháp

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.

Trong ví dụ dưới đây, chúng ta thấy rằng chúng ta có thể chọn một chuỗi ban đầu có thể được sử dụng thêm để tạo uuids ..

Ví dụ

import uuid
# A given string
str1 = "www.tutorialspoint.com"
str2 = "https://www.Tutorialspoint.com"
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))

Chạy đoạn mã trên cho chúng ta kết quả sau:

Đầu ra

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