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

如何用JavaScript生成符合Unix Shadow格式的SHA256哈希?

生成符合Unix Shadow格式的SHA256哈希解决方案

推荐方案:复用成熟第三方库

自行实现SHA-crypt规范的算法逻辑(含特殊Base64编码、盐值融合、多轮迭代)复杂度极高,直接使用现成的加密库是最高效的选择,以下是主流开发环境的实现示例:


Node.js环境实现

使用遵循SHA-crypt规范的sha256-crypt包:

  1. 安装依赖:
npm install sha256-crypt
  1. 生成目标哈希:
const sha256crypt = require('sha256-crypt');

// 可自定义盐值,若不传则自动生成随机盐
const customSalt = 'r.ba0aYa2PI.';
const plainPassword = 'your-target-password';
const roundCount = 5000;

// 生成符合格式的哈希
const shadowHash = sha256crypt.sha256crypt(plainPassword, `$5$rounds=${roundCount}$${customSalt}$`);
console.log(shadowHash);
// 输出示例:$5$rounds=5000$r.ba0aYa2PI.$2Yq2P8t.dKkIT3ZAnxg.y6J2LYD1p7N0NhSzQhL9Zy3

Python环境实现

使用passlib库的sha256_crypt模块:

  1. 安装依赖:
pip install passlib
  1. 生成目标哈希:
from passlib.hash import sha256_crypt

plainPassword = 'your-target-password'
roundCount = 5000
customSalt = 'r.ba0aYa2PI.'

# 生成符合格式的哈希
shadowHash = sha256_crypt.using(rounds=roundCount, salt=customSalt).hash(plainPassword)
print(shadowHash)
# 输出示例:$5$r.ba0aYa2PI.$2Yq2P8t.dKkIT3ZAnxg.y6J2LYD1p7N0NhSzQhL9Zy3

核心特性验证

  • 自动使用SHA-crypt规范定义的特殊Base64字母表(./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz)
  • 支持指定5000轮迭代参数
  • 生成的哈希编码部分长度为43位,完全匹配Unix Shadow格式要求

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 18:31:01