nist-beacon 0.1.0.0 → 0.1.0.1
raw patch · 4 files changed
+37/−28 lines, 4 filesdep +http-conduitdep −HTTP
Dependencies added: http-conduit
Dependencies removed: HTTP
Files
- README.md +2/−0
- nist-beacon.cabal +3/−2
- nist-beacon.cabal~ +2/−2
- src/Net/Beacon.hs +30/−24
README.md view
@@ -1,2 +1,4 @@ # haskell-nist-beacon Simple interface to the NIST Randomness Beacon.++Now available on hackage! See https://hackage.haskell.org/package/nist-beacon-0.1.0.0
nist-beacon.cabal view
@@ -10,10 +10,11 @@ -- PVP summary: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change-version: 0.1.0.0+version: 0.1.0.1 -- A short (one-line) description of the package. synopsis: Haskell interface to the nist random beacon. +homepage: https://github.com/bstamour/haskell-nist-beacon -- A longer description of the package. -- description: @@ -57,7 +58,7 @@ -- other-extensions: -- Other library packages from which modules are imported.- build-depends: base >=4.8 && <4.9, xml >=1.3 && <1.4, HTTP >=4000.2 && <4000.3, bytestring >=0.10 && <0.11+ build-depends: base >=4.8 && <4.9, xml >=1.3 && <1.4, http-conduit >=2.0, bytestring >=0.10 && <0.11 -- Directories containing source files. hs-source-dirs: src
nist-beacon.cabal~ view
@@ -10,7 +10,7 @@ -- PVP summary: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change-version: 0.1.0.0+version: 0.1.0.1 -- A short (one-line) description of the package. synopsis: Haskell interface to the nist random beacon. @@ -57,7 +57,7 @@ -- other-extensions: -- Other library packages from which modules are imported.- build-depends: base >=4.8 && <4.9, xml >=1.3 && <1.4, HTTP >=4000.2 && <4000.3, bytestring >=0.10 && <0.11+ build-depends: base >=4.8 && <4.9, xml >=1.3 && <1.4, http-conduit >=2.0, bytestring >=0.10 && <0.11 -- Directories containing source files. hs-source-dirs: src
src/Net/Beacon.hs view
@@ -43,10 +43,9 @@ import Text.XML.Light.Proc import Text.XML.Light.Types -import Network.HTTP (getResponseBody, simpleHTTP, getRequest)--import qualified Data.ByteString.Lazy.Char8 as B-+import qualified Data.ByteString.Lazy as B+import Network.HTTP.Conduit (simpleHttp)+import Numeric -- | A single record: the random data plus some additional information. data Record =@@ -94,56 +93,63 @@ -- | Last record published. getLastRecord :: IO (Maybe Record) getLastRecord = do- x <- getXmlData "http://beacon.nist.gov/rest/record/last"+ x <- simpleHttp "https://beacon.nist.gov/rest/record/last" return $ getRecord x -- | Current record, or closest to the timestamp. getCurrentRecord :: Timestamp -> IO (Maybe Record) getCurrentRecord ts = do- x <- getXmlData $ "http://beacon.nist.gov/rest/record/" ++ (show ts)+ x <- simpleHttp $ "http://beacon.nist.gov/rest/record/" ++ (show ts) return $ getRecord x -- | Previous record. getPreviousRecord :: Timestamp -> IO (Maybe Record) getPreviousRecord ts = do- x <- getXmlData $ "http://beacon.nist.gov/rest/record/previous/" ++ (show ts)+ x <- simpleHttp $ "https://beacon.nist.gov/rest/record/previous/" ++ (show ts) return $ getRecord x -- | Next record. getNextRecord :: Timestamp -> IO (Maybe Record) getNextRecord ts = do- x <- getXmlData $ "http://beacon.nist.gov/rest/record/next/" ++ (show ts)+ x <- simpleHttp $ "https://beacon.nist.gov/rest/record/next/" ++ (show ts) return $ getRecord x -- | Start chain record. getStartChainRecord :: Timestamp -> IO (Maybe Record) getStartChainRecord ts = do- x <- getXmlData $ "http://beacon.nist.gov/rest/record/start-chain/" ++ (show ts)+ x <- simpleHttp $ "https://beacon.nist.gov/rest/record/start-chain/" ++ (show ts) return $ getRecord x -getXmlData :: String -> IO B.ByteString-getXmlData url = do- body <- getResponseBody <=< simpleHTTP $ getRequest url- return $ B.pack body-- getRecord :: B.ByteString -> Maybe Record getRecord stuff = do xml <- parseXMLDoc stuff let fc = findChild' xml Record- <$> fc "version"- <*> (read <$> fc "frequency")- <*> (read <$> fc "timeStamp")- <*> (B.pack <$> fc "seedValue")- <*> (B.pack <$> fc "previousOutputValue")- <*> (B.pack <$> fc "signatureValue")- <*> (B.pack <$> fc "outputValue")- <*> (read <$> fc "statusCode")+ <$> fc "version"+ <*> (read <$> fc "frequency")+ <*> (read <$> fc "timeStamp")+ <*> (hexToBS <$> fc "seedValue")+ <*> (hexToBS <$> fc "previousOutputValue")+ <*> (hexToBS <$> fc "signatureValue")+ <*> (hexToBS <$> fc "outputValue")+ <*> (read <$> fc "statusCode") where- findChild' xml name = strContent <$> findChild (QName name Nothing Nothing) xml+ findChild' xml name = strContent <$> filterChildName ((name ==) . qName) xml++-- input: even-length string of hex characters+-- output: bytestring packed with the hex bits+-- e.g. B.unpack $ hexToBS "1011" = [16,17]+hexToBS :: String -> B.ByteString +hexToBS = B.pack . go+ where go (a:b:xs) = + let parses = readHex [a,b]+ in case parses of+ [(val,"")] -> val:(go xs)+ _ -> error "parse error in hexToBS"+ go [] = []+ go _ = error "odd length input to hexToBS"