如何从字符串中获取 SSH public 密钥的 MD5 指纹(签名)

How can I get an MD5 fingerprint (signature) of an SSH public key from a string

如果我有一个 public 密钥的字符串而不是一个文件,是否有办法获取该密钥的 MD5 指纹(签名)?

ssh-keygen -l -E md5 -f "path/to/file"

此命令将给我(除其他外)密钥的 MD5 指纹(签名)。我已经阅读了 ssh-keygen 命令的手册页并在我的 shell 中进行了试验,但我无法让它在字符串而不是文件上工作。我找不到接受字符串的参数,我尝试在字符串中进行管道传输,还尝试使用 STDIN 将字符串提供给 ssh-keygen - 都没有成功。

我已经尝试在字符串上使用 Ruby 和 Digest::MD5.hexdigest,但要么我没有提供正确的字符串,要么我需要使用其他一些哈希机制,因为我已经也没有运气。 (我尝试了密钥的各种子字符串)

当然,我可以将字符串写入临时文件,然后在该文件上使用 ssh-keygen,但似乎我不应该这样做。

我最终想在 Ruby 中解决这个问题,但我可以从 unix 实用程序或 bash 解决方案开始。如果需要,我总是可以从 Ruby.

执行 shell 命令

如果重要的话,我是 运行 a bash(GNU bash,版本 3.2.57(1))shell 在 macOS Mojave (10.14.6 ) 和 Ruby 2.6.4

编辑:我将术语从签名更改为指纹,但在括号中留下了签名。这两个术语我都见过,但我认为指纹更常见。

我决定搜索 Ruby Gem 来解决问题。我找到了这个:https://github.com/bensie/sshkey。挖掘源代码,我发现我需要对字符串的关键部分进行 Base64 解码,然后对其执行 Digest::MD5.hexdigest 以获得密钥的指纹。

string = "ssh-rsa aabbccddqq== comment goes here" # not a real key
key = string.split(" ")[1]
fingerprint = Digest::MD5.hexdigest(Base64.decode64(key))

我在原始问题中使用了术语 "signature",我编辑了该问题以添加 "fingerprint" 作为替代术语

使用-作为文件名读取stdin作为文件。 然后用 Bash 的 <<<"herestring"

stdin

与Bash:

#!/usr/bin/env bash

# Fill a keystring variable for demo purpose
IFS= read -r -d '' keystring <"$HOME/.ssh/id_rsa.pub"


# Get ssh-keygen to read from the keystring rather than a file
key_md5="$(ssh-keygen -l -E md5 -f - <<<"$keystring" | cut -d ' ' -f2)" 

# Isolate and reformat the md5
# Remove the MD5: prefix
key_md5="${key_md5##MD5:}"

# Remove the in-between :
key_md5="${key_md5//:}"

# Print the md5 for testing
echo "$key_md5"

或 POSIX shell:

#!/usr/bin/env sh

# Fill a keystring variable for demo purpose
IFS= read -r keystring <"$HOME/.ssh/id_rsa.pub"

# Get ssh-keygen to read from the keystring rather than a file
# and reformat the md5 sum with sed POSIX Extended Regex
# sed commands:
# s/://g deletes all colon characters
# s/^[[:digit:]]\+[[:space:]]\+MD5\([[:xdigit:]]\{32\}\).*//
# starting with digits followed by spaces, followed by MD5
# capture the group of 32 hexadecimal digits from the md5 sum
key_md5="$(
  echo "$keystring" \
    | ssh-keygen -l -E md5 -f - \
    | sed 's/://g;s/^[[:digit:]]\+[[:space:]]\+MD5\([[:xdigit:]]\{32\}\).*//'
)"

# Print the md5 for testing
echo "$key_md5"

或者,您可以使用 base64 命令解码密钥,并使用 md5sum 命令计算 shell 中的总和,如下所示:

#!/usr/bin/env sh

# Fill a keystring variable for demo purpose
IFS= read -r keystring <"$HOME/.ssh/id_rsa.pub"

# Inject this sub-shell commands result into the arguments
set -- "$(

  # Pipe in the ssh key string
  echo "$keystring" |

  # Get the second field containing the base64 encoded key
  cut -d ' ' -f2 |

  # Decode the key from its base64
  base64 -d |

  # Compute the md5 sum of the key
  md5sum |

  # Get the first field containing the md5
  cut -d ' ' -f1
)"

# The key's md5 has been returned in the first argument
key_md5=""

# Print the md5 for testing
echo "$key_md5"

使用Python:

import codecs, hashlib
pubkey = "AAAA...."  #Put you public key string here
fingerprint = hashlib.md5(codecs.decode(bytes(pubkey,'utf-8'), 'base64')).hexdigest()
print(fingerprint)

如果您需要冒号分隔的指纹,请尝试:

cs_fingerprint = ":".join([ fingerprint[pair:pair+2] for pair in range(0, len(fingerprint)-1, 2) ])
print(cs_fingerprint)