Blitz3D+ Command Reference

Sha256File$ ( file$ )

Parameters

file$ - path of the file to hash

Description

Hashes a file's raw bytes and returns the SHA-256 digest as 64 hex characters.

The file member of the SHA-256 family, and the natural integrity check for things on disk: verify a save file was not corrupted or hand-edited, confirm a patch download is complete, or build a manifest of asset fingerprints for your installer. The digest covers the raw file bytes exactly as stored, so it matches what any external SHA-256 tool reports for the same file.

The file is read in bounded chunks, never loaded whole - hashing a huge asset archive will not spike memory. It is read-only and worker-safe, so a Job can fingerprint files in the background (checking for cancellation between chunks); pair it with the main-thread-only WriteTextAtomic / WriteBankAtomic, which write bytes exactly so a write-then-hash round trip is stable.

On failure - missing file, locked file, bad path - it returns an empty string and sets the FilesystemError diagnostics (message, code, path); a successful hash clears them. The result is otherwise always 64 lowercase hex characters.

SHA-256 is an integrity and identity primitive: not a password-storage API, and a bare hash does not authenticate data by itself. This is the hex variant; Sha256FileBytes returns the same digest as a raw 32-byte Bank.

For the full rules, see the Application Data language reference.

Requires Extended mode.

See also: Sha256FileBytes, Sha256Text, Sha256Bank, FilesystemError, WriteTextAtomic.

Example

; Sha256File Example
; ------------------
; Requires Extended mode.

q$=Chr(34)  ; Double-quote character, for building JSON text

; Write a save-game file, then fingerprint it for integrity checks
save_text$="{"+q+"player"+q+":"+q+"Aria"+q+","+q+"gold"+q+":1250}"
WriteTextAtomic "sha_file_example.json",save_text
Print "Wrote sha_file_example.json: "+save_text
Print ""

; Sha256File hashes the raw file bytes in bounded chunks -
; large save files do not need to fit in memory
file_digest$=Sha256File("sha_file_example.json")
Print "Sha256File: "+file_digest
Print ""

; WriteTextAtomic wrote the exact string bytes, so hashing the
; same text in memory gives the identical digest
text_digest$=Sha256Text(save_text)
Print "Sha256Text: "+text_digest
Print ""
Print "File and text digests match: "+(file_digest=text_digest)

; Clean up the file this example created
DeleteFile "sha_file_example.json"

Print ""
Print "Press any key to close the example"
WaitKey

End

Index