packages feed

crypto-keys-ssh (empty) → 0.1.0.0

raw patch · 5 files changed

+98/−0 lines, 5 filesdep +basedep +base64-bytestringdep +binarysetup-changed

Dependencies added: base, base64-bytestring, binary, bytestring

Files

+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for crypto-keys-ssh++## 0.1.0.0 -- 2019-07-15++* First version. Released on an unsuspecting world.
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2019 Brian McKenna++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ crypto-keys-ssh.cabal view
@@ -0,0 +1,27 @@+cabal-version:       >=1.10+name:                crypto-keys-ssh+version:             0.1.0.0+license:             MIT+license-file:        LICENSE+author:              Brian McKenna+maintainer:          brian@brianmckenna.org+category:            Cryptography+build-type:          Simple+extra-source-files:  CHANGELOG.md+synopsis:            Like crypto-pubkey-openssh but not dependent on any specific crypto library+description:+  Just like <https://github.com/knsd/crypto-pubkey-openssh crypto-pubkey-openssh>+  but not dependent on crypto-pubkey-types nor any specific crypto library.++source-repository head+  type: git+  location: https://github.com/puffnfresh/crypto-keys-ssh.git++library+  exposed-modules:     Crypto.PubKey.SSH+  build-depends:       base >=4.10 && <4.14+                     , binary >=0.8 && <0.11+                     , bytestring >=0.10 && <0.11+                     , base64-bytestring >=1.0 && <1.1+  hs-source-dirs:      src+  default-language:    Haskell98
+ src/Crypto/PubKey/SSH.hs view
@@ -0,0 +1,44 @@+module Crypto.PubKey.SSH (+  SshPubKeyError (..)+, getSshRsa+, decodeSshPubKey+) where++import           Control.Monad               (void)+import           Data.Bifunctor              (first)+import           Data.Binary.Get             (Get, getByteString, getWord32be,+                                              runGet)+import qualified Data.ByteString             as BS+import           Data.ByteString.Base64.Lazy (decode)+import qualified Data.ByteString.Lazy.Char8  as LBS+import           Data.Maybe                  (listToMaybe)++data SshPubKeyError+  = SshNotEnoughWords+  | SshInvalidBase64 String+  deriving (Show, Eq, Ord)++integerSize :: Integer -> Int+integerSize =+  (`div` 8) . length . takeWhile (/= 0) . iterate (`div` 2)++getInteger :: Get Integer+getInteger = do+  size <- getWord32be+  bs <- getByteString (fromIntegral size)+  pure (sum (zipWith (\c b -> fromIntegral c * 0x100 ^ b) (BS.unpack bs) [size - 1, size - 2..0]))++getSshRsa :: Get (Integer, Integer)+getSshRsa = do+  size <- getWord32be+  void (getByteString (fromIntegral size))+  e <- getInteger+  n <- getInteger+  pure (e, n)++decodeSshPubKey :: (Int -> Integer -> Integer -> a) -> LBS.ByteString -> Either SshPubKeyError a+decodeSshPubKey f bs = do+  bs' <- maybe (Left SshNotEnoughWords) Right (listToMaybe (drop 1 (LBS.words bs)))+  sshKey <- first SshInvalidBase64 (decode bs')+  let (e, n) = runGet getSshRsa sshKey+  pure (f (integerSize n) n e)