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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • MuleSoft OAuth 2.0 Provider: Password Grant Type
  • Solving Parallel Writing Issues in MuleSoft With Distributed Locking
  • Optimizing MuleSoft Performance With HikariCP: A Complete Guide
  • Generic and Dynamic API: MuleSoft

Trending

  • Prioritizing Cloud Security Risks: A Developer's Guide to Tackling Security Debt
  • Optimizing Serverless Computing with AWS Lambda Layers and CloudFormation
  • Next Evolution in Integration: Architecting With Intent Using Model Context Protocol
  • ITBench, Part 1: Next-Gen Benchmarking for IT Automation Evaluation
  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 user avatar
Saddam Shaikh
·
Feb. 12, 22 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
5.8K 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.

Related

  • MuleSoft OAuth 2.0 Provider: Password Grant Type
  • Solving Parallel Writing Issues in MuleSoft With Distributed Locking
  • Optimizing MuleSoft Performance With HikariCP: A Complete Guide
  • Generic and Dynamic API: MuleSoft

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!