vue3+hubuilderX开发微信小程序使用elliptic生成ECDH密钥对遇到的问题

1、Cannot read properties of null (reading 'getBytes')

具体使用方式为

复制代码
import EC from 'elliptic/lib/elliptic/ec';
const ec = new EC('p256'); // 选择椭圆曲线secp256k1的别名是p256
const aliceKeyPair = ec.genKeyPair();

增加log后发现问题产生原因在const aliceKeyPair = ec.genKeyPair();函数。故进行了进一步的排查找到node_modules/elliptic/lib/elliptic/ec/index.js路径下的EC.prototype.genKeyPair = function genKeyPair(options) 函数后发现问题在于var brorand = require('brorand');这个库的引入,这里我因为调试更改了引入后的名称注意分辨。

同时注意brorand的路径应该为node_modules/brorand/index.js而非Microsoft/TypeScript/5.2/node_modules/@types/brorand/index.d.ts因为这个路径问题多调试了两个小时各种分析。

复制代码
var drbg = new HmacDRBG({
    hash: this.hash,
    pers: options.pers,
    persEnc: options.persEnc || 'utf8',
    entropy: options.entropy || brorand(this.hash.hmacStrength),
    entropyEnc: options.entropy && options.entropyEnc || 'utf8',
    nonce: this.n.toArray(),
  });

在node_modules/brorand/index.js中存在

c 复制代码
function Rand(rand) {
  this.rand = rand;
}

而elliptic/lib/elliptic/ec/index.js在调用时似乎并未传入对应的rand随机数生成函数。所以产生了Cannot read properties of null (reading 'getBytes')问题。

解决方案为将node_modules/brorand/index.js中的Rand替换为以下代码即可正常使用。

c 复制代码
const customRand = {
  getBytes: function(n) {
    const arr = new Uint8Array(n);
    // 自定义随机数生成逻辑(例如使用 Math.random(),但不推荐用于加密)
    for (let i = 0; i < n; i++) {
      arr[i] = Math.floor(Math.random() * 256);
    }
    return arr;
  }
};


function Rand(rand) {
  this.rand = customRand;
}

这部分内容在修改完成后需要把微信开发者工具完全关闭后再重新打开才会生效。包的修改都需要进行这个操作。

但是我也不知道是否应该在node_modules中直接进行修改这样子不会导致后续包更新导致修改被覆盖或者其他问题嘛?欢迎懂的大佬在评论区交流一下。我是小程序新手。

相关推荐
说私域15 小时前
基于开源链动2+1模式AI智能名片S2B2C商城小程序的零售流量重构研究
人工智能·小程序·开源
鹧鸪云光伏与储能软件开发17 小时前
光伏开发小程序:快速获客,成交项目更迅速
大数据·微信小程序·小程序·光伏
晨旭缘19 小时前
解决小程序滚动穿透问题
小程序
游戏开发爱好者821 小时前
iOS 崩溃日志分析工具全指南,多工具协同构建稳定性分析体系
android·macos·ios·小程序·uni-app·cocoa·iphone
00后程序员张1 天前
如何提高 IPA 安全性 多工具组合打造可复用的 iOS 加固与反编译防护体系(IPA 安全 iOS 加固 无源码混淆 Ipa Guard 实战)
android·安全·ios·小程序·uni-app·iphone·webview
游戏开发爱好者81 天前
Fiddler抓包实战教程 从安装配置到代理设置,详解Fiddler使用方法与调试技巧(HTTPHTTPS全面指南)
前端·测试工具·小程序·https·fiddler·uni-app·webview
sen_shan1 天前
《微信小程序》第五章:登录-API封装
微信小程序·小程序
2501_916007472 天前
Fastlane 结合 开心上架 命令行版本实现跨平台上传发布 iOS App
android·ios·小程序·https·uni-app·iphone·webview
00后程序员张2 天前
iOS 26 内存占用监控 多工具协同下的性能稳定性分析实战
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_915909062 天前
iOS 26 性能监控工具有哪些?多工具协同打造全方位性能分析体系
android·macos·ios·小程序·uni-app·cocoa·iphone