【问题标题】:How can I get an MD5 fingerprint (signature) of an SSH public key from a string如何从字符串中获取 SSH 公钥的 MD5 指纹(签名)
【发布时间】:2019-10-06 16:34:23
【问题描述】:

如果我有公钥的字符串而不是文件,有没有办法获取公钥的 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 命令。

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

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

【问题讨论】:

    标签: ruby bash ssh md5 ssh-keygen


    【解决方案1】:

    我决定寻找解决问题的 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))
    

    我在原始问题中使用了术语“签名”,我已经编辑了该问题以添加“指纹”作为替代术语

    【讨论】:

      【解决方案2】:

      使用- 作为文件名来读取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\}\).*/\1/
      # 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\}\).*/\1/'
      )"
      
      # 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="$1"
      
      # Print the md5 for testing
      echo "$key_md5"
      

      【讨论】:

        【解决方案3】:

        使用 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)
        

        【讨论】:

          猜你喜欢
          • 2020-02-12
          • 2018-08-06
          • 2021-08-23
          • 2011-10-04
          • 2018-12-06
          • 2019-03-29
          • 2011-08-26
          • 2023-03-10
          • 2013-12-25
          相关资源
          最近更新 更多