2012-11-03 20 views
7

我正在學習haskell,並決定嘗試編寫一些小測試程序以使用Haskell代碼和使用模塊。目前我正嘗試使用第一個參數來使用Cypto.PasswordStore創建密碼哈希。爲了測試我的程序,我試圖從第一個參數創建一個散列,然後將散列打印到屏幕上。如何將一個Data.ByteString.Internal.ByteString放入?

import Crypto.PasswordStore 
import System.Environment 

main = do 
    args <- getArgs 
    putStrLn (makePassword (head args) 12) 

,我發現了以下錯誤:

testmakePassword.hs:8:19: 
    Couldn't match expected type `String' 
      with actual type `IO Data.ByteString.Internal.ByteString' 
    In the return type of a call of `makePassword' 
    In the first argument of `putStrLn', namely 
     `(makePassword (head args) 12)' 
    In a stmt of a 'do' block: putStrLn (makePassword (head args) 12) 

我使用以下鏈接作爲參考了,但我現在只是試示數無濟於事。 http://hackage.haskell.org/packages/archive/bytestring/0.9.0.4/doc/html/Data-ByteString-Internal.html http://hackage.haskell.org/packages/archive/pwstore-purehaskell/2.1/doc/html/Crypto-PasswordStore.html

回答

4

您還沒有進口的字節串,所以它試圖用putStrLn的字符串版本。 我提供toBSString->ByteString轉換。

嘗試

import Crypto.PasswordStore 
import System.Environment 
import qualified Data.ByteString.Char8 as B 

toBS = B.pack 

main = do 
    args <- getArgs 
    makePassword (toBS (head args)) 12 >>= B.putStrLn 
+0

謝謝!這對我有用。代碼會警告已棄用的函數,但答案就是我所需要的。 _Warning:使用'B.putStrLn' (從Data.ByteString導入): 棄用:「使用Data.ByteString.Char8.putStrLn而不是_ – NerdGGuy

+1

@NerdGGuy讓我們擺脫'Data.ByteString', 。Data.ByteString.Char8'代替,然後(見編輯) – AndrewC

+0

大使用Data.ByteString.Char8對我來說很有意義 – NerdGGuy

4

你必須做兩件事情是不同的。首先,makePassword在IO中,因此您需要將結果綁定到名稱,然後將名稱傳遞給IO函數。其次,你需要從Data.ByteString

import Crypto.PasswordStore 
import System.Environment 
import qualified Data.ByteString as B 

main = do 
    args <- getArgs 
    pwd <- makePassword (B.pack $ head args) 12 
    B.putStrLn pwd 

導入IO功能或者,如果你不會使用密碼結果其他地方,你可以使用綁定到兩個功能直接連接:

main = do 
    args <- getArgs 
    B.putStrLn =<< makePassword (B.pack $ head args) 12 
+0

你的意思進口資質Data.ByteString爲B 還多了一個錯誤:? 未能進行匹配預期類型'B.ByteString「 與實際類型'字符串」 預期類型:[B.ByteString] 實際類型:[字符串] 在頭的'的第一個參數」,即'ARGS' 在第一個參數'makePassw ord',即'(head args)' – NerdGGuy

+1

是的,固定的。教我發帖子前的咖啡 –