0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

JavaScriptでパスワード生成

Last updated at Posted at 2025-01-12

やりたいこと

記号大文字小文字数字を含むパスワード生成

mkpass.html
<form name="makepass">
	<input type="button" value="exec" onclick="makePass()">
	<input type="text" size="1" id="passcount" style="text-align:center" value="10">
	<input type="checkbox" id="kigou" checked>記号あり
	<input type="text" size="15"  name="pass" readonly="readonly">
</form>

<script type="text/javascript">

window.onload = function () {
	makePass();
}

function makePass() {
	const passLen = document.makepass.passcount.value;
	let pass = "";
	const lower = "abcdefghijklmnopqrstuvwxyz";
	const upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	const num = "0123456789";
	const kigou = "!#$%&=/*-+^{}()";
	let kigoucount,lowercount,uppercount;
	let aryIndex = [];
	let aryKigouNum = [];
	let aryLowerNum = [];
	let aryUpperNum = [];
	
	const checkKigou = document.makepass.kigou.checked;
	if (checkKigou == true) {
		kigoucount = Math.floor(Math.random() * ( passLen - 3 ) + 1); //kigoucountにパスワード長から他文字最低1引いた数ランダムな数値(1以上)を格納する
	}else {
		kigoucount = 0;
	}
	aryIndex = [...Array(Number(passLen))].map((_, i) => i); //aryIndexにパスワード長のインデックス(0~)を格納する
	lowercount = Math.floor(Math.random() * ( passLen - kigoucount -2 ) + 1);
	uppercount = Math.floor(Math.random() * ( passLen - kigoucount - lowercount -1 ) + 1);
	for(let i=0; i < kigoucount; i++){
		aryKigouNum.push(aryIndex.splice(Math.floor(Math.random() * aryIndex.length), 1)); //aryIndexからkigoucount個のランダム番目のインデックス数値を取り出す
	}
	for(let i=0; i < lowercount; i++){
		aryLowerNum.push(aryIndex.splice(Math.floor(Math.random() * aryIndex.length), 1));
	}
	for(let i=0; i < uppercount; i++){
		aryUpperNum.push(aryIndex.splice(Math.floor(Math.random() * aryIndex.length), 1));
	}
	for(let i=0;i<passLen;i++){
		if (aryKigouNum.find(element => element == i)) { //passのaryKigouNum番目の位置に記号を入れる
			pass += kigou.charAt(Math.floor(Math.random() * kigou.length));
		}else if (aryLowerNum.find(element => element == i)) {
			pass += lower.charAt(Math.floor(Math.random() * lower.length));
		}else if (aryUpperNum.find(element => element == i)) {
			pass += upper.charAt(Math.floor(Math.random() * upper.length));
		}else {
			pass += num.charAt(Math.floor(Math.random() * num.length));
		}
	}
	console.log("aryKigouNum:" + aryKigouNum);	//aryKigouNum:3,9
	console.log("aryLowerNum:" + aryLowerNum);	//aryLowerNum:2,1
	console.log("aryUpperNum:" + aryUpperNum);	//aryUpperNum:8,6,0
	console.log("aryIndex:" + aryIndex);		//aryIndex:4,5,7
												//     0123456789
	console.log("pass:" + pass);				//pass:Usf)00N8S*
	document.makepass.pass.value = pass;
}

</script>

参考にさせていただいた

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?