[miniProjects] 31_Password Generator

๋ณด๋ฆฌยท2023๋…„ 6์›” 20์ผ
0

miniProjects

๋ชฉ๋ก ๋ณด๊ธฐ
31/47

31_Password Generator

๐Ÿ’ป์ฃผ์ œ : ์ž๋™ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ƒ์„ฑ๊ธฐ

  • ๋น„๋ฐ€๋ฒˆํ˜ธ ๊ธธ์ด
  • ๋Œ€๋ฌธ์ž ํฌํ•จ
  • ์†Œ๋ฌธ์ž ํฌํ•จ
  • ์ˆซ์ž ํฌํ•จ
  • ๊ธฐํ˜ธ ํฌํ•จ
    ๐Ÿ‘๐Ÿป ๋‹ค์„ฏ๊ฐ€์ง€ ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•˜์—ฌ ๋ฌด์ž‘์œ„๋กœ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ƒ์„ฑ๋จ.

โœจjs

function getRandomLower() {
  return String.fromCharCode(Math.floor(Math.random()*26) + 97)
}

function getRandomUpper() {
  return String.fromCharCode(Math.floor(Math.random()*26) + 65)
}

function getRandomNumber() {
  return String.fromCharCode(Math.floor(Math.random()*10) + 48)
}

function getRandomSymbol() {
  const symbols = '!@#$%^&*(){}[]=<>/,.'
  return symbols[Math.floor(Math.random() * symbols.length)]
}

(1) getRandomLower

  • ์•ŒํŒŒ๋ฒณ์˜ ๊ฐœ์ˆ˜๋Š” 26๋ผ random * 26์ž„.
  • 97๋ถ€ํ„ฐ 122๊นŒ์ง€์˜ ์ˆ˜๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
    ์ด ์ˆซ์ž๋“ค์€ ์†Œ๋ฌธ์ž ์•ŒํŒŒ๋ฒณ 'a'๋ถ€ํ„ฐ 'z'๊นŒ์ง€์˜ Unicode ๊ฐ’์— ํ•ด๋‹นํ•œ๋‹ค. (97์ด ์†Œ๋ฌธ์ž a์ž„.)
  • String.fromCharCode()์€ Unicode ๊ฐ’์„ ์ธ์ˆ˜๋กœ ๋ฐ›์•„ ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด ๊ฒฝ์šฐ, ๋ฌด์ž‘์œ„๋กœ ์ƒ์„ฑ๋œ Unicode ๊ฐ’์„ ๋ฌด์ž‘์œ„ ์†Œ๋ฌธ์ž ์•ŒํŒŒ๋ฒณ์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

(2) getRandomUpper

  • getRandomLower์™€ ๋™์ผ.
  • ๋Œ€๋ฌธ์ž A๊ฐ€ 65๋ผ 65๋ถ€ํ„ฐ ์‹œ์ž‘.

(3) getRandomNumber

  • Math.random()*10 : 0๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ๋ฌด์ž‘์œ„ ์ˆซ์ž๋ฅผ ์ƒ์„ฑ
  • 48์€ ๋ฌธ์ž '0'์— ํ•ด๋‹นํ•˜๊ธฐ ๋•Œ๋ฌธ์— 48๋ถ€ํ„ฐ ์‹œ์ž‘ํ•จ.

(4) getRandomSymbol

  • ๊ธฐํ˜ธ๋ฅผ sybols ๋ฐฐ์—ด์— ๋„ฃ๊ณ  ๋ฌด์ž‘์œ„๋กœ ๋ฝ‘์Œ.

โ“String.fromCharCode()

  • JavaScript์˜ ๋‚ด์žฅ ํ•จ์ˆ˜ ์ค‘ ํ•˜๋‚˜๋กœ, ์œ ๋‹ˆ์ฝ”๋“œ(Unicode) ๊ฐ’์„ ์ธ์ˆ˜๋กœ ๋ฐ›์•„ ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
    ์˜ˆ๋ฅผ ๋“ค์–ด, String.fromCharCode(65, 66, 67)์€ ABC๋ผ๋Š” ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ 65๋Š” ๋ฌธ์ž 'A'์˜ ์œ ๋‹ˆ์ฝ”๋“œ ๊ฐ’, 66์€ ๋ฌธ์ž 'B'์˜ ์œ ๋‹ˆ์ฝ”๋“œ ๊ฐ’, 67์€ ๋ฌธ์ž 'C'์˜ ์œ ๋‹ˆ์ฝ”๋“œ ๊ฐ’์— ํ•ด๋‹นํ•œ๋‹ค.

const randomFunc = {
  lower: getRandomLower,
  upper: getRandomUpper,
  number: getRandomNumber,
  symbol: getRandomSymbol
}

clipboardEl.addEventListener('click', () => {
  const password = resultEl.innerText;
if (!password) {
  return;
}
navigator.clipboard.writeText(password);
  alert('ํด๋ฆฝ๋ณด๋“œ์— ๋ณต์‚ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!')
})


generateEl.addEventListener('click', () => {
  const length = +lengthEl.value //+๋ฅผ ๋ถ™์—ฌ ์ˆซ์ž๋กœ ๋ณ€ํ™˜
  const hasLower = lowercaseEl.checked
  const hasUpper = uppercaseEl.checked
  const hasNumber = numbersEl.checked
  const hasSymbol = symbolsEl.checked
  
  resultEl.innerText = generatePassword(hasLower, hasUpper, hasNumber, hasSymbol, length)
})

function generatePassword(lower, upper, number, symbol, length) {
  let generatedPassword = ''
  //์ฒดํฌํ‘œ์‹œ ๋œ๊ฑฐ ์ˆ˜๋Ÿ‰ ์„ธ๊ธฐ
  const typesCount = lower + upper + number + symbol
  const typesArr = [{lower}, {upper}, {number}, {symbol}]
  //false๊ฐ’์€ ๊ฑธ๋Ÿฌ๋ƒ„
  .filter(item => Object.values(item)[0])

  if(typesCount === 0) {
    return ''
  }

  for(let i=0; i<length; i+=typesCount) {
    typesArr.forEach(type => {
      const funcName = Object.keys(type)[0]
      generatedPassword += randomFunc[funcName]()
    })
  }

  const finalPassword = generatedPassword.slice(0, length)
  return finalPassword
}
  • randomFunc ๊ฐ์ฒด๋Š” ๋„ค ๊ฐ€์ง€ ์†์„ฑ(lower, upper, number, symbol)๊ณผ ํ•ด๋‹น ์†์„ฑ์— ์—ฐ๊ฒฐ๋œ ํ•จ์ˆ˜(getRandomLower, getRandomUpper, getRandomNumber, getRandomSymbol)๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์ด ๊ฐ์ฒด๋Š” ๊ฐ ์†์„ฑ์„ ํ†ตํ•ด ๋ฌด์ž‘์œ„ ๋ฌธ์ž๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜์— ์ ‘๊ทผํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.

  • clipboardEl ์š”์†Œ๋Š” ํด๋ฆฝ๋ณด๋“œ ๋ฒ„ํŠผ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ํด๋ฆญ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ๋“ฑ๋ก๋˜์–ด ์žˆ๋‹ค.
    ํด๋ฆญ ์‹œ, resultEl์˜ ๋‚ด์šฉ์„ ๊ฐ€์ ธ์™€ ๋น„์–ด ์žˆ์ง€ ์•Š์€ ๊ฒฝ์šฐ ํ•ด๋‹น ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ํด๋ฆฝ๋ณด๋“œ์— ๋ณต์‚ฌํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  'ํด๋ฆฝ๋ณด๋“œ์— ๋ณต์‚ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!'๋ผ๋Š” ์•Œ๋ฆผ์„ ํ‘œ์‹œํ•œ๋‹ค.

  • generateEl ์š”์†Œ๋Š” ์ƒ์„ฑ ๋ฒ„ํŠผ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ํด๋ฆญ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ๋“ฑ๋ก๋˜์–ด ์žˆ๋‹ค.
    ํด๋ฆญ ์‹œ, ์ž…๋ ฅ๋œ ์˜ต์…˜์— ๋”ฐ๋ผ length์˜ ๊ธธ์ด๋กœ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
    hasLower, hasUpper, hasNumber, hasSymbol ๋ณ€์ˆ˜๋Š” ๊ฐ๊ฐ ์†Œ๋ฌธ์ž, ๋Œ€๋ฌธ์ž, ์ˆซ์ž, ํŠน์ˆ˜ ๋ฌธ์ž ํฌํ•จ ์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ฒดํฌ๋ฐ•์Šค ์š”์†Œ์˜ ์ƒํƒœ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
    generatePassword ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ resultEl ์š”์†Œ์˜ ๋‚ด์šฉ์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.

  • generatePassword ํ•จ์ˆ˜๋Š” ์ฃผ์–ด์ง„ ์˜ต์…˜์— ๋”ฐ๋ผ length ๊ธธ์ด์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
    ์ฒดํฌ๋œ ์˜ต์…˜์˜ ๊ฐœ์ˆ˜๋ฅผ typesCount ๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค.
    typesArr ๋ฐฐ์—ด์— ์†Œ๋ฌธ์ž, ๋Œ€๋ฌธ์ž, ์ˆซ์ž, ํŠน์ˆ˜ ๋ฌธ์ž๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด๋ฅผ ๋‹ด๋Š”๋‹ค. ์ด๋•Œ, ์ฒดํฌ๋œ ์˜ต์…˜๋งŒ ํ•„ํ„ฐ๋งํ•˜์—ฌ ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•œ๋‹ค.

  • ์˜ต์…˜์ด ํ•˜๋‚˜๋„ ์„ ํƒ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ๋นˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

  • ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด length๋งŒํผ ๋ฐ˜๋ณตํ•˜๊ณ , typesArr ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ˆœํšŒํ•œ๋‹ค.
    ๊ฐ ์š”์†Œ์—์„œ ํ•จ์ˆ˜ ์ด๋ฆ„์„ ๊ฐ€์ ธ์™€ randomFunc ๊ฐ์ฒด์—์„œ ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ฌด์ž‘์œ„ ๋ฌธ์ž๋ฅผ ์ƒ์„ฑํ•˜๊ณ , generatedPassword์— ์ถ”๊ฐ€ํ•œ๋‹ค.

  • ์ตœ์ข… ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ์ƒ์„ฑ๋œ ๋น„๋ฐ€๋ฒˆํ˜ธ์—์„œ length๋งŒํผ ์ž˜๋ผ๋‚ด์–ด ๋ฐ˜ํ™˜๋œ๋‹ค. (default๊ฐ€ 20์ด๋ฉด, ๊ธด ๋ฌด์ž‘์œ„ ๋ฌธ์ž์—ด์—์„œ 20๋งŒ ์ž˜๋ผ์„œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค๋Š” ๊ฒƒ.)

โ“โ—์—ฅ css

.result-container #result {
  word-wrap: break-word;
  /* 100%๋Š” ๋ถ€๋ชจ์š”์†Œ, 40px๋Š” ๋ฒ„ํŠผ์˜ ์š”์†Œ์ž„
  ๋น„๋ฐ€๋ฒˆํ˜ธ ๊ฒฐ๊ณผ๊ฐ’์ด ๋ถ€๋ชจ์š”์†Œ์—์„œ ๋ฒ„ํŠผ์„ ๋บ€ ๊ธธ์ด ์•ˆ์—์„œ ์ƒ์„ฑ๋˜์–ด์•ผ ํ•จ. */
  max-width: calc(100% - 40px);
}

โ“word-wrap

  • CSS์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์†์„ฑ์œผ๋กœ, ๊ธด ๋‹จ์–ด๋‚˜ ๊ธด URL ๋“ฑ์ด ์š”์†Œ์˜ ๊ฒฝ๊ณ„๋ฅผ ๋ฒ—์–ด๋‚˜์ง€ ์•Š๊ณ  ์ž๋™์œผ๋กœ ์ค„ ๋ฐ”๊ฟˆ๋˜๋„๋ก ์ง€์ •ํ•œ๋‹ค.
  • normal
    ๊ธฐ๋ณธ๊ฐ’. ๊ธด ๋‹จ์–ด๋‚˜ ๊ธด URL์ด ์š”์†Œ์˜ ๊ฒฝ๊ณ„๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด ์ค„ ๋ฐ”๊ฟˆํ•˜์ง€ ์•Š๊ณ , ๊ทธ๋Œ€๋กœ ํ‘œ์‹œ๋œ๋‹ค.
  • break-word
    ๊ธด ๋‹จ์–ด๋‚˜ ๊ธด URL์ด ์š”์†Œ์˜ ๊ฒฝ๊ณ„๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด ์ค„ ๋ฐ”๊ฟˆ์ด ๋ฐœ์ƒํ•˜์—ฌ ๋‹ค์Œ ์ค„๋กœ ์ด์–ด์„œ ํ‘œ์‹œ๋œ๋‹ค. ์ด๋•Œ, ๋‹จ์–ด๋‚˜ URL์˜ ์ค‘๊ฐ„์—์„œ๋„ ์ค„ ๋ฐ”๊ฟˆ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ‘๐Ÿป์ฐธ๊ณ ๋กœ, word-wrap ์†์„ฑ์€ CSS3์—์„œ๋Š” overflow-wrap์œผ๋กœ ๋Œ€์ฒด๋˜์—ˆ๋‹ค. overflow-wrap์€ word-wrap๊ณผ ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋ฉฐ, ๋” ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šด ์ด๋ฆ„์œผ๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ๋‹ค.

profile
์ •์‹ ์ฐจ๋ ค ์ด ๊ฐ๋ฐ•ํ•œ ์„ธ์ƒ์†์—์„œ

0๊ฐœ์˜ ๋Œ“๊ธ€