Password Generation Using Dataweave in MuleSoft
In this article, we will explore how to generate a password using Dataweave in MuleSoft.
Join the DZone community and get the full member experience.
Join For FreeIn 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
%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.
Opinions expressed by DZone contributors are their own.
Comments