Sha256FileBytes ( file$ )
Parameters
| file$ - path of the file to hash |
Description
|
Hashes a file's raw bytes and returns the raw SHA-256 digest as a 32-byte Bank. The raw-bytes twin of Sha256File: identical chunked file hashing, but the digest arrives as 32 raw bytes instead of hex text - the form binary manifests and save headers want. Rendering the bytes as lowercase hex reproduces Sha256File's output exactly. Like its sibling, it reads the file in bounded chunks (large files never load whole), is read-only and worker-safe so a background Job can fingerprint assets, and hashes the raw stored bytes so the digest matches external SHA-256 tools. On failure - missing file, locked file, bad path - it returns 0 and sets the FilesystemError diagnostics (message, code, path); a successful hash clears them. Check for 0 before using the result. On success the returned Bank is new, exactly 32 bytes, and owned by you - free it with FreeBank. SHA-256 is an integrity and identity primitive: not a password-storage API, and a bare hash does not authenticate data by itself. Siblings cover text (Sha256TextBytes) and Banks (Sha256BankBytes). For the full rules, see the Application Data language reference. Requires Extended mode. See also: Sha256File, Sha256TextBytes, Sha256BankBytes, FilesystemError, FreeBank. |
Example
; Sha256FileBytes Example ; ----------------------- ; Requires Extended mode. ; Write a small save-game file to fingerprint WriteTextAtomic "sha_bytes_example.json","{}" Print "Wrote sha_bytes_example.json" Print "" ; Sha256FileBytes returns the digest as a new owned 32-byte Bank ; (zero with FilesystemError diagnostics on failure) digest=Sha256FileBytes("sha_bytes_example.json") If digest=0 Then RuntimeError FilesystemError() Print "Digest Bank size: "+BankSize(digest)+" bytes (always 32)" ; Show the raw bytes as hex - it matches Sha256File's hex output hex_text$="" For i=0 To BankSize(digest)-1 hex_text=hex_text+Right(Hex(PeekByte(digest,i)),2) Next Print "Digest bytes: "+Lower(hex_text) Print "Sha256File gives: "+Sha256File("sha_bytes_example.json") ; The digest Bank is owned by us and must be freed FreeBank digest ; Clean up the file this example created DeleteFile "sha_bytes_example.json" Print "" Print "Press any key to close the example" WaitKey End
Index