在powershell中创建htpasswd SHA1密码

问题描述 投票:0回答:1

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

使用单词“ test”作为密码,我已经测试了各种功能,并且始终获得SHA1值:

a94a8fe5ccb19ba61c4c0873d391e987982fbbd3

在htpasswd文件中测试

user:{SHA}a94a8fe5ccb19ba61c4c0873d391e987982fbbd3

我无法登录。

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

user:{SHA}qUqP5cyxm6YcTAhz05Hph5gvu9M=

哪个效果很好。

起初我以为我需要进行base64编码/解码,但事实并非如此。

关于如何从“测试”转换为“ qUqP5cyxm6YcTAhz05Hph5gvu9M =“的任何想法?

powershell sha1 .htpasswd
1个回答
0
投票

起初我以为我需要进行base64编码/解码

就是确实情况!但这不是您需要编码的字符串“ 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}"
© www.soinside.com 2019 - 2024. All rights reserved.