Get MD5 Hash

GetMD5Hash

The GetMD5Hash procedure returns the MD5 for the input data as a 32-byte hexadecimal  (16-byte) value. The MD5 Hash is often stored in database in place of pass phrases or passwords. Then when the user enters the password or pass phrase, the MD5 Hash is recalculated from the user-entered data and compared with the MD5 Hash stored in the database.

The MD5 Hash is a 16-byte value. By default the MD5 Hash is determined using the ASCII character set (CCSID 819). To return the MD5 Hash in another character set, specify the CCSID value for the desired character set as the second parameter.

NOTE:  See RFC 1321 for information on the MD5 hash

32-char MD5 Hash  =  GetMD5Hash(
  szCipherData      32766A    Const Varying
  nCCSID               10I 0  Const OPTIONS(*NOPASS:*OMIT)
  bToHex                1N    Const OPTIONS(*NOPASS:*OMIT)
)

See also: GetSecHash

Parameters

szCipherData
[input VChar(*) const]  The original text that is used to create the MD5 "message digest".  The 16-byte "signature", known as the "message digest" is returned after processing the value specified for this parameter with the MD5 Hash algorithm.
nCCSID  [optional] DFT(819)
[input int(4) const]  This parameter identifies the CCSID of the return value. The text specified on the szCipherData parameter is convert to this CCSID and then processed by the MD5 Hash algorithm. If the parameter is not specified, the default is US ASCII CCSID 819. Specifying *OMIT or -1 for this parameter also causes the default CCSID of 819 to be used. 
If this parameter is 0, the CCSID of the job is used. That is, no conversion of the input value is performed when CCSID(0) is specified.
bToHex  [optional] DFT(*OFF)
[input Indy const]  This parameter indicates whether or not the MD5 Hash is returned as hexadecimal (32-bytes) or character (16-bytes).  If *OFF or '0' is specified, or the parameter is not specified or specified as *OMIT, then the MD5 Hash is returned as a 16-byte value. If *ON, or '1' is specified, the MD5 Hash is converted from its 16-byte character format, into a 32-byte hexadecimal value and returned.

Return Value

If the function succeeds, the return value is a 16-byte MD5 "message digest" or the 32-byte hexadecimal MD5 "message digest". The return value is in the CCSID specified for the second parameter of the procedure, or PC ASCII (CCSID 819) if unspecified. If the return value is to be converted to hexadecimal format, the actual characters are EBCDIC characters in the CCSID of the job, but the hexadecimal character pairs themselves represent the CCSID specified on the nCCSID parameter (for example, US-ASCII CCSID 819).

Example 1 - Retrieve MD5 Hash

 
      /INCLUDE QCPYSRC,security

     D PassPhrase      S            128A   Inz('The quick brown fox jumped +   

     D                                          over the lazy dog.')

     D MD5Hash         S             16A   

 

     C                   eval      MD5hash=GetMD5Hash(%TrimR(PassPhrase))

As a result of the call to GetMD5Hash(), the field MD5Hash contains the "message digest" for the text stored in the PassPhrase field. This 16-byte value is unique and may be stored in a database file for comparison later on.

Example 2 - Retrieve MD5 Hash as a Hex String

 
      /INCLUDE QCPYSRC,security

     D PassPhrase      S            128A   Inz('The quick brown fox jumped +   

     D                                          over the lazy dog.')

     D MD5Hash         S             32A   

 

     C                   eval      MD5hash=GetMD5Hash(%TrimR(PassPhrase) :

     C                                                 -1 : *ON)

 

As a result of the call to GetMD5Hash() in this example, the 16-byte MD5 Hash is returned after converting it to hexadecimal. Therefore the length of the return value is 32-bytes. The return value will be the following:

"5c6ffbdd40d9556b73a21e63c3e0e904"

There is an example program in the QRPGEXTRA source file, shipped with the RPG xTools, named MD5PROOF. This source member, when compiled and run, is a program that generates the MD5 Hash for the so called MD5 Proof of accuracy values. The output from calling the MD5Proof program will be as follows:

call md5proof
"The quick brown fox jumped over the lazy dog." - "5c6ffbdd40d9556b73a21e63c3e0e904"
"" - "d41d8cd98f00b204e9800998ecf8427e"
"a" - "0cc175b9c0f1b6a831c399e269772661"
"abc" - "900150983cd24fb0d6963f7d28e17f72"
"message digest" - "f96b697d7cb7938d525a2f31aaf161d0"
"abcdefghijklmnopqrstuvwxyz" - "c3fcd3d76192e4007dfb496cca67e13b"
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 - "d174ab98d277d9f5a5611c2c9f419d9f"
"12345678901234567890123456789012345678901234567890123456789012345678901234567890" - "57edf4a22be3c955ac49da2e2107b67a"