From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Thu, 12 Feb 2015 14:34:05 +0000 (+0530)
Subject: info hash calculation
X-Git-Url: https://git.rkrishnan.org/components/com_hotproperty/css/%22file:?a=commitdiff_plain;h=8495605e8bca5c31d26a9a2b7eec4353b74f312b;p=functorrent.git

info hash calculation
---

diff --git a/deluge.cabal b/deluge.cabal
index 5b5741b..4ae419b 100644
--- a/deluge.cabal
+++ b/deluge.cabal
@@ -25,7 +25,8 @@ executable deluge
                        time ==1.4.2,
                        bytestring,
                        doctest,
-                       HTTP >=4000.2.1
+                       HTTP >=4000.2.1,
+                       cryptohash
   hs-source-dirs:      src
   ghc-options:         -Wall -fwarn-incomplete-patterns
   default-language:    Haskell2010
\ No newline at end of file
diff --git a/src/Main.hs b/src/Main.hs
index 16b2ca5..12d3515 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -16,6 +16,9 @@ main = do
   case (Benc.decode torrentStr) of
    Right d -> case (MInfo.mkMetaInfo d) of
                Nothing -> putStrLn "parse error"
-               Just m -> putStrLn (show m)
+               Just m -> do
+                 let (Benc.Bdict d') = d
+                 putStrLn (show m)
+                 putStrLn (MInfo.infoHash d')
    Left e -> printError e
   putStrLn "done"
diff --git a/src/Metainfo.hs b/src/Metainfo.hs
index 22df5f6..bf1c1de 100644
--- a/src/Metainfo.hs
+++ b/src/Metainfo.hs
@@ -3,6 +3,8 @@ module Metainfo where
 import qualified Bencode as Benc
 import qualified Data.ByteString.Char8 as BC
 import qualified Data.Map as M
+import qualified Crypto.Hash as H
+import qualified Crypto.Hash.SHA1 as SHA1
 import Data.Time.Clock
 
 -- only single file mode supported for the time being.
@@ -63,3 +65,6 @@ mkMetaInfo (Benc.Bdict m) = let (Just info') = mkInfo (m M.! (Benc.Bstr (BC.pack
                                              , encoding = maybeBstrToString encoding' }
 mkMetaInfo _ = Nothing
 
+infoHash :: (M.Map Benc.BVal Benc.BVal) -> String
+infoHash m = let info = m M.! (Benc.Bstr (BC.pack "info"))
+             in show $ SHA1.hash $ BC.pack $ Benc.encode info