root / src / Ganeti / Hash.hs @ d99012a6
History | View | Annotate | Download (1.8 kB)
1 | 21dc8694 | Iustin Pop | {-| Crypto-related helper functions. |
---|---|---|---|
2 | 21dc8694 | Iustin Pop | |
3 | 21dc8694 | Iustin Pop | -} |
4 | 21dc8694 | Iustin Pop | |
5 | 21dc8694 | Iustin Pop | {- |
6 | 21dc8694 | Iustin Pop | |
7 | 21dc8694 | Iustin Pop | Copyright (C) 2011, 2012 Google Inc. |
8 | 21dc8694 | Iustin Pop | |
9 | 21dc8694 | Iustin Pop | This program is free software; you can redistribute it and/or modify |
10 | 21dc8694 | Iustin Pop | it under the terms of the GNU General Public License as published by |
11 | 21dc8694 | Iustin Pop | the Free Software Foundation; either version 2 of the License, or |
12 | 21dc8694 | Iustin Pop | (at your option) any later version. |
13 | 21dc8694 | Iustin Pop | |
14 | 21dc8694 | Iustin Pop | This program is distributed in the hope that it will be useful, but |
15 | 21dc8694 | Iustin Pop | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | 21dc8694 | Iustin Pop | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | 21dc8694 | Iustin Pop | General Public License for more details. |
18 | 21dc8694 | Iustin Pop | |
19 | 21dc8694 | Iustin Pop | You should have received a copy of the GNU General Public License |
20 | 21dc8694 | Iustin Pop | along with this program; if not, write to the Free Software |
21 | 21dc8694 | Iustin Pop | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
22 | 21dc8694 | Iustin Pop | 02110-1301, USA. |
23 | 21dc8694 | Iustin Pop | |
24 | 21dc8694 | Iustin Pop | -} |
25 | 21dc8694 | Iustin Pop | |
26 | 21dc8694 | Iustin Pop | module Ganeti.Hash |
27 | 21dc8694 | Iustin Pop | ( computeMac |
28 | 21dc8694 | Iustin Pop | , verifyMac |
29 | 21dc8694 | Iustin Pop | , HashKey |
30 | 21dc8694 | Iustin Pop | ) where |
31 | 21dc8694 | Iustin Pop | |
32 | 21dc8694 | Iustin Pop | import qualified Data.ByteString as B |
33 | 21dc8694 | Iustin Pop | import Data.Char |
34 | 21dc8694 | Iustin Pop | import Data.HMAC (hmac_sha1) |
35 | 21dc8694 | Iustin Pop | import qualified Data.Text as T |
36 | 21dc8694 | Iustin Pop | import Data.Text.Encoding (encodeUtf8) |
37 | 21dc8694 | Iustin Pop | import Data.Word |
38 | 21dc8694 | Iustin Pop | import Text.Printf (printf) |
39 | 21dc8694 | Iustin Pop | |
40 | 21dc8694 | Iustin Pop | -- | Type alias for the hash key. This depends on the library being |
41 | 21dc8694 | Iustin Pop | -- used. |
42 | 21dc8694 | Iustin Pop | type HashKey = [Word8] |
43 | 21dc8694 | Iustin Pop | |
44 | 21dc8694 | Iustin Pop | -- | Converts a string to a list of bytes. |
45 | 21dc8694 | Iustin Pop | stringToWord8 :: String -> HashKey |
46 | 21dc8694 | Iustin Pop | stringToWord8 = B.unpack . encodeUtf8 . T.pack |
47 | 21dc8694 | Iustin Pop | |
48 | 21dc8694 | Iustin Pop | -- | Converts a list of bytes to a string. |
49 | 21dc8694 | Iustin Pop | word8ToString :: HashKey -> String |
50 | 2cdaf225 | Iustin Pop | word8ToString = concatMap (printf "%02x") |
51 | 21dc8694 | Iustin Pop | |
52 | 21dc8694 | Iustin Pop | -- | Computes the HMAC for a given key/test and salt. |
53 | 21dc8694 | Iustin Pop | computeMac :: HashKey -> Maybe String -> String -> String |
54 | 21dc8694 | Iustin Pop | computeMac key salt text = |
55 | 21dc8694 | Iustin Pop | word8ToString . hmac_sha1 key . stringToWord8 $ maybe text (++ text) salt |
56 | 21dc8694 | Iustin Pop | |
57 | 21dc8694 | Iustin Pop | -- | Verifies the HMAC for a given message. |
58 | 21dc8694 | Iustin Pop | verifyMac :: HashKey -> Maybe String -> String -> String -> Bool |
59 | 21dc8694 | Iustin Pop | verifyMac key salt text digest = |
60 | 21dc8694 | Iustin Pop | map toLower digest == computeMac key salt text |