DZone
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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Code and Connect: MCP + MuleSoft
  • MuleSoft MCP and A2A in Production: What 17 Recipes Reveal
  • MuleSoft IDP: Enhancing Efficiency and Accuracy in Data Extraction
  • From AI Chaos to Control: Building Enterprise-Grade LLM Gateways With MuleSoft Anypoint

Trending

  • Using LLMs to Generate Unit Tests and Review Pull Requests in Spring Boot
  • Crash-Proof Agents: Durable Execution for Long-Running AI Systems
  • Idempotent Consumers: Preventing Stale Retries With Version-Fenced Writes
  • Measuring AI Impact at the Team Level

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. 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.

By 
Saddam Shaikh
·
Feb. 12, 22 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
6.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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.

Related

  • Code and Connect: MCP + MuleSoft
  • MuleSoft MCP and A2A in Production: What 17 Recipes Reveal
  • MuleSoft IDP: Enhancing Efficiency and Accuracy in Data Extraction
  • From AI Chaos to Control: Building Enterprise-Grade LLM Gateways With MuleSoft Anypoint

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook