可视化安全——打造定制化验证码

315 阅读2分钟

前言

我们现如今处在一个数字信息化的时代,随着互联网的普及,网络安全变得极其重要,验证码的问世,作为防止自动化软件恶意登录或提交表单的有效屏障,已然成为保护用户数据安全的中不可或缺的一环。

实现效果图

canvas.gif

html

创建一个canvas画布,其大小设置为120*40,再为其绑定一个点击事件触发回调函数draw()实现随机生成验证码。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <canvas id="canvas" width="120" height="40" onclick="draw()"></canvas>
</body>
</html>

Js文件

  • let定义一个变量pool,用于生成0-9和A-Z的随机字符。
  • 定义一个函数randomNum(min, max),通过Math.random()生成一个介于0(包括)和1(不包括)范围内的随机数,再将该数乘(max - min),再加上min,得到在min和max的范围内的随机数,再使用Math.floor()向下取整,确保randomNum生成的随机数是整数。
  • 定义一个函数randomColor(min, max)用于随机生成验证码的背景颜色。,通过调用三次randomNum(min, max),分别生成rgb颜色中的随机值最后将这三个随机数组合并返回。
  • 通过document.getElementById("canvas")来获取id为canvas的元素,再对canvas画布进行随机颜色填充。
  • 定义一个变量为imgCode,用于随机生成字符串,再通过for循环调用四次randomNum函数,得到四个随机数,随机数的范围为0- pool.length,再获取到pool对应的下标元素,并且通过fontize来改变获取到的随机数的大小。
  • 再随机生成干扰线条,通过for循环生成两个随机数,范围是randomNum(0, canvas.width), randomNum(0, canvas.height),使用ctx.beginPath()为干扰线条开始一条新的路径,使用ctx.moveTo(randomNum(0, canvas.width), randomNum(0, canvas.height))将起点移动到随机坐标的位置,ctx.lineTo(randomNum(0, canvas.width), randomNum(0, canvas.height))在终点添加一条线段,再调用randomColor生成干扰线条的颜色。
  • 再随机生成干扰的小点点,通过for循环调用randomNum画出干扰小圆,ctx.arc(randomNum(0, canvas.width), randomNum(0, canvas.height), 1, 0, 2 * Math.PI)在随机位置绘制一个一像素大小的圆,并且调用randomColor为小圆生成随机颜色,将其作为干扰小点点。
  • 最后调用draw()函数
 let pool = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'

    function randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min)
    }

    function randomColor(min, max) {
      const r = randomNum(min, max)
      const g = randomNum(min, max)
      const b = randomNum(min, max)
      return `rgb(${r},${g},${b})`
    }


    function draw() {
      let canvas = document.getElementById("canvas");
      let ctx = canvas.getContext("2d");
      // 填充色 随机
      ctx.fillStyle = randomColor(100, 230)
      ctx.fillRect(0, 0, canvas.width, canvas.height)
      // 随机生成字符串
      let imgCode = ''
      for (let i = 0; i < 4; i++) {
        const text = pool[randomNum(0, pool.length)]
        imgCode += text
        // 随机字体大小
        const fontSize = randomNum(18, 40)
        // 随机旋转角度
        const deg = randomNum(-30, 30)
        ctx.font = `${fontSize}px Simhei`
        ctx.textBaseline = 'top'
        ctx.fillStyle = randomColor(80, 150)
        ctx.save()  // 将当前状态封存入栈
        ctx.translate(30 * i + 15, 15)
        ctx.rotate((deg * Math.PI) / 180)
        ctx.fillText(text, -10, -15)
        ctx.restore() //

      }

      //随机生成干扰线条
      for(let i = 0; i < 5; i++){
        ctx.beginPath()
        ctx.moveTo(randomNum(0, canvas.width), randomNum(0, canvas.height))
        ctx.lineTo(randomNum(0, canvas.width), randomNum(0, canvas.height))
        ctx.strokeStyle = randomColor(100, 230)
        ctx.closePath()
        ctx.stroke()
      }
      // 随机生成小点
      for(let i = 0; i < 40; i++) {
        ctx.beginPath()
        ctx.arc(randomNum(0, canvas.width), randomNum(0, canvas.height), 1, 0, 2 * Math.PI)
        ctx.fillStyle = randomColor(150, 200)
        ctx.closePath()
        ctx.fill()
      }
    }

    draw()

结语

快去自己尝试打造一个验证码吧

冲冲冲.jpg