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

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>随机生成密码</title> </head> <body> <input type="button" value="生成密码" /> <span></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代码运行工具测试上述代码,可得如下运行结果:

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

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:这里再为大家提供两款相关在线工具供大家参考使用:

在线随机数字/字符串生成工具:

高强度密码生成器:

更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript数学运算用法总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript数组操作技巧总结》、《JavaScript排序算法总结》、《JavaScript遍历算法与技巧总结》、《JavaScript查找算法技巧总结》及《JavaScript错误与调试技巧总结

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/50da91f19cbdc87e5fcae250efd0f5e6.html