How Do I … ? / Compute a hash value from a BDL string |
Then uses the security.Digest API to compute a XML digital signature for the content of the root node, or in other words the string you wish to hash, using a XPath expression.
And finally, retrieves the hash value from the signature and returns it.
IMPORT SECURITY MAIN DEFINE result STRING IF num_args() != 2 THEN DISPLAY "Usage: ComputeHash <string> <hashcode>" DISPLAY " string: the string to digest" DISPLAY " hashcode: SHA1, SHA512, SHA384, SHA256, SHA224, MD5" ELSE LET result = ComputeHash(arg_val(1), arg_val(2)) IF result IS NOT NULL THEN DISPLAY "Hash value is: ",result ELSE DISPLAY "Error" END IF END IF END MAIN FUNCTION ComputeHash(toDigest, algo) DEFINE toDigest, algo, result STRING DEFINE dgst security.Digest TRY LET dgst = security.Digest.CreateDigest(algo) CALL dgst.AddStringData(toDigest) LET result = dgst.DoBase64Digest() CATCH DISPLAY "ERROR : ", STATUS, " - ", SQLCA.SQLERRM EXIT PROGRAM(-1) END TRY RETURN result END FUNCTION
Example of usage:
$ fglrun ComputeHash "Hello, world !!!" SHA1
$ Hash value is: Ck1VqNd45QIvq3AZd8XYQLvEhtA=