Giter VIP home page Giter VIP logo

python-sshpubkeys's Introduction

OpenSSH Public Key Parser for Python

https://travis-ci.org/ojarva/python-sshpubkeys.svg?branch=master https://pypip.in/v/sshpubkeys/badge.png

Native implementation for validating OpenSSH public keys.

Currently ssh-rsa, ssh-dss (DSA), ssh-ed25519 and ecdsa keys with NIST curves are supported.

Installation:

pip install sshpubkeys

or clone the repository and use

python setup.py install

Usage:

import sys
from sshpubkeys import SSHKey

ssh = SSHKey("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAYQCxO38tKAJXIs9ivPxt7AY"
      "dfybgtAR1ow3Qkb9GPQ6wkFHQqcFDe6faKCxH6iDRteo4D8L8B"
      "xwzN42uZSB0nfmjkIxFTcEU3mFSXEbWByg78aoddMrAAjatyrh"
      "H1pON6P0= ojarva@ojar-laptop", strict_mode=True)
try:
    ssh.parse()
except InvalidKeyException as err:
    print("Invalid key:", err)
    sys.exit(1)
except NotImplementedError as err:
    print("Invalid key type:", err)
    sys.exit(1)

print(ssh.bits)  # 768
print(ssh.hash_md5())  # 56:84:1e:90:08:3b:60:c7:29:70:5f:5e:25:a6:3b:86
print(ssh.hash_sha256())  # SHA256:xk3IEJIdIoR9MmSRXTP98rjDdZocmXJje/28ohMQEwM
print(ssh.hash_sha512())  # SHA512:1C3lNBhjpDVQe39hnyy+xvlZYU3IPwzqK1rVneGavy6O3/ebjEQSFvmeWoyMTplIanmUK1hmr9nA8Skmj516HA
print(ssh.comment)  # ojar@ojar-laptop
print(ssh.options_raw)  # None (string of optional options at the beginning of public key)
print(ssh.options)  # None (options as a dictionary, parsed and validated)

Options

Set options in constructor as a keywords (i.e., SSHKey(None, strict_mode=False))

  • strict_mode: defaults to True. Disallows keys OpenSSH's ssh-keygen refuses to create. For instance, this includes DSA keys where length != 1024 bits and RSA keys shorter than 1024-bit. If set to False, tries to allow all keys OpenSSH accepts, including highly insecure 1-bit DSA keys.
  • skip_option_parsing: if set to True, options string is not parsed (ssh.options_raw is populated, but ssh.options is not).

Exceptions

  • NotImplementedError if invalid ecdsa curve or unknown key type is encountered.
  • InvalidKeyException if any other error is encountered:
    • TooShortKeyException if key is too short (<768 bits for RSA, <1024 for DSA, <256 for ED25519)
    • TooLongKeyException if key is too long (>16384 for RSA, >1024 for DSA, >256 for ED25519)
    • InvalidTypeException if key type ("ssh-rsa" in above example) does not match to what is included in base64 encoded data.
    • MalformedDataException if decoding and extracting the data fails.
    • InvalidOptionsException if options string is invalid.
      • InvalidOptionNameException if option name contains invalid characters.
        • UnknownOptionNameException if option name is not recognized.
      • MissingMandatoryOptionValueException if option needs to have parameter, but it is absent.

Tests

See "tests/" folder for unit tests. Use

python setup.py test

or

python3 setup.py test

to run test suite. If you have keys that are not parsed properly, or malformed keys that raise incorrect exception, please send your public key to [email protected], and I'll include it. Alternatively, create a new issue or make a pull request in github.

python-sshpubkeys's People

Contributors

ojarva avatar flaccid avatar oyarzun avatar blag avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.