Topics   All   Mac OS X (Only)   Windows (Only)   Linux (Only, Not)   iOS (Only, Not)  
Components   Crossplatform Mac & Win   Server   Client   Old   Guides   Examples
New in version: 7.0   7.1   7.2   7.3   7.4   7.5   8.0   8.1   8.2   8.3    Statistic  

Hash.SHA1

Calculated SHA-1 hash of given text.

Component Version macOS Windows Server FileMaker Cloud FileMaker iOS SDK
Hash 3.1 Yes Yes Yes Yes Yes

MBS( "Hash.SHA1"; text { ; Flags } )

Parameters

Parameter Description Example value
text The text to process. "Hello World"
Flags Optional
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.
0

Result

Returns SHA-256 hash.

Description

Calculated SHA-1 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-1 is stronger than MD5 and weaker than SHA-512.

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.SHA1"; $password )

Calculate empty hash:

MBS("Hash.SHA1"; "")

Example result: DA39A3EE5E6B4B0D3255BFEF95601890AFD80709

Calculate empty hash as Base64:

MBS("Hash.SHA1"; ""; 1)

Example result: "2jmj7l5rSw0yVb/vlWAYkK/YBwk="

Calculate for two similar texts:

MBS("Hash.SHA1"; "Franz jagt im komplett verwahrlosten Taxi quer durch Bayern")

Example result: 68AC906495480A3404BEEE4874ED853A037A7A8F

Encode Password like in PHP:

# original in PHP:
#
# $nonce_date_pwd = pack("A*",$nonce) . pack("A*",$CREATIONDATE) . pack("H*", sha1($password));
# $PASSWORDDIGEST = base64_encode( pack('H*', sha1($nonce_date_pwd)));
#
# in FileMaker:
#
# you can use Hash.RandomHexString to make your Nonce.
#
Set Variable [ $NonceHexEncoded ; Wert: MBS("Text.EncodeToHex"; Password Digest::Nonce) ]
Set Variable [ $CreationDateHexEncoded ; Wert: MBS("Text.EncodeToHex"; Password Digest::CreationDate) ]
Set Variable [ $PasswordHashHexEncoded ; Wert: MBS( "Hash.SHA1"; Password Digest::Password; 0 ) ]
Set Variable [ $Input ; Wert: $NonceHexEncoded & $CreationDateHexEncoded & $PasswordHashHexEncoded ]
# build hash by decoding from Hex, doing SHA1 and encode back to Base64
Set Variable [ $PasswordDigest ; Wert: MBS( "Hash.Digest"; "SHA1"; "hex"; $input; ""; "Base64"; "" ) ]
Set Field [ Password Digest::PasswordDigest ; $PasswordDigest ]

Calculate for two similar texts:

MBS("Hash.SHA1"; "Frank jagt im komplett verwahrlosten Taxi quer durch Bayern")

Example result: D8E8ECE39C437E515AA8997C1A1E94F1ED2A0E62

FileMaker 16 vs. Plugin:

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

See also


Hash.RandomString   -   Hash.SHA1.HMAC

Feedback: Report problem or ask question.




Links
MBS FileMaker Plugins