Skip to content

Instantly share code, notes, and snippets.

@alpeware
Created November 12, 2019 19:39
Show Gist options
  • Save alpeware/1fcd5b07e3caea596c3ee98ce798ada9 to your computer and use it in GitHub Desktop.
Save alpeware/1fcd5b07e3caea596c3ee98ce798ada9 to your computer and use it in GitHub Desktop.
Clojure Meetup Austin - Monday, November 11, 2019
(ns meetup.core
"Clojure Meetup Austin
Monday, November 11, 2019
Hack Night - Clojure Coding Challenge
Instructions here -
https://gitlab.com/maximoburrito/pubparse/tree/master"
(:import
[java.io RandomAccessFile]
[java.math BigInteger]
[java.nio.channels FileChannel]
[java.nio ByteBuffer]
[java.util Base64 Base64$Decoder]))
(set! *warn-on-reflection* true)
(def ^Base64$Decoder decoder (Base64/getDecoder))
(defn ^FileChannel file [^String path]
(-> path
(RandomAccessFile. "r")
(.getChannel)))
(defn content [^FileChannel channel]
(let [buf (-> channel
(.size)
(ByteBuffer/allocate))]
(.read channel buf)
(.flip buf)))
(defn key-bytes [^ByteBuffer buf]
(let [_ (doseq [_ (range)
:while (not= \space (char (.get buf)))])
_ (.mark buf) ;; remember position
start (.position buf)
_ (doseq [_ (range)
:while (not= \space (char (.get buf)))])
end (- (.position buf) 1) ;; w/o space
len (- end start)
arr (byte-array len)]
(.reset buf)
(.get buf arr 0 len)
arr))
(defn decode [#^bytes arr]
(.decode decoder arr))
(defn buffer [arr]
(ByteBuffer/wrap arr))
(defn length [^ByteBuffer buf]
(.getInt buf))
(defn big-int [coll]
(BigInteger. (byte-array coll)))
(defn process [^ByteBuffer buf f g]
(->> buf
(length)
(range)
(map (fn [_] (.get buf)))
(map f)
(g)))
(defn values [buf]
{:key-type (process buf char (partial apply str))
:e (process buf byte big-int)
:n (process buf byte big-int)})
(defn result [path]
(with-open [channel (file path)]
(->> channel
(content)
(key-bytes)
(decode)
(buffer)
(values))))
(defn -main [& [path]]
(let [{:keys [key-type e n]} (result path)]
(prn "The result is " key-type e n)))
#_(
;; call from a repl
(result "/home/username/.ssh/id_rsa.pub")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment