From ef5631f8e90ed30fedc3e1dc2d2bb5f882fee183 Mon Sep 17 00:00:00 2001 From: joe Date: Mon, 4 Aug 2014 21:03:38 -0400 Subject: KnownHosts encode/decode --- KnownHosts.hs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'KnownHosts.hs') diff --git a/KnownHosts.hs b/KnownHosts.hs index a8f4fe3..e04ad90 100644 --- a/KnownHosts.hs +++ b/KnownHosts.hs @@ -11,6 +11,7 @@ data KnownHostsEntry = KnownHostsEntry , eSp :: [L.ByteString] , eComment :: L.ByteString , eKeyBlob :: L.ByteString + , eKey :: Maybe SSH.Key , eKeyHeader :: L.ByteString , eHosts :: [L.ByteString] } @@ -20,12 +21,15 @@ serializeLine :: KnownHostsEntry -> L.ByteString serializeLine (KnownHostsEntry { eSp = sp , eComment = comment , eKeyHeader = hdr - , eKeyBlob = key + , eKeyBlob = blob + , eKey = _ , eHosts = hosts }) - = L.concat $ zipWith L.append [host,hdr,key] (take 3 sp) ++ [comment] + = L.concat $ zipWith L.append [host,hdr,blob] (take 3 sp) ++ [comment] + -- = L.concat $ zipWith L.append [host,hdr,keyblob] (take 3 sp) ++ [comment] where host = L.intercalate "," hosts + -- keyblob = L.dropWhile isSpace $ L.dropWhile (not . isSpace) $ SSH.keyblob key parseLine :: Int -> L.ByteString -> KnownHostsEntry @@ -34,7 +38,8 @@ parseLine num str = KnownHostsEntry , eSp = sp , eComment = comment , eKeyHeader = hdr - , eKeyBlob = key + , eKeyBlob = blob + , eKey = key , eHosts = hosts } where @@ -47,6 +52,23 @@ parseLine num str = KnownHostsEntry h0 <- take 1 hs h0 : hs' hdr = L.concat $ take 1 (drop 1 ns) - key = L.concat $ take 1 (drop 2 ns) + blob = L.concat $ take 1 (drop 2 ns) + key = SSH.blobkey $ L.concat $ hdr:" ":blob:[] comment = L.concat $ zipWith L.append (drop 3 ns) (drop 3 sp ++ repeat "") +data KnownHosts = KnownHosts + { khcount :: Int + , khentries :: [KnownHostsEntry] + } + +decode :: L.ByteString -> KnownHosts +decode input = renum $ foldr grok (KnownHosts 0 []) ls + where + ls = L.lines input + grok x (KnownHosts cnt es) = + KnownHosts (cnt+1) (parseLine 0 x:es) + renum kh = kh { khentries = zipWith setnum [1..] (khentries kh) } + where setnum n e = e { eLineno=n } + +encode :: KnownHosts -> L.ByteString +encode kh = L.unlines $ map serializeLine (khentries kh) -- cgit v1.2.3