是否有命令行实用程序来提取证书指纹?

时间:2010-12-27 18:42:01

标签: windows security certificate

我创建了一个机器证书。它出现在证书(本地计算机)\ Personal \ Certificates 证书存储库文件夹中。现在我希望使用命令行实用程序提取其指纹。

不幸的是,我能找到的最接近的是this article

我需要能够在任何以XP开头的Windows操作系统上执行此过程。

感谢。

6 个答案:

答案 0 :(得分:7)

老了,但也许这会对某人有所帮助。将以下内容放在powershell脚本(.ps1)中并运行它。它会将拇指打印到屏幕上。在我的粘贴中观看自动换行。

$computerName = $Env:Computername
$domainName = $Env:UserDnsDomain
write-host "CN=$computername.$domainname"
$getThumb = Get-ChildItem -path cert:\LocalMachine\My | where { $_.Subject -match "CN\=$Computername\.$DomainName" }
$getThumb.thumbprint

答案 1 :(得分:4)

直接从命令行获取未安装的.cer文件,并删除嵌入的空格(可能可以改进):

certutil.exe <mycert>.cer | findstr /c:"Cert Hash(sha1)" | for /f "tokens=3-22" %f in ('more') do @echo %f%g%h%i%j%k%l%m%n%o%p%q%r%s%t%u%v%w%x%y

答案 2 :(得分:2)

直接从文件.cer获取指纹

const certpath = "\\host\res\something.cer"
dim objStdOut
dim strLine, resString

set objStdOut = CreateObject("WScript.Shell").Exec("certutil " & certpath).StdOut

while not objStdOut.AtEndOfStream
    strLine = objStdOut.ReadLine
    if InStr(strLine, "(sha1)") > 0 then resString = trim(split(strLine, ":")(1))
wend
wscript.echo resString

答案 3 :(得分:1)

在我的情况下,我无法使用PowerShell,所以我编写了这个脚本来运行cscript.exe,它会让你使用正则表达式。

If WScript.Arguments.Count() = 0 Then
    WScript.Echo "Domain name to search for must be specified as first parameter."
    WScript.Quit 1
End If
domain = WScript.Arguments.Item(0)

Set objShell = WScript.CreateObject ("WScript.shell")

' Get all certificate information in store.
Set objCert = objShell.Exec("certutil -store my")
certOutput = ""
Do While objCert.Status = 0
  WScript.Sleep 10 
  Do While Not objCert.StdOut.AtEndOfStream 
     certOutput = certOutput & objCert.StdOut.ReadLine & vbNewLine
  Loop
Loop 

' Capture thumb for specified certificate using Regex.
Set thumbRegex = New RegExp
thumbRegex.Pattern = "Subject:\s+CN=" & domain & "\s*\n.*\n.*\nCert\sHash\(sha1\):\s+(.*)"
thumbRegex.IgnoreCase = True
thumbRegex.Global = False

' Verify match and trim out white space.
Set match = thumbRegex.Execute(certOutput)
result = ""
If match.Count > 0 Then
    result = match.Item(0).Submatches(0)
    result = Replace(result, " ", "")
    WScript.Echo result
Else
    WScript.Echo "The certificate for """ & domain & """ was not found."
    WScript.Quit 2
End If

答案 4 :(得分:1)

powershell Get-Childitem Cert:\LocalMachine\My

答案 5 :(得分:0)

这是一个简单的python脚本:

def getThumbPrint(cert, passwd):
    val = ""
    info = subprocess.Popen(["certutil", "-p", passwd, cert], shell=False, stdout=subprocess.PIPE)
    for i in info.communicate()[0].split('\n'):
        if i.startswith("Cert Hash(sha1):"):
            val = i.split(':')[1].strip()

    # There may be more than 1, we want the last one.
    return val