This library implements the MD5 hashing algorithm in a PLC-compatible structured text language for Siemens S7-1500 PLCs. The implementation follows the standard MD5 algorithm, processing input strings into 128-bit hash values.
Caution
Use at your own risk. MD5 has been found to suffer from extensive vulnerabilities, and therefore should not be used as a crypthographic hash function! If you need a crypthographic hash funciton, we recommend using a state of the art algorithm. There is also a crypto-library for SIMATIC AX - see the official documentation.
Enter:
apax add @simatic-ax/md5
to install this package you need to login into the GitHub registry. You'll find more information here
- Call
Hash(InputString := InputText, HashOutput => HashedValue)with theinputTextas string to be hashed. - The function returns a 32-character hexadecimal MD5 hash and writes it to
HashedValue.
VAR
InputText : STRING := 'P@ssw0rd!'; // Example input string
HashedValue : STRING; // Variable to hold the hashed output
hasher : hash.md5;
END_VAR
// Call the Hash method to compute the MD5 hash
md5.Hash(InputString := InputText, HashOutput => HashedValue);
- No built-in error handling
- Maximum number of characters to be used in an input string: 247
- Only usable with standard ASCII strings - no complete unicode support (see disclaimer)
When working with strings or passwords that contain special characters such as $, \ or Unicode characters (e.g., é, ü, non-Latin scripts, or emoji), be aware that PLCs may not handle these characters correctly due to parsing or string handling limitations. In particular:
$$(double dollar signs) may be misinterpreted or cause parsing issues.\\(double backslashes) can be collapsed, rejected, or mishandled if not escaped properly.- Unicode characters like
émay result in inconsistent MD5 hashes due to incorrect or unsupported encoding (e.g., using Latin-1 instead of UTF-8).
Recommended Workarounds:
- Avoid using these characters where possible.
- Sanitize input strings before passing them into PLC logic.
- Use UTF-8 encoding consistently when generating MD5 hashes outside the PLC.
- If needed, consider encoding special characters (e.g., replace
$with%24or use ASCII-based functions likeCharsToString([36])for$).
Always test your string handling on your specific PLC setup to ensure correct behavior.
Hashes the input string using the MD5 algorithm and returns the computed hash as a hexadecimal string.
Pads the input string to fit into 512-bit blocks as required by MD5.
Loads a 512-bit block of the padded input string into the message array.
Processes a single 512-bit block using the MD5 transformation algorithm.
Converts a DWORD value into an 8-character hexadecimal string representation.
F(X, Y, Z) : DWORDG(X, Y, Z) : DWORDH(X, Y, Z) : DWORDI(X, Y, Z) : DWORDThese functions implement the fundamental bitwise operations required for MD5.
The code has been tested with unit tests (see test folder) for various cases, but it must be used at your own risk. While functionality has been verified, unforeseen issues or limitations may arise in different configurations, software versions, or for different input strings. It is strongly recommended that the code be thoroughly tested in your own system before being used in production to ensure proper behavior. Any potential issues arising from its use are not the responsibility of the author.
Thanks for your interest in contributing. Anybody is free to report bugs, unclear documentation, and other problems regarding this repository in the Issues section or, even better, is free to propose any changes to this repository using Merge Requests.
Please read the Legal information