亲宝软件园·资讯

展开

JS与SQL随机生成高强度密码 JS与SQL方式随机生成高强度密码示例

csmzl 人气:0

本文实例讲述了JS与SQL方式随机生成高强度密码。分享给大家供大家参考,具体如下:

JS方式

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>随机生成密码</title>
  </head>
  <body>
    <input type="button" onclick="RanPwd()" value="生成密码" />
    <span id="pwd"></span>
    <script>
      var numChar = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
      var alphChar = ['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'];
      var syblChar = ['~', '!', '@', '#', '$', '%', '^', '&', '*', '-', '+', '?', '¥', ';', '"', '<', '>', '|'];
      function generatePwd() {
        var res = "";
        for(let i = 0; i < 2; i++) {
          var id = Math.floor(Math.random() * 10);
          res += numChar[id];
        }
        for(let j = 0; j < 3; j++) {
          var id = Math.floor(Math.random() * 26);
          res += alphChar[id];
        }
        for(let k = 0; k < 1; k++) {
          var id = Math.floor(Math.random() * 18);
          res += syblChar[id];
        }
        return res;
      }
      function strengthPwd(pwd) {
        var len = n = pwd.length-1;
        pwd = pwd.split("");
        var newPwd = "";
        for(var i = 0; i <= len; i++) {
          var id = Math.floor(Math.random() * n)
          newPwd += pwd.splice(id,1);
          n--;
        }
        return newPwd;
      }
      function RanPwd(){
        var prePwd = generatePwd();
        var pwd = strengthPwd(prePwd);
        document.getElementById("pwd").innerHTML = pwd;
      }
    </script>
  </body>
</html>

这里使用在线HTML/CSS/JavaScript代码运行工具http://tools.softyun.net/code/HtmlJsRun测试上述代码,可得如下运行结果:

SQL方式

--生成高强度的随机密码
declare @strPwd varchar(82)
declare @i int
declare @n int
declare @Index int
declare @pwd varchar(6)
declare @newPwd varchar(6)
set @i = 0
set @pwd = ''
set @newPwd = ''
set @strPwd = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*-=+?.,;:|><'
while (@i < 6)
begin
  if(@i<2)
  begin
    set @Index = ceiling((rand() * 10))
  end
  else if(@i<5 and @i>=2)
  begin
    set @Index = ceiling(rand() * 52 + 10)
  end
  else if(@i=5)
  begin
    set @Index = ceiling(rand() * 20 + 62)
  end
  set @pwd = @pwd + substring(@strPwd,@Index,1)
  set @i = @i + 1
end
set @i = 0
set @n = LEN(@pwd)
while (@i < 6)
begin
 set @Index = ceiling(rand() * @n)
 set @newPwd = @newPwd + substring(@pwd,@Index,1)
 set @pwd = STUFF(@pwd,@Index,1,'')
 set @n = @n-1
 set @i = @i + 1
end
select @newPwd

PS:这里再为大家提供两款相关在线工具供大家参考使用:

在线随机数字/字符串生成工具:
http://tools.softyun.net/aideddesign/suijishu

高强度密码生成器:
http://tools.softyun.net/password/CreateStrongPassword

希望本文所述对大家JavaScript程序设计有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论