{-# LANGUAGE LambdaCase, ScopedTypeVariables #-} {- Example of using PBKDF2 to seed the TF (ThreeFish) random number generator with a string. Copyright 2015 Ken Takusagawa This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . -} module Main(main) where { import qualified System.Random.TF.Instances as TF; import Data.ByteString (ByteString); import qualified Data.Binary as Binary; import Crypto.PBKDF.ByteString(sha512PBKDF2); import Data.Text.Encoding(encodeUtf8); import qualified Data.Text as Text; import qualified Data.ByteString.Lazy as Lazy; import System.Random.TF.Gen(TFGen, seedTFGen); import Data.Word; main :: IO (); main = do { let { gen :: TFGen ; gen = seedTFGen $ Binary.decode $ Lazy.fromStrict $ sha512PBKDF2 (string2bs mypassphrase) (string2bs salt) (fromIntegral iterations_pbkdf2) (fromIntegral keylength_bytes) }; mapM_ print (TF.randoms gen :: [Word8]); }; -- world's most famous password mypassphrase :: String; mypassphrase = "ACollectionOfDiplomaticHistorySince_1966_ToThe_PresentDay#"; salt::String; salt = "mysalt98754321"; string2bs :: String -> ByteString; string2bs = encodeUtf8 . Text.pack; keylength_bytes :: Integer; keylength_bytes = 4 * div 64 8; -- 4-tuple of Word64 {- On one hand, quoting the documentation: "it has not been designed with cryptographic applications in mind." On the other hand, I see no reason (other than side channel attacks) why the stream is not cryptographically secure, so use a crytographically secure number of rounds in the KDF as well.-} iterations_pbkdf2 :: Integer; iterations_pbkdf2 = 100000; } -- end