Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old

Hash.SHA256

Calculated SHA-256 hash of given text.

Component Version macOS Windows Linux Server iOS SDK
Hash 3.1 ✅ Yes ✅ Yes ✅ Yes ✅ Yes ✅ Yes
MBS( "Hash.SHA256"; text { ; Flags } )   More

Parameters

Parameter Description Example Flags
text The text to process. "Hello World"
Flags Various flags you can combine by addition.
Pass 1 for getting result Base64 encoded instead of Hex encoded.
Pass 2 if input data is Hex encoded and plugin should decode it first.
Pass 8 for base64URL encoding. (new in v10.3)
0 Optional

Result

Returns SHA-256 hash.

Description

Calculated SHA-256 hash of given text.
Text is converted to UTF-8 to make sure the text encoding doesn't change the hash value.
Hashes are used to store a fingerprint of some data. When hashes are not equal, the data is probably also not equal.
This function is good to make hashes for passwords. You don't save the password in your database, but only the hash. On login, you calculate the hash for the password entered by the user and compare the hash with the stored one. This way the database only contains hashes. And it's unlikely someone finds the password given the hash, except for easy cases like "hello" where google will tell you the text for the hash.
To make hashes more secure, use a prefix/suffix like the user id or simply your application name. This way even hello as a password will be quite impossible to recover from the hash.
SHA-256 is stronger than SHA-1.

Please be aware that text can result in different hashes if it contains line endings. You may want to make sure that line endings are right with the Text.ReplaceNewline function before you compare hashes to results in other development tools. FileMaker uses Mac line endings by default and most examples for Javascript use Unix line endings.

Examples

Calculate hash of password:

MBS( "Hash.SHA256"; $password )

Calculate empty text hash:

MBS("Hash.SHA256"; "")

Example result: E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855

Calculate hash:

MBS("Hash.SHA256"; "The quick brown fox jumps over the lazy dog")

Example result: D7A8FBB307D7809469CA9ABCB0082E4F8D5651E46D3CDB762D02D0BF37C9E592

FileMaker 16 vs. Plugin:

# MBS Plugin:
MBS( "Hash.SHA256"; "Hello World")
# Same via FileMaker 16 native:
HexEncode( CryptDigest ( "Hello World"; "SHA256" ))

Hash a file with newer or older way:

#newer function:
Set Variable [$hash1; Value: MBS( "Hash.DigestFile"; "SHA256"; "/Users/cs/Desktop/test.xml"; "base64") ]

#older way:
Set Variable [$hash2; Value: MBS( "Hash.SHA256"; MBS("Text.ReadTextFile"; "/Users/cs/Desktop/test.xml"; "UTF-8"); 1)]

See also

Release notes

Blog Entries

This function is free to use.

Created 18th August 2014, last changed 11st March 2021


Hash.SHA1.HMAC - Hash.SHA256.HMAC