在powershell中创建htpasswd SHA1密码

在powershell中创建htpasswd SHA1密码,powershell,sha1,.htpasswd,Powershell,Sha1,.htpasswd,我想在PowerShell中基于SHA1创建一个htpasswd密码 使用单词“test”作为密码,我测试了各种功能,并始终获得SHA1值: a94a8fe5ccb19ba61c4c0873d391e987982fbbd3 在htpasswd文件中测试此功能 user:{SHA}a94a8fe5ccb19ba61c4c0873d391e987982fbbd3 我无法登录 使用在线htpasswd生成器。例如,我得到 这很好用 起初我认为我需要进行base64 en/解码,但事实并非如此 有人

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

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

a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
在htpasswd文件中测试此功能

user:{SHA}a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
我无法登录

使用在线htpasswd生成器。例如,我得到

这很好用

起初我认为我需要进行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}"
$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}"