Các tệp có cùng tên không đại diện cho cùng một bản sao: sử dụng hàm băm nội dung để xác định đầu vào

Khách hàng đã gửi hai tài liệu, cả hai đều có tên `final.pdf`, tài liệu nào đã được dịch? Thật nguy hiểm khi dựa vào tên tệp để trả lời câu hỏi này. Các tệp có cùng tên có thể có nội dung khác nhau và các tệp có tên đã thay đổi có thể có cùng byte. Băm nội dung phù hợp để thiết lập sự tương ứng giữa các phiên bản, nhưng không nên mang ý nghĩa là chúng không có.

Những điểm chính của bài viết này

-the content of a file with the same name may be different, and you cannot judge which one has been translated by the file name alone. -SHA-256 calculates the summary based on file bytes, and the hash result is independent of the chunk size. -the hash remains the same when only the file name is changed, but rewriting PDF metadata or resaving the zip package will change the hash. -knowing the summary of a file does not mean you have the right to download it, nor does it mean that you have copyright. Access control is still needed after the cache is hit. Record the input summary before processing, record the output summary after downloading the translation, and associate the language and terminology version.

Các tệp có cùng tên không nhất thiết phải giống nhau và các tệp có tên đã thay đổi có thể có nội dung giống hệt nhau. Nếu một nhóm dựa vào tên tệp để theo dõi bản dịch thì rất dễ có phiên bản sai. Bài viết này giải thích cách sử dụng hàm băm nội dung để để lại dấu vân tay cấp byte của đầu vào và đầu ra cũng như hàm băm có thể và không thể chứng minh những gì.

Các ví dụ sử dụng mẫu tổng hợp để chứng minh phương pháp xử lý. Mã chỉ được thử nghiệm trong một môi trường biệt lập và không thể hiện sự đảm bảo về tất cả các tệp thực hoặc hiệu ứng sản phẩm. Trước khi xử lý các văn bản quan trọng, vui lòng giữ lại bản chính và xem xét theo ranh giới và các mục chấp nhận trong văn bản.

Tại sao tên tệp không đáng tin cậy? Một câu kết luận

Use the content hash for the corresponding version, and the file name is for reference only. The following table describes what to do in common situations.

hoàn cảnh của bạnRecommended practiceReason
Both documents are called final.pdf.So sánh các bản tóm tắt SHA-256, không phải tên tệpThe contents of a file with the same name can be different.
Tập tin đã được đổi tênConfirm whether it is the same byte content with a summaryTên không tham gia vào quá trình phân loại byte
Cần xác nhận bản dịch tương ứng với đầu vào nàoRecord the summary of input and output respectivelyInput and output are different byte contents
Các tập tin có thể được lưu lại hoặc siêu dữ liệu có thể bị ghi đèGhi lại dưới dạng nội dung byte mớiNhững thay đổi về siêu dữ liệu hoặc tham số nén có thể ảnh hưởng đến hàm băm

Để lại dấu vân tay byte cho đầu vào

SHA-256 tính toán thông báo dựa trên byte tệp. Các tệp lớn có thể được đọc theo từng đoạn mà không cần phải tải vào bộ nhớ cùng một lúc. Kết quả băm không liên quan gì đến kích thước khối, phân khối chỉ là một chiến lược đọc chứ không phải là một phần nhận dạng của tệp. Các luồng bộ nhớ bên dưới là các mẫu tổng hợp và không chứa tài liệu thực.

Sử dụng cùng một nội dung và các khối khác nhau để xác minh việc triển khai

import hashlib
from io import BytesIO

def digest(stream, chunk_size=65536):
    if type(chunk_size) is not int or chunk_size <= 0:
        raise ValueError("positive chunk size required")
    h = hashlib.sha256()
    while block := stream.read(chunk_size):
        h.update(block)
    return h.hexdigest()

raw = b"synthetic document version A\n"
a = digest(BytesIO(raw), 3)
assert a == digest(BytesIO(raw), 4096)
assert a == hashlib.sha256(raw).hexdigest()
assert a != digest(BytesIO(raw.replace(b"A", b"B")))
assert digest(BytesIO(b"")) == hashlib.sha256(b"").hexdigest()
try:
    digest(BytesIO(raw), 0)
except ValueError:
    pass
else:
    raise AssertionError("zero chunk must be rejected")

Hàm bắt đầu đọc từ vị trí luồng hiện tại. Trước khi thực hiện cuộc gọi chính thức, cần xác định rõ liệu luồng có ở đầu hay không. Nếu trình phân tích cú pháp đã đọc một phần nội dung thì kết quả của phép tính trực tiếp không phải là "băm toàn bộ tệp". Cách tiếp cận an toàn nhất là bật riêng đầu vào chỉ đọc hoặc đặt lại rõ ràng trước khi tính toán trên luồng có thể nhắm mục tiêu.

Sự khác biệt giữa cùng một byte và cùng một nội dung là gì?

Hàm băm chỉ phản ánh liệu các byte có nhất quán hay không và các hoạt động khác nhau có tác động khác nhau lên nó.

Tình trạngHow the hash may changeHow to explain
Only change the file nameNo changeThe name does not participate in the byte summary
PDF metadata is rewrittenChangeThe page looks the same or may be different
Resave the compressed packageChangeCompression parameters or timestamps can affect bytes
Translate into another languageUsually changeInput and output should be recorded separately

# Core comparison

Judgment modeWhat does that mean?It doesn't mean anything.
Compare file namesIt can only mean the same name.Is the content consistent?
Compare SHA-256 feedsIs the byte content exactly the same?Whether you have the right to use the file, or whether you have copyright

# which method is suitable for you?

if you need to confirm whether the two documents are consistent

Compare the SHA-256 summary, the same description byte content is the same, the file name is not used as the basis.

if you need to track the source of translation

Record the input summary and output summary respectively, and then associate the language and terminology version.

if you plan to use hash to determine file permissions

Don't do this. Hash is not a permission credential and requires additional access control.

# frequently asked questions (FAQ)

1. Will the hash change after changing the file name?

No, I won't. The name does not participate in the byte summary, and the file hash that only changes the file name remains the same.

2. The page looks the same, does the hash have to be the same?

Not necessarily. When PDF metadata is rewritten, the hash may change, even if the page appears the same.

3. Can you use hash to determine whether the user has the right to download the file?

No, I can't. Knowing the summary of the file does not mean you have the right to download it, nor does it mean that you have copyright.

4. What is the risk of cross-tenant resurrection?

Information about whether a file already exists may be disclosed, so access control still needs to be done after a cache hit.

# Translation list starts here

Record the input summary before sending the file to OmniDoc (Product Import); after downloading the translation, record the output summary and associate the language and terminology version. After that, if a colleague reports that "the result is wrong," both parties can first confirm that they are checking the same set of files without having to guess repeatedly around multiple final file names.

Tham khảo: Python hashlib.

# Summary

The same name does not represent the same copy, and the file name can only be used as a reference. Recording input and output with a content hash allows the team to align the file itself before discussing the translation results.

Experience now: upload the document and start translation, keeping the original typesetting

Cập nhật lúc 2026-11-11