module Passman.Engine.KeyDerivationSpec where

import           Test.Hspec                     (Spec, describe, it)
import           Test.Hspec.Expectations.Pretty

import qualified Passman.Engine.ByteString      as B
import           Passman.Engine.KeyDerivation

k :: String -> Key
k = Key . B.fromString

pbkdf2Salt :: Salt
pbkdf2Salt = Salt $ B.pack [ 0x19, 0x2a, 0x3b, 0x4c, 0x5d, 0x6e, 0x7f, 0x80 ]

pbkdf2DerivesExpectedKeyAscii :: () -> Expectation
pbkdf2DerivesExpectedKeyAscii _ =
    actual `shouldBe` Right expected
    where
        actual = B.unpack <$> deriveKey (PBKDF2WithHmacSHA1 1000) (Parameters 1 20) (k "passw0rd") pbkdf2Salt
        expected = [ 0xac, 0x22, 0x98, 0xff, 0x9e, 0xc6, 0xd2, 0xaa
                   , 0x17, 0x16, 0x81, 0x29, 0x12, 0x48, 0xb5, 0x16
                   , 0xc7, 0x79, 0xca, 0xaa ]

pbkdf2DerivesExpectedKeyUtf8 :: () -> Expectation
pbkdf2DerivesExpectedKeyUtf8 _ =
    actual `shouldBe` Right expected
    where
        actual = B.unpack <$> deriveKey (PBKDF2WithHmacSHA1 1000) (Parameters 1 20) (k "pässw0rð") pbkdf2Salt
        expected = [ 0x3a, 0xb9, 0xe9, 0xd1, 0x97, 0xd1, 0x40, 0x50
                   , 0x25, 0xfd, 0xd3, 0x1e, 0x24, 0x7d, 0xd5, 0xb0
                   , 0xba, 0x2d, 0x7b, 0x04 ]

sha512DigestSalt :: Salt
sha512DigestSalt = Salt $ B.pack [ 0x24, 0x78, 0x5a, 0x5b, 0x75, 0x28, 0x2d, 0x54, 0x72, 0x66 ]

sha512DigestDerivesExpectedKeyAscii :: () -> Expectation
sha512DigestDerivesExpectedKeyAscii _ =
    actual `shouldBe` Right expected
    where
        actual = B.unpack <$> deriveKey SHA512Digest (Parameters 1 64) (k "password") sha512DigestSalt
        expected = [ 0x8b, 0x0e, 0x37, 0x48, 0x61, 0xb5, 0xbc, 0xe4
                   , 0x79, 0xf3, 0x8d, 0x70, 0x81, 0xf5, 0xea, 0x43
                   , 0xcc, 0xfc, 0xa4, 0x82, 0x36, 0x01, 0x59, 0xdc
                   , 0xd5, 0xfe, 0x22, 0x63, 0x49, 0xff, 0x92, 0xa2
                   , 0x62, 0xa6, 0x9e, 0xc9, 0xac, 0x8a, 0x30, 0x3f
                   , 0x0b, 0xdd, 0xf6, 0xd7, 0xf1, 0xac, 0xd6, 0x2f
                   , 0x8a, 0x16, 0x0e, 0x11, 0xd3, 0x49, 0xbf, 0x5e
                   , 0xc3, 0x8b, 0x23, 0xf8, 0x25, 0x54, 0x77, 0xc3
                   ]

sha512DigestDerivesExpectedKeyUtf8 :: () -> Expectation
sha512DigestDerivesExpectedKeyUtf8 _ =
    actual `shouldBe` Right expected
    where
        actual = B.unpack <$> deriveKey SHA512Digest (Parameters 1 64) (k "pássw0rð") sha512DigestSalt
        expected = [ 0x01, 0x7b, 0x8c, 0xc2, 0xb5, 0xcb, 0xc9, 0x34
                   , 0x09, 0xe1, 0x64, 0x6e, 0xaa, 0x3e, 0x97, 0x93
                   , 0x25, 0x45, 0xdc, 0xbe, 0x6b, 0x99, 0xf0, 0x53
                   , 0xd5, 0xf6, 0x9b, 0x31, 0xdd, 0x6f, 0xa9, 0xa3
                   , 0xa6, 0xd2, 0x5e, 0xa1, 0xcf, 0x6f, 0xf2, 0x45
                   , 0xef, 0xfc, 0x56, 0x18, 0x79, 0xaf, 0x29, 0xb3
                   , 0x00, 0xc8, 0x1f, 0xa2, 0x6e, 0x65, 0xe8, 0xa7
                   , 0xe4, 0x6e, 0xd4, 0x5b, 0x92, 0xbf, 0xc4, 0xf3
                   ]

sha512PasswordSalt :: Salt
sha512PasswordSalt = Salt $ B.pack [ 0x73, 0x61, 0x6c, 0x74, 0x73, 0x61, 0x6c, 0x74 ]

sha512PasswordDerivesExpectedKeyAscii :: () -> Expectation
sha512PasswordDerivesExpectedKeyAscii _ =
    actual `shouldBe` Right expected
    where
        actual = B.unpack <$> deriveKey SHA512Password (Parameters 1 64) (k "password") sha512PasswordSalt
        expected = [ 0x45, 0x44, 0x33, 0xaa, 0xef, 0x02, 0xa4, 0x0c
                   , 0xa7, 0x21, 0x24, 0xad, 0x7a, 0xad, 0x26, 0x40
                   , 0x67, 0x8b, 0x0e, 0x54, 0xde, 0xa8, 0x98, 0xe7
                   , 0x67, 0xa4, 0x13, 0xa6, 0xda, 0x18, 0xc8, 0x5f
                   , 0x8a, 0xca, 0x07, 0x95, 0x96, 0xf3, 0xbe, 0x2f
                   , 0x95, 0xe1, 0xcf, 0xdd, 0x01, 0x42, 0x5b, 0xf5
                   , 0x82, 0x89, 0x31, 0x86, 0xfb, 0x2c, 0x90, 0x7c
                   , 0x70, 0x3c, 0xdd, 0xf0, 0xa3, 0xa2, 0x08, 0x50
                   ]

sha512PasswordDerivesExpectedKey2Iterations :: () -> Expectation
sha512PasswordDerivesExpectedKey2Iterations _ =
    actual `shouldBe` Right expected
    where
        actual = B.unpack <$> deriveKey SHA512Password (Parameters 2 64) (k "password") sha512PasswordSalt
        expected = [ 0xbe, 0x87, 0x8f, 0xa3, 0x35, 0xfb, 0xe1, 0xe7
                   , 0x34, 0xbb, 0xec, 0x35, 0xbc, 0x12, 0x10, 0x60
                   , 0x92, 0xa3, 0x1a, 0xc5, 0x37, 0xe1, 0xd1, 0xcb
                   , 0x33, 0xed, 0x49, 0xdb, 0x1c, 0xfa, 0x2a, 0xeb
                   , 0x8d, 0x4c, 0x7b, 0x59, 0x9a, 0x59, 0xcd, 0x72
                   , 0xdc, 0x06, 0x9b, 0x36, 0x63, 0x89, 0x70, 0x1e
                   , 0x41, 0x01, 0x79, 0x98, 0x2b, 0xaa, 0x6e, 0x5a
                   , 0x19, 0x4a, 0x95, 0x7a, 0x5b, 0x40, 0x61, 0x43
                   ]

sha512PasswordDerivesExpectedKeyUtf8 :: () -> Expectation
sha512PasswordDerivesExpectedKeyUtf8 _ =
    actual `shouldBe` Right expected
    where
        actual = B.unpack <$> deriveKey SHA512Password (Parameters 1 64) (k "pássw0rð") sha512PasswordSalt
        expected = [ 0x47, 0xbc, 0x66, 0x00, 0xae, 0x7f, 0xbc, 0xb6
                   , 0x74, 0xe9, 0x99, 0x8f, 0x3a, 0x12, 0x0c, 0x02
                   , 0x21, 0xbd, 0x44, 0x53, 0xf1, 0xf5, 0x13, 0x0f
                   , 0x55, 0x42, 0x75, 0x15, 0x9d, 0xd4, 0xbe, 0xcf
                   , 0x65, 0xff, 0x7d, 0x0e, 0x69, 0xba, 0x3a, 0xdc
                   , 0x68, 0x7e, 0xc8, 0xdd, 0x30, 0xcb, 0x09, 0x74
                   , 0x10, 0x65, 0x32, 0x1b, 0xe1, 0xf5, 0x1b, 0xd8
                   , 0xba, 0xe7, 0xc5, 0xd6, 0x85, 0x33, 0xfc, 0x49
                   ]

spec :: Spec
spec = do
    describe "PBKDF2/SHA1 derivation" $ do
        it "should produce the expected key from ascii password" $
            pbkdf2DerivesExpectedKeyAscii ()
        it "should produce the expected key from non-ascii password" $
            pbkdf2DerivesExpectedKeyUtf8 ()
    describe "SHA512 digest derivation" $ do
        it "should produce the expected key from ascii password" $
            sha512DigestDerivesExpectedKeyAscii ()
        it "should produce the expected key from non-ascii password" $
            sha512DigestDerivesExpectedKeyUtf8 ()
    describe "SHA512 password derivation" $ do
        it "should produce the expected key from ascii password" $
            sha512PasswordDerivesExpectedKeyAscii ()
        it "should produce the expected key from ascii password with 2 iterations" $
            sha512PasswordDerivesExpectedKey2Iterations ()
        it "should produce the expected key from non-ascii password" $
            sha512PasswordDerivesExpectedKeyUtf8 ()
