Skip to content

Commit 8827eea

Browse files
committed
add generate-keypair
1 parent 5f31815 commit 8827eea

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/clj_ssh/ssh.clj

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,3 +577,22 @@ Options are
577577
(disconnect channel))
578578
(when-not (or session-given channel-given)
579579
(disconnect session))))))
580+
581+
582+
583+
(defvar- key-types {:rsa KeyPair/RSA :dsa KeyPair/DSA})
584+
585+
(defn generate-keypair
586+
"Generate a keypair, returned as [private public] byte arrays.
587+
Valid types are :rsa and :dsa. key-size is in bytes. passphrase
588+
can be a string or byte array."
589+
([key-type key-size passphrase]
590+
(generate-keypair *ssh-agent* key-type key-size passphrase))
591+
([agent key-type key-size passphrase]
592+
(let [keypair (KeyPair/genKeyPair agent (key-type key-types) key-size)]
593+
(when passphrase (.setPassphrase keypair passphrase))
594+
(let [pub-baos (java.io.ByteArrayOutputStream.)
595+
pri-baos (java.io.ByteArrayOutputStream.)]
596+
(.writePublicKey keypair pub-baos "")
597+
(.writePrivateKey keypair pri-baos)
598+
[(.toByteArray pri-baos) (.toByteArray pub-baos)]))))

test/clj_ssh/ssh_test.clj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,3 +424,7 @@ list, Alan Dipert and MeikelBrandmeyer."
424424
(with-default-session-options {:strict-host-key-checking :no}
425425
(test-sftp-transient-with "localhost" :username (username)))))
426426

427+
(deftest generate-keypair-test
428+
(with-ssh-agent []
429+
(let [[priv pub] (generate-keypair :rsa 1024 "hello")]
430+
(add-identity *ssh-agent* "name" priv pub (.getBytes "hello")))))

0 commit comments

Comments
 (0)