首页 > 数据库 >Node.js通过密钥计算验证码,无需存数据库

Node.js通过密钥计算验证码,无需存数据库

时间:2022-11-19 20:11:42浏览次数:187  
标签:Node iKeeptime oPri const 验证码 js p1Len return

Node.js通过密钥计算验证码,无需存数据库

const CRC32 = require('crc-32');
function XixiOtp(sKey, iKeeptime = 600) {
    const oPri = {};
    oPri.sKey = sKey;
    oPri.iKeeptime = iKeeptime * 1000;
    oPri.p1Len = 1; // 验证码前缀长度(建议1~2)
    oPri.p2Len = 5; // 验证码后缀长度(建议3~7)
    oPri.sAccount = '';
    oPri.time = () => {
        return +new Date();
    };
    oPri.intval = (num) => {
        return parseInt(num, 10);
    };
    oPri.numPad = (num, len) => {
        // 补零
        const diff = len - num.toString().length;
        const pre = (diff > 0) ? Array(diff + 1).join('0') : '';
        return pre + num;
    };
    oPri.getCrc32 = (str) => {
        var i = CRC32.str(str);
        if (i < 0) {
            i += Math.pow(2, 32);
        }
        return i;
    };
    oPri.getP2 = (iT1, iP1) => {
        // 通过时间和验证码前缀来计算验证码的后缀
        const iT2 = oPri.intval((iT1 - iP1 * oPri.iKeeptime / Math.pow(10, oPri.p1Len)) / oPri.iKeeptime);
        const sP1 = oPri.numPad(iP1, oPri.p1Len);
        const arr = [oPri.sAccount, iT2, sP1, oPri.sKey];
        const crc = oPri.getCrc32(arr.join(','));
        const iP2 = crc % Math.pow(10, oPri.p2Len);
        return oPri.numPad(iP2, oPri.p2Len);
    };
    const oPub = {};
    oPub.setAccount = (sAccount) => {
        oPri.sAccount = sAccount;
    };
    oPub.getCode = () => {
        // 取得Otp验证码
        const iT1 = oPri.time();
        const m = iT1 % oPri.iKeeptime;
        const iP1 = oPri.intval(m * Math.pow(10, oPri.p1Len) / oPri.iKeeptime);
        const iP2 = oPri.getP2(iT1, iP1);
        return oPri.numPad(iP1, oPri.p1Len) + iP2;
    };
    oPub.verify = (sCode) => {
        // 校验Otp验证码是否有效
        const iT1 = oPri.time();
        const sP1 = sCode.substr(0, oPri.p1Len);
        const sP2 = sCode.substr(oPri.p1Len);
        return sP2 === oPri.getP2(iT1, oPri.intval(sP1));
    }
    return oPub;
}
const iKeeptime = 600;
const otp = XixiOtp('feieryun.cn', iKeeptime);
otp.setAccount('[email protected]');
const sCode = otp.getCode();
console.log(sCode);
var i = 0;
setInterval(() => {
    console.log(i, otp.verify(sCode));
    i++;
}, 1000);

Node.js通过密钥计算验证码,无需存数据库

标签:Node,iKeeptime,oPri,const,验证码,js,p1Len,return
From: https://www.cnblogs.com/xiangxisheng/p/16906916.html

相关文章

  • Mybatis出现Caused by: net.sf.jsqlparser.parser.ParseException: ....异常
    今天在开发项目中遇到了一个奇怪的异常,记录一下把!异常信息如下(截取了主要的部分)Causedby:net.sf.jsqlparser.parser.ParseException:Encountered""TOP""top""at......
  • js端 分页导航条
    有些情况界面一次性加载完所有数据,table列表的形式展示:对于这样的情况有时候需要用js将这些数据进行分页显示。下面的js实现的就是纯js分页条。......
  • PHP通过加密计算出短信验证码,无需缓存验证码
    通过加密计算出短信验证码,无需缓存验证码classXixiOtp{private$iKeeptime;//验证码有效期private$sKey;//加密的密钥private$p1Len=1;//验......
  • 使用redis实现验证码功能
    redis模拟验证码发送要求:1.输入手机号,点击发送后随即生成6位数字码,2分钟有效​ 2.输入验证码,点击验证,返回成功或者失败​ 3.每个手机号每天只能输入3次分析:​ 验......
  • 日学壹技:json.load() vs json.loads()
    导读本文演示如何使用Python的json.load()和json.loads()方法从文件和字符串中读取JSON数据。使用json.load()和json.loads()方法,您可以将JSON格式的数据转......
  • DOM_Element对象以及Node对象
    DOM_Element对象Element:元素对象获取/创建:通过document来获取和创建方法:removeAttribute():删除属性setAttribute():添加新属性<body><......
  • fpjson使用例子【转】
    lazarusfpjson使用例子procedureTForm1.Button2Click(Sender:TObject);varjData:TJSONData;jDataArr:TJSONData;jObject:TJSONObject;jArray:TJSONA......
  • commonjs规范 require 函数解析
    functionrequire(modulePath){//1.根据传入的模块路径得到模块完整的绝对路径constmoduleId=getModuleId(modulePath)//2.判断缓存if(cache[mo......
  • Bot in Discord with discord.js (9)
    BotinDiscordwithdiscord.js(9)Chapter10-交互四大组件之:按钮新建一个操作行ActionRow上来先导入需要的ActionRowBuilder,用于建立操作行ActionRow。通过ne......
  • js 之在图片添加链接、视频等
    一、效果图   ......