DZone
Security Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Security Zone > Password Generation Using Dataweave in MuleSoft

Password Generation Using Dataweave in MuleSoft

In this article, we will explore how to generate a password using Dataweave in MuleSoft.

Saddam Shaikh user avatar by
Saddam Shaikh
·
Feb. 12, 22 · Security Zone · Tutorial
Like (3)
Save
Tweet
3.59K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, we will check how to generate a random password of a specific length that would have at least one digit, an uppercase character, a lowercase character, and a special symbol in the password using Dataweave in MuleSoft.

Importance of Passwords

Passwords provide the first line of defense against unauthorized access to devices and personal information. The stronger your password, the more protected your device will be from hackers and malicious software. 

A weak password can be easily guessed by executing a brute force attack, thus making your system vulnerable.

Dataweave 2.0 Code

JavaScript
 
%dw 2.0
output application/json  
import * from dw::Runtime

/*
 * Define permissible values for digits, upper case characters, lower case characters and special symbols in password.
 */

var digits = '0123456789'
var lowercase_char = 'abcdefghijklmnopqrstuvwxyz'
var uppercase_char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
var symbols = '@#\$'

/* 
 * Combine all the variables.
 */
var combined_list = digits ++ uppercase_char ++ lowercase_char ++ symbols

/* 
 * Find length of each variables.
 */
var digits_length = sizeOf(digits)
var lowercase_char_length = sizeOf(lowercase_char)
var uppercase_char_length = sizeOf(uppercase_char)
var symbols_length = sizeOf(symbols)
var combined_list_length = sizeOf(combined_list)

/* 
 * Declaring a function generate_password that takes maxLength parameter and return password of length maxLength.
 * This function will only work if maxLength is greater than 4.  
 */
fun generate_password(maxLength:Number) = 
  if (maxLength <= 4) 
     fail("Password length must be greater than 4")
  else (
      do {
            /*
             * Randomly select one value from each character set and combined them. This is to ensure that final password will have atleast one value from each set.
             * temp_pass variable will store 4 character of final password.
             */
            var random_digit = digits[digits_length * random()]
            var random_upper = uppercase_char[uppercase_char_length * random()]
            var random_lower = lowercase_char[lowercase_char_length * random()]
            var random_symbol = symbols[symbols_length * random()]
            var temp_pass = [random_digit ++ random_upper ++ random_lower ++ random_symbol]
        ---
            /*
             * Generate remaining characters of password and combine it with temp_pass variable.
             * Randomly shuffle the generated password using random() function to ensure that password is not folliwing any specific pattern. 
            */

            (((0 to maxLength - 5) as Array default [] 
            reduce ((item, acc = []) -> acc + combined_list[combined_list_length * random()]) ++ temp_pass) 
            orderBy random()) 
            joinBy ""
         }
       )

---
generate_password(6)


Let's Test the Dataweave Code

Success Case

If the maxLength parameter is greater than 4, the password is generated successfully.


Error Case

If the maxLength parameter is lower than 4, an error is thrown.

I hope this article will help you generate random passwords.

MuleSoft

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • To Shift Right, You Need Observability
  • Biometric Authentication: Best Practices
  • Container Orchestration Tools Comparison
  • API Security Tools: What To Look For

Comments

Security Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo