TOTP.Calculate
--------------

Calculates a Time-based one-time password.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [TOTP](component_TOTP.md) | [12.3](newinversion123.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "TOTP.Calculate"; Key; TimeNow; TimeStart; TimeStep; DigitCount { ; Algorithm; DecodeBase32 } ) 

**MBS**( **"TOTP.Calculate";** /\* Calculates a Time-based one-time password. \*/  
**$Key**; /\* The secret to use.e.g. "12345678" \*/   
**$TimeNow**; /\* The current time.  
In seconds.e.g. MBS("TOTP.CurrentTime") \*/   
**$TimeStart**; /\* The start time.  
In seconds.e.g. 0 \*/   
**$TimeStep**; /\* The step value defining the duration the code is valid.  
In Seconds.e.g. 30 \*/   
**$DigitCount**; /\* The number of digits to generate.  
Ranges usually from 4 to 9.e.g. 6 \*/   
**$Algorithm**; /\* Optional; The algorithm to use.  
Can be empty, SHA1, SHA256 or SHA512.  
Default is SHA1 if nothing is passed.e.g. "SHA1" \*/   
**$DecodeBase32**) /\* Optional; Whether the password is base32 encoded and should be decoded.e.g. 1 \*/ 

### Parameters

| Parameter | Description | Example | Flags |
|---|---|---|---|
| Key | The secret to use. | "12345678" |  |
| TimeNow | The current time.   In seconds. | MBS("TOTP.CurrentTime") |  |
| TimeStart | The start time.   In seconds. | 0 |  |
| TimeStep | The step value defining the duration the code is valid.   In Seconds. | 30 |  |
| DigitCount | The number of digits to generate.   Ranges usually from 4 to 9. | 6 |  |
| Algorithm | The algorithm to use.   Can be empty, SHA1, SHA256 or SHA512.   Default is SHA1 if nothing is passed. | "SHA1" | Optional      Added in version **12.5**. |
| DecodeBase32 | Whether the password is base32 encoded and should be decoded. | 1 | Optional      Added in version **12.5**. |

### Result

Returns value or error.

### Description

Calculates a Time-based one-time password.  
Uses SHA-1 as hash algorithm.  
  
The result is returned as number and may miss leading zeros, if you just convert it to text.  
### Examples

Calculate a code:

 Set Variable \[$code ; Value: MBS ( "TOTP.Calculate"; "12345678"; MBS ("[TOTP.CurrentTime](TOTPCurrentTime.md)"); 0; 30; 6 ) \]  
Test the function with SHA-1:

 Let(\[  
key = "12345678901234567890";  
TimeNow = 59;  
TimeStep = 30;  
TimeStart = 0;  
Digits = 8;  
r = MBS ( "TOTP.Calculate"; Key; TimeNow; TimeStart; TimeStep; Digits )  
\]; r)  
<small>  
Example result: 94287082</small>Test the function with SHA-256:

 Let(\[  
key = "12345678901234567890123456789012";  
TimeNow = 59;  
TimeStep = 30;  
TimeStart = 0;  
Digits = 8;  
r = MBS ( "TOTP.Calculate"; Key; TimeNow; TimeStart; TimeStep; Digits; "sha256" )  
\]; r)  
<small>  
Example result: 46119246</small>Test the function with SHA-512:

 Let(\[  
key = "1234567890123456789012345678901234567890123456789012345678901234";  
TimeNow = 59;  
TimeStep = 30;  
TimeStart = 0;  
Digits = 8;  
r = MBS ( "TOTP.Calculate"; Key; TimeNow; TimeStart; TimeStep; Digits; "sha512" )  
\]; r)  
<small>  
Example result: 90693936</small>Test with base32 decoding:

 MBS ("TOTP.Calculate"; "JBSWY3DPEHPK3PXP"; MBS ("[TOTP.CurrentTime](TOTPCurrentTime.md)"); 0; 30; 6; "SHA1")  
### See also

- [TOTP.CurrentTime](TOTPCurrentTime.md)

### Release notes

- **Version 15.2**
    - Rewrote parts of [TOTP.Calculate](https://www.mbsplugins.eu/TOTPCalculate.shtml) function, broken with Xcode upgrade.
- **Version 12.5**
    - Added algorithm and DecodeBase32 parameters to [TOTP.Calculate](https://www.mbsplugins.eu/TOTPCalculate.shtml) function.
    - Changed [TOTP.Calculate](https://www.mbsplugins.eu/TOTPCalculate.shtml) function to return number with zeros on front.
- **Version 12.3**
    - Added [TOTP.Calculate](https://www.mbsplugins.eu/TOTPCalculate.shtml) function and [TOTP.CurrentTime](https://www.mbsplugins.eu/TOTPCurrentTime.shtml) function to create Time-based one-time passwords.

### Example Databases

- [Utility functions/TOTP](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Utility%20functions/TOTP.shtml#1ScriptAnchor_)

### Blog Entries

- [MBS FileMaker Plugin, version 15.2pr3](https://www.mbsplugins.de/archive/2025-04-10/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [Neues MBS Plugin 12.5 für Claris FileMaker](https://www.mbsplugins.de/archive/2022-11-08/Neues_MBS_Plugin_125_für_Clar/monkeybreadsoftware_blog_filemaker)
- [MBS Plugin 12.5 for Claris FileMaker - More than 6800 Functions In One Plugin](https://www.mbsplugins.de/archive/2022-11-08/MBS_Plugin_125_for_Claris_File/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 12.5pr4](https://www.mbsplugins.de/archive/2022-10-24/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 12.5pr3](https://www.mbsplugins.de/archive/2022-10-14/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 12.5pr1](https://www.mbsplugins.de/archive/2022-09-29/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin 12.3](https://www.mbsplugins.de/archive/2022-07-19/MBS_FileMaker_Plugin_123/monkeybreadsoftware_blog_filemaker)
- [Neues MBS Plugin 12.3 für Claris FileMaker](https://www.mbsplugins.de/archive/2022-07-12/Neues_MBS_Plugin_123_für_Clar/monkeybreadsoftware_blog_filemaker)
- [MBS Plugin 12.3 for Claris FileMaker](https://www.mbsplugins.de/archive/2022-07-12/MBS_Plugin_123_for_Claris_File/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 12.3pr1](https://www.mbsplugins.de/archive/2022-06-02/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)

This function is free to use.

Created 24th May 2022 , last changed 14th October 2022

  
[TAPI.Unpark](TAPIUnpark.md) - [TOTP.CurrentTime](TOTPCurrentTime.md)

[HTML Version](TOTPCalculate.shtml)