You are on page 1of 3

Random Password Generator

SDN Community Contribution


(This is not an official SAP document.)

Disclaimer & Liability Notice


This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade. SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document, and anyone using these methods does so at his/her own risk. SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or code sample, including any liability resulting from incompatibility between the content within this document and the materials and services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this document.

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

Random Password Generator

Applies To:
SAP NetWeaver Web AS, BSP

Summary
This function module (FM) will generate a random password with a length specified via the import parameter. The FM will return a random password containing case sensitive (alpha)-numeric characters. For detailed information, see my weblog at: https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/2584. By: Eddy De Clercq Company: Katholieke Universiteit Leuven Date: 26 October 2005

Code
FUNCTION z_generate_pwd. *"---------------------------------------------------------------------*"*"Local interface: *" *" *" *" IMPORTING REFERENCE(CHARS) TYPE EXPORTING REFERENCE(PWD) TYPE STRING I

*"---------------------------------------------------------------------DATA: a_sign TYPE x, xpwd TYPE xstring, conv TYPE REF TO cl_abap_conv_in_ce, prnga TYPE REF TO cl_abap_random_int, prngb TYPE REF TO cl_abap_random_int, prngc TYPE REF TO cl_abap_random_int, prngd TYPE REF TO cl_abap_random_int, seed TYPE i, random TYPE i.

seed = cl_abap_random=>seed( ). prnga = cl_abap_random_int=>create( seed = seed min = 1 max = 3 ). seed = cl_abap_random=>seed( ). prngb = cl_abap_random_int=>create( seed = seed min = 48 max = 57 ). seed = cl_abap_random=>seed( ). prngc = cl_abap_random_int=>create( seed = seed min = 65 max = 90 ). seed = cl_abap_random=>seed( ). prngd = cl_abap_random_int=>create( seed = seed min = 97 max = 122 ). 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 1

Random Password Generator

DO chars TIMES. random = prnga->get_next( ). CASE random. WHEN 1. a_sign = prngb->get_next( ). WHEN 2. a_sign = prngc->get_next( ). WHEN 3. a_sign = prngd->get_next( ). ENDCASE. CONCATENATE xpwd a_sign INTO xpwd IN BYTE MODE. ENDDO. conv = cl_abap_conv_in_ce=>create( input = xpwd ). conv->read( IMPORTING data = pwd ).

ENDFUNCTION.

Author Bio
Eddy De Clercq has 20 years experience in computing. He currently works at the Katholieke Universiteit Leuven, the oldest university of the Low Countries and the largest Flemish university. Eddy makes part of the E-university team that creates mostly self services (web) applications.

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

You might also like