Return to the RPG Tips
MD5 via RPG
Here's some sample RPG code which takes into consideration the EBCDIC to ASCII conversion, the hash generation, and the creation of a suitable text string. The program assumes that EBCDIC means CCSID 37 and ASCII CCSID 819. These assumptions may not hold true for all languages. Bruce H DFTACTGRP(*NO) ACTGRP('QILE') BNDDIR('QC2LE') DCipher PR EXTPROC('_CIPHER') D * VALUE D * VALUE D * VALUE DConvert PR EXTPROC('_XLATEB') D * VALUE D * VALUE D 10u 0 VALUE Dcvthc PR EXTPROC('cvthc') D 1 D 1 D 10i 0 VALUE DControls DS D Function 5i 0 inz(5) D HashAlg 1 inz(x'00') D Sequence 1 inz(x'00') D DataLngth 10i 0 inz(15) D Unused 8 inz(*LOVAL) D HashCtxPtr * inz(%addr(HashWorkArea)) DHashWorkArea S 96 inz(*LOVAL) DMsg S 50 DReceiverHex S 16 DReceiverPtr S * inz(%addr(ReceiverHex)) DReceiverChr S 32 DSourcePtr S * inz(%addr(Msg)) DStartMap s 256 DTo819 s 256 DCCSID1 s 10i 0 inz(37) DST1 s 10i 0 inz(0) DL1 s 10i 0 inz(%size(StartMap)) DCCSID2 s 10i 0 inz(819) DST2 s 10i 0 inz(0) DGCCASN s 10i 0 inz(0) DL2 s 10i 0 inz(%size(To819)) DL3 s 10i 0 DL4 s 10i 0 DFB s 12 D ds D x 5i 0 D LowX 2 2 D* Get all single byte ebcdic hex values C 0 do 255 x C eval %subst(StartMap:x+1:1) = LowX C enddo C* Get conversion table for 819 from 37 C call 'QTQCVRT' C parm CCSID1 C parm ST1 C parm StartMap C parm L1 C parm CCSID2 C parm ST2 C parm GCCASN C parm L2 C parm To819 C parm L3 C parm L4 C parm FB C* Set message text C eval Msg = 'message digest' C eval DataLngth = %len(%trimr(Msg)) C* Now Change Msg to 819 from 37 using MI C callp Convert( %addr(Msg) C :%addr(To819) C :%size(Msg)) C* Get MD5 for Msg C callp Cipher( %addr(ReceiverPtr) C :%addr(Controls) C :%addr(SourcePtr)) C* Convert nibbles to characters C callp cvthc( ReceiverChr C :ReceiverHex C :%size(ReceiverChr)) C* Display the "proof" C ReceiverChr dsply C eval *INLR = '1' C return
[report a broken link by clicking here]