sshkey 4 is a modernization release. It requires a supported Ruby, adds Ed25519 and OpenSSH
private key support, has no runtime dependencies, and cleans up the API. Most applications that
call SSHKey.new, #private_key, #ssh_public_key and the fingerprint methods only need the changes listed under Upgrading from 3.x.
- Ruby 3.3 or newer, or JRuby 10 or newer.
- The
base64gem is no longer a dependency. sshkey has no runtime dependencies.
- Ed25519 keys:
SSHKey.generate(type: "ed25519"), loading existing keys, public keys, fingerprints, bit length, SSHFP records and randomart. (#41) - Loading unencrypted OpenSSH format private keys (
-----BEGIN OPENSSH PRIVATE KEY-----), the default output ofssh-keygen, for RSA, DSA, ECDSA and Ed25519. The comment stored in the key is used unlesscomment:is given. (#41) #openssh_private_keyreturns the private key in OpenSSH format.SSHKey.newaccepts anOpenSSL::PKeyobject as well as a PEM or OpenSSH format string.SSHKey.generateacceptsdirectives:, and a Symboltype:(e.g.type: :ed25519).- ECDSA keys are fully supported on JRuby.
- An error hierarchy:
SSHKey::Error, withSSHKey::PrivateKeyError,SSHKey::PublicKeyErrorandSSHKey::UnsupportedError.
SSHKey.generaterequires atype:(it previously defaulted to RSA). Calling it without one raisesArgumentError. Choose"ed25519"to matchssh-keygen, or"rsa"for the previous behavior. This is required rather than defaulting to Ed25519, because switching key types silently can break code that uses the generated key: for example, net-ssh needs theed25519gem to load Ed25519 keys, and Ed25519 private keys can't be passphrase-encrypted.SSHKey.generateandSSHKey.newtake keyword arguments. Unknown options raiseArgumentErrorinstead of being ignored.- RSA keys are generated with 3072 bits by default (was 2048), matching
ssh-keygen. #encrypted_private_keyreturns PKCS#8 PEM (-----BEGIN ENCRYPTED PRIVATE KEY-----) encrypted with AES-256-CBC and a PBKDF2-derived key, instead of traditional PEM encrypted with AES-128-CBC and a single MD5 iteration. OpenSSH andSSHKey.newread both formats.#private_keyreturns Ed25519 keys in OpenSSH format, since OpenSSH does not read Ed25519 keys in PEM format. RSA, DSA and ECDSA keys are still returned in PEM format.#randomartdefaults to SHA256 (was MD5) and matchesssh-keygen -lvoutput exactly, including the[SHA256]footer. It accepts the digest name in any case.- Errors:
- A private key that can't be read raises
SSHKey::PrivateKeyError(was anOpenSSL::PKey::PKeyErrorsubclass). - Unsupported keys and operations raise
SSHKey::UnsupportedError. - Invalid
generateoptions and an unknownrandomartdigest raiseArgumentError(wasRuntimeError). SSHKey::PublicKeyErrornow inherits fromSSHKey::Error.
- A private key that can't be read raises
- Generating DSA keys. OpenSSH removed DSA support in version 10. Existing DSA keys can still be loaded, validated and fingerprinted.
- The
fingerprintalias, on bothSSHKeyand instances. Usemd5_fingerprintfor the old behavior, orsha256_fingerprintto matchssh-keygen. - The
rsa_private_key,dsa_private_key,rsa_public_keyanddsa_public_keyaliases. Useprivate_keyandpublic_key. SSHKey.format_sshfp_record(useSSHKey.sshfpor#sshfp) and theSSH_CONVERSIONconstant. The internal constantsSSHFP_TYPES,ECDSA_CURVES,VALID_BITSandSSH2_LINE_LENGTHare now private.SSH_TYPESremains public.- The
identifierandqmethods added toOpenSSL::PKey::EC, and the globaljruby_not_implementedmethod.
- Loading an encrypted PEM key without a passphrase no longer prompts for one on the terminal.
It raises
SSHKey::PrivateKeyError. - The class-level fingerprint and
sshfpmethods no longer treat a public key whose comment contains "PRIVATE" as a private key. - The bit length of DSA public keys is measured from
p, so it no longer occasionally reports 1016 for a 1024-bit key. Ed25519 public keys always report 256. SSHKey.generate(type: "RSA")with an uppercase type uses the right default size.ssh_public_key_bitsraisesSSHKey::PublicKeyErrorfor truncated keys instead ofTypeError.
- Pass a
type:toSSHKey.generate. Usetype: "rsa"for the previous default, ortype: "ed25519"for the key typessh-keygennow creates by default (if you connect with net-ssh, also install theed25519gem). - Pass options as keywords. Literal hashes already work (
SSHKey.generate(type: "rsa")); a hash held in a variable needs a double splat:SSHKey.generate(**options). - Replace
fingerprintwithmd5_fingerprint(same result) orsha256_fingerprint. - Replace
rsa_private_key/dsa_private_keywithprivate_key, andrsa_public_key/dsa_public_keywithpublic_key. - Replace
rescue OpenSSL::PKey::PKeyError(orRSAError,DSAError,ECError) aroundSSHKey.newwithrescue SSHKey::PrivateKeyError, andrescue RuntimeErroraroundSSHKey.generatewithrescue ArgumentError. - If you generate DSA keys, switch to Ed25519 or ECDSA.
- If you compare
randomartoutput, pass"MD5"to keep the previous digest. - Pass
bits: 2048if you depend on the previous RSA default.
See the GitHub history for changes before 4.0.0.