Skip to content

Commit

Permalink
Added custom function to token gen
Browse files Browse the repository at this point in the history
Function syntax: `string generate_custom_token(int token_length, string characters);`

* `generate_token` function now uses the custom function to generate the characters depending on the mode.
  • Loading branch information
harrymkt authored and braillescreen committed Sep 13, 2024
1 parent fa78826 commit fa13ae0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Token generation include
Allows you to easily generate random strings of characters of any length in a given mode.
Allows you to easily generate random strings of characters of any length in a given mode, and possibly custom function if you want to generate only certain characters.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# token_gen_flag
This enum holds various constants that can be passed to the mode parameter in order to change how tokens are generated.
This enum holds various constants that can be passed to the mode parameter in order to change how tokens are generated in `generate_token` function.

* token_gen_flag_all: Uses all characters, numbers and symbols, see below.
* token_gen_flag_characters: Uses only characters, a-z, A-Z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
## returns:
String: a random token depending on the mode.
## Remarks:
The characters used to generate the token will depend on the mode you set. See `token_gen_flags` enum constants.
This function uses `generate_custom_token` function to generate. The characters used to generate the token will depend on the mode you set. See `token_gen_flags` enum constants.
*/

// Example:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
Generates a string of random characters, or token base on the characters you set.
string generate_custom_token(int token_length, characters);
## Arguments:
* int token_length: the length of the token to generate.
* string characters: a list of characters to generate.
## returns:
String: a random token.
## Remarks:
If characters list is empty or token length is set to 0 or less, an empty string is returned.
*/

// Example:
#include "token_gen.nvgt"

void main() {
alert("Info", "Your A to C token is: " + generate_custom_token(10, "abc"));
alert("Info", "A to C with capitals included token is: " + generate_custom_token(10, "abcABC"));
}
11 changes: 7 additions & 4 deletions release/include/token_gen.nvgt
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ string generate_token(int token_length, int mode = token_gen_flag_all) {
else if (mode == token_gen_flag_symbols) token_sims = symbols;
else if (mode == token_gen_flag_numbers_symbols) token_sims = numbers + symbols;
else if (mode == token_gen_flag_characters_symbols) token_sims = chars + symbols;

return generate_custom_token(token_length, token_sims);
}
string generate_custom_token(int token_length, string characters) {
if (characters.empty() || token_length <= 0) return "";
string final_token;
for (uint i = 0; i < token_length; i++)
final_token += token_sims[random(0, token_sims.length() - 1)];
return final_token;
}
final_token += characters[random(0, characters.length() - 1)];
return final_token;
}

0 comments on commit fa13ae0

Please sign in to comment.