【html】随机生成任意长度密码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<button class="btn getRandomSymbol2" id="getRandomSymbol2" onclick="generatePasswordTest()">生成密码</button>
<input id="password" />
</body>
<script src="https://npm.elemecdn.com/jquery@3.3.1/dist/jquery.min.js"></script>
<script src="https://npm.elemecdn.com/amazeui@2.7.2/dist/js/amazeui.min.js"></script>
<script type="application/javascript">

    /**
     * 测试API
     * @returns {*}
     */
    function generatePasswordTest() {
        var a = + 4
            , b = true
            , c = true
            , d = true
            , e = false;
        // 長度 小寫 大寫 數字 字符
        var passwordText = generatePassword(a, b, c, d, e);
        console.log("长度 小写 大写 数字 字符: ", passwordText)
        $("#password").val(passwordText);
        return a[Math.floor(Math.random() * a.length)]
    }

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

    /**
     *
     * @param a 长度
     * @param b 小写
     * @param c 大写
     * @param d 数字
     * @param e 字符
     * @returns {string}
     */
    function generatePassword(a, b, c, d, e) {
        var i, f = "", g = b + c + d + e, h = [{
            lower: b
        }, {
            upper: c
        }, {
            number: d
        }, {
            symbol: e
        }].filter(function (a) {
            return Object.values(a)[0]
        });
        if (0 === g)
            return "";
        for (i = 0; a > i; i++)
            h.forEach(function (a) {
                var b = Object.keys(a)[0];
                f += randomFunc[b]()
            });
        return f.slice(0, a).split("").sort(function () {
            return Math.random() - .5
        }).join("")
    }

    function secureMathRandom() {
        return window.crypto.getRandomValues(new Uint32Array(1))[0] / (Math.pow(2, 32) - 1)
    }

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

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

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

    function getRandomSymbol() {
        var a = '~!@#$%^&*()_+{}":?><;.,';
        return a[Math.floor(Math.random() * a.length)]
    }
</script>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值