summaryrefslogtreecommitdiff
path: root/src/Network/Tox
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-10-13 15:23:17 -0400
committerjoe <joe@jerkface.net>2017-10-13 15:23:17 -0400
commitb5df42236969b38bbddcc47cb5cbfdc94a7d3393 (patch)
tree5e59d71747ea5496803242e2fd6a5d9daf7c0d54 /src/Network/Tox
parentd0666b1beffa2684552ff6e0c1c077f530784ee2 (diff)
Switched toxid format from hexadecimal to base64.
Diffstat (limited to 'src/Network/Tox')
-rw-r--r--src/Network/Tox/NodeId.hs32
1 files changed, 24 insertions, 8 deletions
diff --git a/src/Network/Tox/NodeId.hs b/src/Network/Tox/NodeId.hs
index d0c57416..bba56e27 100644
--- a/src/Network/Tox/NodeId.hs
+++ b/src/Network/Tox/NodeId.hs
@@ -40,6 +40,7 @@ import qualified Data.ByteArray as BA
40import qualified Data.ByteString as B 40import qualified Data.ByteString as B
41 ;import Data.ByteString (ByteString) 41 ;import Data.ByteString (ByteString)
42import qualified Data.ByteString.Base16 as Base16 42import qualified Data.ByteString.Base16 as Base16
43import qualified Data.ByteString.Base64 as Base64
43import qualified Data.ByteString.Char8 as C8 44import qualified Data.ByteString.Char8 as C8
44import Data.Char 45import Data.Char
45import Data.Data 46import Data.Data
@@ -122,15 +123,23 @@ instance Ord NodeId where
122zeroID :: NodeId 123zeroID :: NodeId
123zeroID = NodeId $ replicate 4 0 -- throwCryptoError $ publicKey $ B.replicate 32 0 124zeroID = NodeId $ replicate 4 0 -- throwCryptoError $ publicKey $ B.replicate 32 0
124 125
126-- | Convert to and from a Base64 variant that uses .- instead of +/.
127nmtoken64 :: Bool -> Char -> Char
128nmtoken64 False '.' = '+'
129nmtoken64 False '-' = '/'
130nmtoken64 True '+' = '.'
131nmtoken64 True '/' = '-'
132nmtoken64 _ c = c
133
125instance Read NodeId where 134instance Read NodeId where
126 readsPrec _ str 135 readsPrec _ str
127 | (bs, xs) <- Base16.decode $ C8.pack str 136 | Right bs <- fmap (BA.drop 1) $ Base64.decode $ C8.pack $ 'A':map (nmtoken64 False) (take 43 str)
128 , CryptoPassed pub <- publicKey bs -- B.length bs == 32 137 , CryptoPassed pub <- publicKey bs -- B.length bs == 32
129 = [ (key2id pub, drop 64 str) ] 138 = [ (key2id pub, drop 43 str) ]
130 | otherwise = [] 139 | otherwise = []
131 140
132instance Show NodeId where 141instance Show NodeId where
133 show nid = C8.unpack $ Base16.encode $ BA.convert $ id2key nid 142 show nid = map (nmtoken64 True) $ C8.unpack $ BA.drop 1 $ Base64.encode $ BA.cons 0 $ BA.convert $ id2key nid
134 143
135instance S.Serialize NodeId where 144instance S.Serialize NodeId where
136 get = key2id <$> getPublicKey 145 get = key2id <$> getPublicKey
@@ -241,18 +250,25 @@ instance S.Serialize NodeInfo where
241hexdigit :: Char -> Bool 250hexdigit :: Char -> Bool
242hexdigit c = ('0' <= c && c <= '9') || ( 'a' <= c && c <= 'f') || ( 'A' <= c && c <= 'F') 251hexdigit c = ('0' <= c && c <= '9') || ( 'a' <= c && c <= 'f') || ( 'A' <= c && c <= 'F')
243 252
253b64digit :: Char -> Bool
254b64digit '.' = True
255b64digit '+' = True
256b64digit '-' = True
257b64digit '/' = True
258b64digit c = ('0' <= c && c <= '9') || ( 'a' <= c && c <= 'z') || ( 'A' <= c && c <= 'Z')
259
244instance Read NodeInfo where 260instance Read NodeInfo where
245 readsPrec i = RP.readP_to_S $ do 261 readsPrec i = RP.readP_to_S $ do
246 RP.skipSpaces 262 RP.skipSpaces
247 let n = 64 -- characters in node id. 263 let n = 43 -- characters in node id.
248 parseAddr = RP.between (RP.char '(') (RP.char ')') (RP.munch (/=')')) 264 parseAddr = RP.between (RP.char '(') (RP.char ')') (RP.munch (/=')'))
249 RP.+++ RP.munch (not . isSpace) 265 RP.+++ RP.munch (not . isSpace)
250 nodeidAt = do hexhash <- sequence $ replicate n (RP.satisfy hexdigit) 266 nodeidAt = do hexhash <- sequence $ replicate n (RP.satisfy b64digit)
251 RP.char '@' RP.+++ RP.satisfy isSpace 267 RP.char '@' RP.+++ RP.satisfy isSpace
252 addrstr <- parseAddr 268 addrstr <- parseAddr
253 nid <- case Base16.decode $ C8.pack hexhash of 269 nid <- case Base64.decode $ C8.pack $ 'A' : map (nmtoken64 False) hexhash of
254 (bs,_) | B.length bs==32 -> return (bs2id bs) 270 Right bs | B.length bs - 1==32 -> return (bs2id $ BA.drop 1 bs)
255 _ -> fail "Bad node id." 271 _ -> fail "Bad node id."
256 return (nid,addrstr) 272 return (nid,addrstr)
257 (nid,addrstr) <- ( nodeidAt RP.+++ ( (zeroID,) <$> parseAddr) ) 273 (nid,addrstr) <- ( nodeidAt RP.+++ ( (zeroID,) <$> parseAddr) )
258 let raddr = do 274 let raddr = do