New password generation
up vote
2
down vote
favorite
public static GeneratePassword(minPassLength) {
let small = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split(' ');
let big = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".split(' ');
let numbers = "0 1 2 3 4 5 6 7 8 9".split(' ');
let special = "! " # $ % & ( ) * + - . : ; < = > ? @ [ ] _ { | }".split(' ');
var pass = "";
for (let i = 0; i < minPassLength; i++) {
pass += small[this.randomIntFromInterval(0, small.length - 1)];
}
for (let i = 0; i < minPassLength / 4; i++) {
pass += big[this.randomIntFromInterval(0, big.length - 1)];
}
for (let i = 0; i < minPassLength / 4; i++) {
pass += numbers[this.randomIntFromInterval(0, numbers.length - 1)];
}
for (let i = 0; i < minPassLength / 4; i++) {
pass += special[this.randomIntFromInterval(0, special.length - 1)];
}
pass = pass.split('').sort(function () { return 0.5 - Math.random() }).join('');
return pass;
}
private static randomIntFromInterval(min, max) // min and max included
{
return Math.floor(Math.random() * (max - min + 1) + min);
}
A simple function to generate a new password with at least X (this is function parameter) chars including 1 special, 1 number and 1 uppercase.
Please a code review :-)
strings random typescript
New contributor
add a comment |
up vote
2
down vote
favorite
public static GeneratePassword(minPassLength) {
let small = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split(' ');
let big = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".split(' ');
let numbers = "0 1 2 3 4 5 6 7 8 9".split(' ');
let special = "! " # $ % & ( ) * + - . : ; < = > ? @ [ ] _ { | }".split(' ');
var pass = "";
for (let i = 0; i < minPassLength; i++) {
pass += small[this.randomIntFromInterval(0, small.length - 1)];
}
for (let i = 0; i < minPassLength / 4; i++) {
pass += big[this.randomIntFromInterval(0, big.length - 1)];
}
for (let i = 0; i < minPassLength / 4; i++) {
pass += numbers[this.randomIntFromInterval(0, numbers.length - 1)];
}
for (let i = 0; i < minPassLength / 4; i++) {
pass += special[this.randomIntFromInterval(0, special.length - 1)];
}
pass = pass.split('').sort(function () { return 0.5 - Math.random() }).join('');
return pass;
}
private static randomIntFromInterval(min, max) // min and max included
{
return Math.floor(Math.random() * (max - min + 1) + min);
}
A simple function to generate a new password with at least X (this is function parameter) chars including 1 special, 1 number and 1 uppercase.
Please a code review :-)
strings random typescript
New contributor
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
public static GeneratePassword(minPassLength) {
let small = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split(' ');
let big = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".split(' ');
let numbers = "0 1 2 3 4 5 6 7 8 9".split(' ');
let special = "! " # $ % & ( ) * + - . : ; < = > ? @ [ ] _ { | }".split(' ');
var pass = "";
for (let i = 0; i < minPassLength; i++) {
pass += small[this.randomIntFromInterval(0, small.length - 1)];
}
for (let i = 0; i < minPassLength / 4; i++) {
pass += big[this.randomIntFromInterval(0, big.length - 1)];
}
for (let i = 0; i < minPassLength / 4; i++) {
pass += numbers[this.randomIntFromInterval(0, numbers.length - 1)];
}
for (let i = 0; i < minPassLength / 4; i++) {
pass += special[this.randomIntFromInterval(0, special.length - 1)];
}
pass = pass.split('').sort(function () { return 0.5 - Math.random() }).join('');
return pass;
}
private static randomIntFromInterval(min, max) // min and max included
{
return Math.floor(Math.random() * (max - min + 1) + min);
}
A simple function to generate a new password with at least X (this is function parameter) chars including 1 special, 1 number and 1 uppercase.
Please a code review :-)
strings random typescript
New contributor
public static GeneratePassword(minPassLength) {
let small = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split(' ');
let big = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".split(' ');
let numbers = "0 1 2 3 4 5 6 7 8 9".split(' ');
let special = "! " # $ % & ( ) * + - . : ; < = > ? @ [ ] _ { | }".split(' ');
var pass = "";
for (let i = 0; i < minPassLength; i++) {
pass += small[this.randomIntFromInterval(0, small.length - 1)];
}
for (let i = 0; i < minPassLength / 4; i++) {
pass += big[this.randomIntFromInterval(0, big.length - 1)];
}
for (let i = 0; i < minPassLength / 4; i++) {
pass += numbers[this.randomIntFromInterval(0, numbers.length - 1)];
}
for (let i = 0; i < minPassLength / 4; i++) {
pass += special[this.randomIntFromInterval(0, special.length - 1)];
}
pass = pass.split('').sort(function () { return 0.5 - Math.random() }).join('');
return pass;
}
private static randomIntFromInterval(min, max) // min and max included
{
return Math.floor(Math.random() * (max - min + 1) + min);
}
A simple function to generate a new password with at least X (this is function parameter) chars including 1 special, 1 number and 1 uppercase.
Please a code review :-)
strings random typescript
strings random typescript
New contributor
New contributor
edited Nov 13 at 15:22
200_success
127k15148410
127k15148410
New contributor
asked Nov 13 at 15:04
jdimko
111
111
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
You've four for-loops that are doing the same with different input. Make a function for this logic and call it with the different arrays.
I don't know how typescript handles it and how often you call this function, but it could be better to define your characters outside of this because generating them every time you call this function is not necessary.
Besides, you don't need to define your alphabet twice. You can use toUpperCase/toLowerCase.
And last: From an outsiders perspective, I have no idea what this code is for:
return 0.5 - Math.random()
You should make a function for it or at the very least, write a comment for it.
The0.5 - Math.random()
is a basic (not very good) way to achieve string shuffle
– Peter B
Nov 13 at 16:41
add a comment |
up vote
0
down vote
Not sure about typescript, but I think you don't need 4 for loops. Not tested but here is what i came up with on the go:
public static GeneratePassword(minPassLength) {
let small = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split(' ');
let big = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".split(' ');
let numbers = "0 1 2 3 4 5 6 7 8 9".split(' ');
let special = "! " # $ % & ( ) * + - . : ; < = > ? @ [ ] _ { | }".split(' ');
var pass = "";
if (minPassLength % 2 == 1){
minPassLength++;
}
for (let i = 0; i < minPassLength/4; i++) {
appendPass (pass, small[this.randomIntFromInterval(0, small.length - 1)]);
appendPass (pass, big[this.randomIntFromInterval(0, big.length - 1)]);
appendPass (pass, numbers[this.randomIntFromInterval(0, numbers.length - 1)]);
appendPass (pass, special[this.randomIntFromInterval(0, special.length - 1)]);
}
pass = pass.split('').sort(function () { return 0.5 - Math.random() }).join('');
return pass;
}
private static randomIntFromInterval(min, max) // min and max included
{
return Math.floor(Math.random() * (max - min + 1) + min);
}
private static appendPass(passwrd, interval)
{
passwrd += interval;
}
New contributor
Thanks for your reply
– jdimko
Nov 14 at 8:23
You're welcome.. please mark it as answered if it helped
– DrDev
Nov 14 at 9:23
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You've four for-loops that are doing the same with different input. Make a function for this logic and call it with the different arrays.
I don't know how typescript handles it and how often you call this function, but it could be better to define your characters outside of this because generating them every time you call this function is not necessary.
Besides, you don't need to define your alphabet twice. You can use toUpperCase/toLowerCase.
And last: From an outsiders perspective, I have no idea what this code is for:
return 0.5 - Math.random()
You should make a function for it or at the very least, write a comment for it.
The0.5 - Math.random()
is a basic (not very good) way to achieve string shuffle
– Peter B
Nov 13 at 16:41
add a comment |
up vote
1
down vote
You've four for-loops that are doing the same with different input. Make a function for this logic and call it with the different arrays.
I don't know how typescript handles it and how often you call this function, but it could be better to define your characters outside of this because generating them every time you call this function is not necessary.
Besides, you don't need to define your alphabet twice. You can use toUpperCase/toLowerCase.
And last: From an outsiders perspective, I have no idea what this code is for:
return 0.5 - Math.random()
You should make a function for it or at the very least, write a comment for it.
The0.5 - Math.random()
is a basic (not very good) way to achieve string shuffle
– Peter B
Nov 13 at 16:41
add a comment |
up vote
1
down vote
up vote
1
down vote
You've four for-loops that are doing the same with different input. Make a function for this logic and call it with the different arrays.
I don't know how typescript handles it and how often you call this function, but it could be better to define your characters outside of this because generating them every time you call this function is not necessary.
Besides, you don't need to define your alphabet twice. You can use toUpperCase/toLowerCase.
And last: From an outsiders perspective, I have no idea what this code is for:
return 0.5 - Math.random()
You should make a function for it or at the very least, write a comment for it.
You've four for-loops that are doing the same with different input. Make a function for this logic and call it with the different arrays.
I don't know how typescript handles it and how often you call this function, but it could be better to define your characters outside of this because generating them every time you call this function is not necessary.
Besides, you don't need to define your alphabet twice. You can use toUpperCase/toLowerCase.
And last: From an outsiders perspective, I have no idea what this code is for:
return 0.5 - Math.random()
You should make a function for it or at the very least, write a comment for it.
answered Nov 13 at 15:21
Synth
1165
1165
The0.5 - Math.random()
is a basic (not very good) way to achieve string shuffle
– Peter B
Nov 13 at 16:41
add a comment |
The0.5 - Math.random()
is a basic (not very good) way to achieve string shuffle
– Peter B
Nov 13 at 16:41
The
0.5 - Math.random()
is a basic (not very good) way to achieve string shuffle– Peter B
Nov 13 at 16:41
The
0.5 - Math.random()
is a basic (not very good) way to achieve string shuffle– Peter B
Nov 13 at 16:41
add a comment |
up vote
0
down vote
Not sure about typescript, but I think you don't need 4 for loops. Not tested but here is what i came up with on the go:
public static GeneratePassword(minPassLength) {
let small = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split(' ');
let big = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".split(' ');
let numbers = "0 1 2 3 4 5 6 7 8 9".split(' ');
let special = "! " # $ % & ( ) * + - . : ; < = > ? @ [ ] _ { | }".split(' ');
var pass = "";
if (minPassLength % 2 == 1){
minPassLength++;
}
for (let i = 0; i < minPassLength/4; i++) {
appendPass (pass, small[this.randomIntFromInterval(0, small.length - 1)]);
appendPass (pass, big[this.randomIntFromInterval(0, big.length - 1)]);
appendPass (pass, numbers[this.randomIntFromInterval(0, numbers.length - 1)]);
appendPass (pass, special[this.randomIntFromInterval(0, special.length - 1)]);
}
pass = pass.split('').sort(function () { return 0.5 - Math.random() }).join('');
return pass;
}
private static randomIntFromInterval(min, max) // min and max included
{
return Math.floor(Math.random() * (max - min + 1) + min);
}
private static appendPass(passwrd, interval)
{
passwrd += interval;
}
New contributor
Thanks for your reply
– jdimko
Nov 14 at 8:23
You're welcome.. please mark it as answered if it helped
– DrDev
Nov 14 at 9:23
add a comment |
up vote
0
down vote
Not sure about typescript, but I think you don't need 4 for loops. Not tested but here is what i came up with on the go:
public static GeneratePassword(minPassLength) {
let small = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split(' ');
let big = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".split(' ');
let numbers = "0 1 2 3 4 5 6 7 8 9".split(' ');
let special = "! " # $ % & ( ) * + - . : ; < = > ? @ [ ] _ { | }".split(' ');
var pass = "";
if (minPassLength % 2 == 1){
minPassLength++;
}
for (let i = 0; i < minPassLength/4; i++) {
appendPass (pass, small[this.randomIntFromInterval(0, small.length - 1)]);
appendPass (pass, big[this.randomIntFromInterval(0, big.length - 1)]);
appendPass (pass, numbers[this.randomIntFromInterval(0, numbers.length - 1)]);
appendPass (pass, special[this.randomIntFromInterval(0, special.length - 1)]);
}
pass = pass.split('').sort(function () { return 0.5 - Math.random() }).join('');
return pass;
}
private static randomIntFromInterval(min, max) // min and max included
{
return Math.floor(Math.random() * (max - min + 1) + min);
}
private static appendPass(passwrd, interval)
{
passwrd += interval;
}
New contributor
Thanks for your reply
– jdimko
Nov 14 at 8:23
You're welcome.. please mark it as answered if it helped
– DrDev
Nov 14 at 9:23
add a comment |
up vote
0
down vote
up vote
0
down vote
Not sure about typescript, but I think you don't need 4 for loops. Not tested but here is what i came up with on the go:
public static GeneratePassword(minPassLength) {
let small = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split(' ');
let big = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".split(' ');
let numbers = "0 1 2 3 4 5 6 7 8 9".split(' ');
let special = "! " # $ % & ( ) * + - . : ; < = > ? @ [ ] _ { | }".split(' ');
var pass = "";
if (minPassLength % 2 == 1){
minPassLength++;
}
for (let i = 0; i < minPassLength/4; i++) {
appendPass (pass, small[this.randomIntFromInterval(0, small.length - 1)]);
appendPass (pass, big[this.randomIntFromInterval(0, big.length - 1)]);
appendPass (pass, numbers[this.randomIntFromInterval(0, numbers.length - 1)]);
appendPass (pass, special[this.randomIntFromInterval(0, special.length - 1)]);
}
pass = pass.split('').sort(function () { return 0.5 - Math.random() }).join('');
return pass;
}
private static randomIntFromInterval(min, max) // min and max included
{
return Math.floor(Math.random() * (max - min + 1) + min);
}
private static appendPass(passwrd, interval)
{
passwrd += interval;
}
New contributor
Not sure about typescript, but I think you don't need 4 for loops. Not tested but here is what i came up with on the go:
public static GeneratePassword(minPassLength) {
let small = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split(' ');
let big = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".split(' ');
let numbers = "0 1 2 3 4 5 6 7 8 9".split(' ');
let special = "! " # $ % & ( ) * + - . : ; < = > ? @ [ ] _ { | }".split(' ');
var pass = "";
if (minPassLength % 2 == 1){
minPassLength++;
}
for (let i = 0; i < minPassLength/4; i++) {
appendPass (pass, small[this.randomIntFromInterval(0, small.length - 1)]);
appendPass (pass, big[this.randomIntFromInterval(0, big.length - 1)]);
appendPass (pass, numbers[this.randomIntFromInterval(0, numbers.length - 1)]);
appendPass (pass, special[this.randomIntFromInterval(0, special.length - 1)]);
}
pass = pass.split('').sort(function () { return 0.5 - Math.random() }).join('');
return pass;
}
private static randomIntFromInterval(min, max) // min and max included
{
return Math.floor(Math.random() * (max - min + 1) + min);
}
private static appendPass(passwrd, interval)
{
passwrd += interval;
}
New contributor
New contributor
answered Nov 13 at 16:57
DrDev
1014
1014
New contributor
New contributor
Thanks for your reply
– jdimko
Nov 14 at 8:23
You're welcome.. please mark it as answered if it helped
– DrDev
Nov 14 at 9:23
add a comment |
Thanks for your reply
– jdimko
Nov 14 at 8:23
You're welcome.. please mark it as answered if it helped
– DrDev
Nov 14 at 9:23
Thanks for your reply
– jdimko
Nov 14 at 8:23
Thanks for your reply
– jdimko
Nov 14 at 8:23
You're welcome.. please mark it as answered if it helped
– DrDev
Nov 14 at 9:23
You're welcome.. please mark it as answered if it helped
– DrDev
Nov 14 at 9:23
add a comment |
jdimko is a new contributor. Be nice, and check out our Code of Conduct.
jdimko is a new contributor. Be nice, and check out our Code of Conduct.
jdimko is a new contributor. Be nice, and check out our Code of Conduct.
jdimko is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207565%2fnew-password-generation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown