Generate Random AlphaNumeric string in Java and RPGLE

Sometimes there is need for unique Random AlphaNumeric string to be used in applications where you have to generate a sessionId, unique file names, etc. Here is sample code both in Java and then in RPGLE for IBM i programmers to use.

You can add the string generated here with current Date and Time also !
$Random_       = %char(%Date():*ISO0) + '_' +       
                 %char(%Time():*HMS0) + '_' +       
                 $Random_String;      

Sample Java source code

package com.mysample;

import java.math.BigInteger;
import java.security.SecureRandom;

public class GenerateRandomString {

    public String getString() {

        SecureRandom random = new SecureRandom();
        String randomString = new BigInteger(130, random).toString(32);
        return(randomString);
    }

}

Sample RPGLE source code for Random AlphaNumeric String

h DftActGrp(*no) ActGrp(*Caller)                                      
                                                                      
d ExcCmd          PR                  ExtPgm('QCMDEXC')               
d  Cmd_Str                     512a   Options(*VARSIZE)               
d                                     Const                           
d  Cmd_Len                      15p 5 Const                           
                                                                      
d $CmdStr         s            512a   inz                             
d $Random_String  s             32a                                   
d @Random_String  s               o   Class(*JAVA:'java.lang.String') 
                                                                      
 * Prototype for Java String Object                                   
d crtString       PR              o   EXTPROC(*JAVA:                  
d                                             'java.lang.String':     
d                                             *CONSTRUCTOR)           
d RPGBytes                    5000A   Const Varying                   
                                                                      
 * Prototype for Java String's getBytes method                        
d cvtToBytes      PR            50A   EXTPROC(*JAVA:                  
d                                             'java.lang.String':     
d                                             'getBytes')             
d                                     Varying                         
                                                                      
* Prototype for GenerateRandomString Object                               
d GenRandom       PR              o   EXTPROC(*JAVA:                       
d                                     'com.mysample.GenerateRandomString': 
d                                      *CONSTRUCTOR)                       
                                                                           
 * GenerateRandomString Object                                             
d GenRandom_      s               o   Class(*JAVA:                         
d                                     'com.mysample.GenerateRandomString') 
                                                                           
 * Prototype for GenerateRandomString's getString Method                   
d getString       PR              o   EXTPROC(*JAVA:                       
d                                     'com.mysample.GenerateRandomString': 
d                                     'getString')                         
d                                     Class(*JAVA:'java.lang.String')      
 *                                                                         
 *----- Main Routine                                                       
 *                                                                         
 /free                                                                     
                                                                           
      GenRandom_ = GenRandom();                                            
      @Random_String = getString(GenRandom_);                              
      $Random_String = cvtToBytes(@Random_String);                         
      dsply $Random_String;                                               
                                                                      
    *inlr = *on;                                                      
    return;                                                           
                                                                      
 /end-free                                                            
                                                                      
 *                                                                    
 *----- Initial Routine                                               
 *                                                                    
c     *inzsr        begsr                                             
                                                                      
c     *entry        plist                                             
c                   parm                    $Random_String            
                                                                      
c                   eval      $CmdStr = 'CD DIR(''/directory/folder'')'
c                   callp     ExcCmd(%Trim($CmdStr) :                 
c                                   %Len(%Trim($CmdStr)))             
                                                                      
c                   endsr  

Recommended Reading

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.