Skip to content

Instantly share code, notes, and snippets.

@simonkuang
Last active November 5, 2023 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonkuang/7051100dcc5a5fe8989ca6745911bb61 to your computer and use it in GitHub Desktop.
Save simonkuang/7051100dcc5a5fe8989ca6745911bb61 to your computer and use it in GitHub Desktop.
Basic Auth
  • cut: 取出user.txt的第二个字段(密码明文),将结果输出到stdout;
  • openssl: 将stdin中输入的数据(3条密码明文)使用SHA512形式进行hash,输出到stdout;
  • paste: 将user.txt中的原始内容与hash过的密码组合起来,输出到stdout;
  • tr: 去除可能出现的\r符号;
  • awk: 从stdin中提取出用户名、hash过的密码、注释字段,将结果输出到passwd.txt。

OpenSSL 的帮助命令可以看出当前的 openssl 命令支持哪些加密方式。

openssl help passwd

$ openssl help passwd
Usage: passwd [options]
Valid options are:
 -help               Display this summary
 -in infile          Read passwords from file
 -noverify           Never verify when reading password from terminal
 -quiet              No warnings
 -table              Format output as table
 -reverse            Switch table columns
 -salt val           Use provided salt
 -stdin              Read passwords from stdin
 -6                  SHA512-based password algorithm
 -5                  SHA256-based password algorithm
 -apr1               MD5-based password algorithm, Apache variant
 -1                  MD5-based password algorithm
 -aixmd5             AIX MD5-based password algorithm
 -crypt              Standard Unix password algorithm (default)
 -rand val           Load the file(s) into the random number generator
 -writerand outfile  Write random data to the specified file

可以根据参数自行修改加密参数。

cut -d ":" -f 2 user.txt | openssl passwd -6 -stdin | paste -d ":" user.txt - | tr -d '\r' | awk 'BEGIN{FS=":";OFS=":"}{if (NF==4) print $1,$4,$3; else if (NF==3) print $1,$3}' > passwd.txt
user1:$6$gjGELGyOCDsUrRtG$OcQy9GXbdpZ9Iujd1Jmzwyd5dJaXxyWUgcsiQxAOAbBj/OEQChPSD0iojfDIn3qKd82Spm5yVd3qAUmJno0KP.:comment
user2:$6$ak/BIY4C7knXMVq5$hVGDCcKaz5ExcJ2Fv9xusW6ZdqCoWQrveqrK3pWgtbUebi5CCsO4e9GbxgGfSThuVILIfJUaIBjUv7B.60nez1
m3ng9i:$6$euEJqJ4PmiMdJJXC$gPLLrYkPWz.LqW50RcMA/rPzOTI9j1pWjxSlgXtHd8RJbBedpxjf.w67QnjG2m8MIxnY9G.fDCIlbDw5E2aXu.:mengqi.info
user1:abcde:comment
user2:xyz123
m3ng9i:123456:mengqi.info
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment