在powershell中创建htpasswd SHA1密码



我想在PowerShell中创建一个基于SHA1的htpasswd密码。

使用单词"test"作为密码,我测试了各种功能,总是得到SHA1值:

a94a8fe5ccb19ba61c4c0873d391e987982fbbd3

在htpasswd文件中测试

user:{SHA}a94a8fe5ccb19ba61c4c0873d391e987982fbbd3

我无法登录。

使用联机htpasswd生成器。例如https://www.askapache.com/online-tools/htpasswd-generator/我得到

user:{SHA}qUqP5cyxm6YcTAhz05Hph5gvu9M=

这很好用。

起初我认为我需要做一个base64 en/解码,但事实并非如此。

有人知道如何从"测试"到"qUqP5cyxm6YcTAhz05Hph5gvu9M="吗?

一开始我觉得我需要做一个base64 en/解码

事实就是这样!但你需要编码的不是字符串"a94a8fe5ccb19ba61c4c0873d391e987982fbbd3",而是它代表的底层字节数组

$username = 'user'
$password = 'test'
# Compute hash over password
$passwordBytes = [System.Text.Encoding]::ASCII.GetBytes($password)
$sha1 = [System.Security.Cryptography.SHA1]::Create()
$hash = $sha1.ComputeHash($passwordBytes)
# Had we at this point converted $hash to a hex string with, say:
#
#   [BitConverter]::ToString($hash).ToLower() -replace '-'
#
# ... we would have gotten "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"

# Convert resulting bytes to base64
$hashedpasswd = [convert]::ToBase64String($hash)
# Generate htpasswd entry
"${username}:{{SHA}}${hashedpasswd}"

最新更新