(ssh_pubkey, hashfunction=hashlib.sha256)
| 147 | |
| 148 | |
| 149 | def key_fingerprint(ssh_pubkey, hashfunction=hashlib.sha256): |
| 150 | |
| 151 | # Treat as bytes, not string |
| 152 | if type(ssh_pubkey) == str: |
| 153 | ssh_pubkey = ssh_pubkey.encode('utf-8') |
| 154 | |
| 155 | # Strip space from end |
| 156 | ssh_pubkey = ssh_pubkey.rstrip(b"\r\n\0 ") |
| 157 | |
| 158 | # Only look at first line |
| 159 | ssh_pubkey = ssh_pubkey.split(b"\n")[0] |
| 160 | # If two spaces, look at middle segment |
| 161 | if ssh_pubkey.count(b" ") >= 1: |
| 162 | ssh_pubkey = ssh_pubkey.split(b" ")[1] |
| 163 | |
| 164 | # Try to decode key as base64 |
| 165 | try: |
| 166 | keybin = base64.b64decode(ssh_pubkey) |
| 167 | except: |
| 168 | sys.stderr.write("Invalid key value:\n") |
| 169 | sys.stderr.write(" \"%s\":\n" % ssh_pubkey) |
| 170 | return None |
| 171 | |
| 172 | # Fingerprint |
| 173 | return hashfunction(keybin).digest() |
| 174 | |
| 175 | |
| 176 | if __name__ == "__main__": |
no test coverage detected