You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ruby应用重置密码遇OpenSSL::HMACError错误求解决方案

解决OpenSSL::HMACError (EVP_PKEY_new_mac_key: malloc failure)问题

以下是针对你的环境的可行解决方案:

1. 重新用指定OpenSSL版本编译Ruby

RVM管理的Ruby可能未正确关联新安装的OpenSSL,需重新编译Ruby并指定OpenSSL路径:

  • 先确认OpenSSL 3.1.2的安装路径,执行命令查看:
    openssl version -a
    
    从OPENSSLDIR字段获取路径。
  • 用RVM重新安装Ruby 3.1.2,指定上述OpenSSL路径:
    rvm reinstall 3.1.2 --with-openssl-dir=/usr/local/ssl
    
  • 验证Ruby关联的OpenSSL版本:
    ruby -r openssl -e 'puts OpenSSL::OPENSSL_VERSION'
    
    确保输出为OpenSSL 3.1.2。

2. 检查Rails加密配置

Rails 7的加密配置可能与OpenSSL版本不兼容,确认凭据文件状态:

  • 备份config/master.key和config/credentials.yml.enc。
  • 重新生成加密凭据,若提示无编辑器,先设置默认编辑器:
    export EDITOR=nano
    rails credentials:edit
    
  • 确保密码重置逻辑使用Rails内置加密方法,避免手动调用OpenSSL HMAC时参数错误。

3. 修复WSL内存分配问题

你使用Windows下的Ubuntu终端(大概率是WSL2),malloc failure可能与内存限制有关:

  • 打开Windows的%UserProfile%\.wslconfig文件(无则新建),添加配置:
    [wsl2]
    memory=8GB
    swap=4GB
    
  • 重启WSL后再测试:
    wsl --shutdown
    

4. 降级OpenSSL到兼容版本

OpenSSL 3.x与Ruby 3.1.x可能存在兼容性问题,尝试降级到OpenSSL 1.1.1t:

  • 卸载当前OpenSSL 3.1.2:
    sudo apt remove --purge openssl
    
  • 安装指定版本:
    sudo apt update
    sudo apt install openssl=1.1.1t-1ubuntu2.10
    
  • 重新编译Ruby关联该版本:
    rvm reinstall 3.1.2 --with-openssl-dir=/usr/lib/ssl
    

5. 刷新Gem依赖

确保Bundler安装的gem适配当前Ruby和OpenSSL版本:

  • 清空gem缓存:
    bundle clean --force
    
  • 重新安装所有gem:
    bundle install
    
  • 重点检查bcrypt、devise等密码处理类gem的版本兼容性。

内容的提问来源于stack exchange,提问作者Gian Lucca

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 13:10:29