File dengan nama yang sama tidak mewakili salinan yang sama: gunakan hashing konten untuk mengidentifikasi masukan
Klien mengirimkan dua dokumen, keduanya bernama `final.pdf`, yang mana yang diterjemahkan? Berbahaya jika mengandalkan nama file untuk menjawab pertanyaan ini. File dengan nama yang sama mungkin memiliki konten berbeda, dan file dengan nama yang diubah mungkin memiliki byte yang sama. Hash konten cocok untuk membuat korespondensi versi, tetapi tidak boleh diberi arti yang tidak dimilikinya.
File dengan nama yang sama belum tentu sama, dan file dengan nama yang diubah mungkin memiliki konten yang sama persis. Jika sebuah tim mengandalkan nama file untuk melacak terjemahan, mudah untuk mendapatkan versi yang salah. Artikel ini menjelaskan cara menggunakan hashing konten untuk meninggalkan sidik jari input dan output tingkat byte, dan apa yang dapat dan tidak dapat dibuktikan oleh hash.
Contohnya menggunakan sampel sintetis untuk mendemonstrasikan metode pemrosesan. Kode ini hanya diuji di lingkungan yang terisolasi dan tidak mewakili jaminan semua file nyata atau efek produk. Sebelum memproses dokumen penting, harap simpan dokumen asli dan tinjau sesuai dengan batasan dan item penerimaan dalam teks.
Mengapa nama file tidak dapat diandalkan? Kesimpulan satu kalimat
Gunakan hash konten agar sesuai dengan versinya, dan nama file hanya untuk referensi. Tabel berikut menjelaskan apa yang harus dilakukan dalam situasi umum.
| situasi Anda | Praktik yang direkomendasikan | alasan |
|---|---|---|
Kedua dokumen tersebut disebut final.pdf | Bandingkan intisari SHA-256, bukan nama file | Isi file dengan nama yang sama bisa berbeda |
| File telah diganti namanya | Gunakan intisari untuk mengonfirmasi apakah konten byte tersebut sama | Nama tidak berpartisipasi dalam intisari byte |
| Perlu mengonfirmasi input mana yang sesuai dengan terjemahannya | Catat ringkasan input dan output secara terpisah | Input dan output adalah konten byte yang berbeda |
| File mungkin disimpan ulang atau metadata mungkin ditimpa | Rekam ulang sebagai konten byte baru | Perubahan metadata atau parameter kompresi dapat memengaruhi hash |
Tinggalkan sidik jari byte untuk input
SHA-256 menghitung intisari berdasarkan byte file. File berukuran besar dapat dibaca dalam potongan-potongan tanpa harus dimuat ke dalam memori sekaligus. Hasil hash tidak ada hubungannya dengan ukuran potongan, pemotongan hanyalah strategi membaca dan bukan bagian dari identitas file. Aliran memori di bawah ini adalah sampel sintetis dan tidak berisi dokumen asli.
Gunakan konten yang sama dan blok berbeda untuk memverifikasi penerapannya
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")
Fungsi ini mulai membaca dari posisi aliran saat ini. Sebelum melakukan panggilan resmi, harus jelas apakah alirannya ada di awal. Jika parser telah membaca sebagian konten, hasil penghitungan langsung bukanlah "hash seluruh file". Pendekatan yang paling aman adalah dengan mengaktifkan input read-only satu per satu, atau mengatur ulang input tersebut secara eksplisit sebelum menghitungnya pada aliran yang dapat ditargetkan.
Apa perbedaan antara byte yang sama dan konten yang sama?
Hash hanya mencerminkan apakah byte konsisten, dan operasi yang berbeda memiliki efek yang berbeda pula.
| Situation | How the hash may change | How to explain |
|---|---|---|
| Only change the file name | No change | The name does not participate in the byte summary |
| PDF metadata is rewritten | Change | The page looks the same or may be different |
| Resave the compressed package | Change | Compression parameters or timestamps can affect bytes |
| Translate into another language | Usually change | Input and output should be recorded separately |
# Core comparison
| Judgment mode | What does that mean? | It doesn't mean anything. |
|---|---|---|
| Compare file names | It can only mean the same name. | Is the content consistent? |
| Compare SHA-256 feeds | Is 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.
Referensi: 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


