Blog Archive

RPGLE generate SHA-1 Hash - Use Qc3CalculateHash, QDCXLATE to Hash MD5 SHA1 SHA256 SHA384 SHA512

In cryptography, SHA-1 is a cryptographic hash function designed by the National Security Agency and published by the NIST as a U.S. Federal Information Processing Standard. SHA stands for Secure Hash Algorithm. SHA-1 is the most widely used of the existing SHA hash functions, and is employed in several widely-used security applications and protocols.

SHA-1 produces a 160-bit message digest based on principles similar to those used by Ronald L. Rivest of MIT in the design of the MD4 and MD5 message digest algorithms, but has a more conservative design.

Example SHA-1 hashes

These are examples of SHA-1 digests. ASCII encoding is used for all messages.

SHA1("The quick brown fox jumps over the lazy dog")
= 2fd4e1c6 7a2d28fc ed849ee1 bb76e739 1b93eb12

The hash of the zero-length string is:
SHA1("") = da39a3ee 5e6b4b0d 3255bfef 95601890 afd80709

RPGLE Sample Code to Generate SHA-1 Hash

You can also use it to HASH MD5, SHA256, SHA384, SHA512
h DFTACTGRP(*NO) ACTGRP('AS') BNDDIR('QC2LE') debug                  
d Qc3CalculateHash...                                                     
d                 PR                  ExtProc('Qc3CalculateHash')         
d   InData                        *   value                               
d   IndataL                     10i 0 const                               
d   InDataF                      8a   const                               
d   AlgoDes                     16a   const                               
d   AlgoFmt                      8a   const                               
d   CryptoSP                     1a   const                               
d   CryptoDev                    1a   const options(*omit)                
d   Hash                        64a   options(*varsize:*omit)             
d   ErrorCode                32767a   options(*varsize)                   
d ALGD0500_t      ds                  qualified                           
d                                     based(Template)                     
d   HashAlg                     10i 0                                     
d QDCXLATE        PR                  ExtPgm('QDCXLATE')                  
d   len                          5p 0 const                               
d   data                     32702a   options(*varsize)         
d   table                       10a   const                     
d cvthc           PR                  ExtProc('cvthc')          
d   target                   65534A   options(*varsize)         
d   src_bits                 32767A   options(*varsize) const   
d   tgt_length                  10I 0 value                     
d ErrorNull       ds                  qualified                 
d    BytesPro                   10i 0 inz(0)                    
d    BytesAvai                  10i 0 inz(0)                    
d HASH_MD5        c                   1
d HASH_SHA1       c                   2
d HASH_SHA256     c                   3
d HASH_SHA384     c                   4
d HASH_SHA512     c                   5
d data            s           2000A                             
d len             s             10i 0                           
d alg             ds                  likeds(ALGD0500_t)        
d bin             s             20a 
d $hex            s             40a 
      len = %len(%trimr(data));              
      alg.HashAlg = HASH_SHA1; 
   //Set the HASH Algorithm you want to use !       
   //Convert from EBCDIC to ASCII (skip this step if you want Hash in EBCDIC)         
      QDCXLATE(len: data: 'QTCPASC'); 
   //API to calculate the SHA1 hash
      Qc3CalculateHash( %addr(data)          
                      : len                  
                      : 'DATA0100'           
                      : alg                  
                      : 'ALGD0500'           
                      : '0'                  
                      : *OMIT                
                      : bin                  
                      : ErrorNull );         
      //Convert to HEX                                      
      cvthc( $hex: bin: %len(bin)*2);
   dsply $hex; 
      *inlr = *on;                           
This program uses the Qc3CalculateHash API to calculate the SHA1 hash. It then uses the cvthc() MI function to create a hex representation of the hash to display on the screen. The QDCXLATE API is used to convert the hash from EBCDIC to ASCII.