Sha256Text$ ( value$ )
Parameters
| value$ - the text to hash; its exact stored bytes are consumed |
Description
|
Hashes a String's exact bytes and returns the SHA-256 digest as 64 hex characters. SHA-256 gives text a fingerprint: change one character anywhere and all 64 output characters scramble. Games use that to detect corrupted or tampered save data (store the digest alongside the payload and compare on load), to build stable content identifiers from names, and to fingerprint JsonStringify output - which is deterministic precisely so its hash is meaningful. Exactly the stored bytes are hashed - no BOM, no terminator, no newline conversion, no Unicode normalization. That exactness is why WriteTextAtomic followed by Sha256File reproduces the same digest as hashing the string in memory. An empty string is fine and yields the standard empty-message digest. The result is always 64 lowercase hex characters. One honest limit: SHA-256 is an integrity and identity primitive. It spots accidental corruption and casual tampering, but it is not a password-storage API, and a bare hash does not authenticate data - anyone who can rewrite your save can rewrite the digest beside it. This is the hex variant; Sha256TextBytes returns the same digest as a raw 32-byte Bank. The family covers text, Banks and files - all worker-safe. For the full rules, see the Application Data language reference. Requires Extended mode. See also: Sha256TextBytes, Sha256Bank, Sha256File, JsonStringify. |
Example
; Sha256Text Example ; ------------------ ; Requires Extended mode. q$=Chr(34) ; Double-quote character, for building JSON text ; A save-game as text - hashing it gives a fingerprint we can ; store alongside the file to detect corruption or tampering save_text$="{"+q+"player"+q+":"+q+"Aria"+q+","+q+"gold"+q+":1250}" Print "Save-game text: "+save_text Print "" ; Sha256Text hashes the exact stored bytes and returns ; 64 lowercase hexadecimal characters digest$=Sha256Text(save_text) Print "SHA-256: "+digest Print "" ; Change a single character (gold 1250 -> 1251) and the ; digest changes completely - the avalanche effect tampered$=Replace(save_text,"1250","1251") Print "Tampered text: "+tampered Print "SHA-256: "+Sha256Text(tampered) Print "" Print "One changed digit, a completely different fingerprint." Print "" Print "Press any key to close the example" WaitKey End
Index