File: BCryptPBKDF.hs

package info (click to toggle)
haskell-crypton 1.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,548 kB
  • sloc: haskell: 26,764; ansic: 22,294; makefile: 6
file content (212 lines) | stat: -rw-r--r-- 5,108 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
{-# LANGUAGE OverloadedStrings #-}

module BCryptPBKDF (tests) where

import qualified Data.ByteString as B

import Test.Tasty
import Test.Tasty.HUnit

import Crypto.KDF.BCryptPBKDF (
    Parameters (..),
    generate,
    hashInternal,
 )

tests :: TestTree
tests =
    testGroup
        "BCryptPBKDF"
        [ testGroup
            "generate"
            [ testCase "1" generate1
            , testCase "2" generate2
            , testCase "3" generate3
            ]
        , testGroup
            "hashInternal"
            [ testCase "1" hashInternal1
            ]
        ]
  where
    -- test vector taken from the go implementation by @dchest
    generate1 = expected @=? generate params pass salt
      where
        params = Parameters 12 32
        pass = "password" :: B.ByteString
        salt = "salt" :: B.ByteString
        expected =
            B.pack
                [ 0x1a
                , 0xe4
                , 0x2c
                , 0x05
                , 0xd4
                , 0x87
                , 0xbc
                , 0x02
                , 0xf6
                , 0x49
                , 0x21
                , 0xa4
                , 0xeb
                , 0xe4
                , 0xea
                , 0x93
                , 0xbc
                , 0xac
                , 0xfe
                , 0x13
                , 0x5f
                , 0xda
                , 0x99
                , 0x97
                , 0x4c
                , 0x06
                , 0xb7
                , 0xb0
                , 0x1f
                , 0xae
                , 0x14
                , 0x9a
                ]
                :: B.ByteString

    -- test vector generated with the go implemenation by @dchest
    generate2 = expected @=? generate params pass salt
      where
        params = Parameters 7 71
        pass = "DieWuerdeDesMenschenIstUnantastbar" :: B.ByteString
        salt = "Tafelsalz" :: B.ByteString
        expected =
            B.pack
                [ 0x17
                , 0xb4
                , 0x76
                , 0xaa
                , 0xd7
                , 0x42
                , 0x33
                , 0x49
                , 0x5c
                , 0xe8
                , 0x79
                , 0x49
                , 0x15
                , 0x74
                , 0x4c
                , 0x71
                , 0xf9
                , 0x99
                , 0x66
                , 0x89
                , 0x7a
                , 0x60
                , 0xc3
                , 0x70
                , 0xb4
                , 0x3c
                , 0xa8
                , 0x83
                , 0x80
                , 0x5a
                , 0x56
                , 0xde
                , 0x38
                , 0xbc
                , 0x51
                , 0x8c
                , 0xd4
                , 0xeb
                , 0xd1
                , 0xcf
                , 0x46
                , 0x0a
                , 0x68
                , 0x3d
                , 0xc8
                , 0x12
                , 0xcf
                , 0xf8
                , 0x43
                , 0xce
                , 0x21
                , 0x9d
                , 0x98
                , 0x81
                , 0x20
                , 0x26
                , 0x6e
                , 0x42
                , 0x0f
                , 0xaa
                , 0x75
                , 0x5d
                , 0x09
                , 0x8d
                , 0x45
                , 0xda
                , 0xd5
                , 0x15
                , 0x6e
                , 0x65
                , 0x1d
                ]
                :: B.ByteString

    -- test vector generated with the go implemenation by @dchest
    generate3 = expected @=? generate params pass salt
      where
        params = Parameters 5 5
        pass = "ABC" :: B.ByteString
        salt = "DEF" :: B.ByteString
        expected =
            B.pack
                [ 0xdd
                , 0x6e
                , 0xa0
                , 0x69
                , 0x29
                ]
                :: B.ByteString

    hashInternal1 = expected @=? hashInternal passHash saltHash
      where
        passHash = B.pack [0 .. 63] :: B.ByteString
        saltHash = B.pack [64 .. 127] :: B.ByteString
        expected =
            B.pack
                [ 0x87
                , 0x90
                , 0x48
                , 0x70
                , 0xee
                , 0xf9
                , 0xde
                , 0xdd
                , 0xf8
                , 0xe7
                , 0x61
                , 0x1a
                , 0x14
                , 0x01
                , 0x06
                , 0xe6
                , 0xaa
                , 0xf1
                , 0xa3
                , 0x63
                , 0xd9
                , 0xa2
                , 0xc5
                , 0x04
                , 0xdb
                , 0x35
                , 0x64
                , 0x43
                , 0x72
                , 0x1e
                , 0xb5
                , 0x55
                ]
                :: B.ByteString