-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMD5.hs
33 lines (31 loc) · 1.09 KB
/
MD5.hs
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
-- |
-- Module : Robotics.ROS.Msg.MD5
-- Copyright : Alexander Krupenkin 2016
-- License : BSD3
--
-- Maintainer : mail@akru.me
-- Stability : exp
-- Portability : POSIX / WIN32
--
-- ROS message MD5 recurrent hash. Replace user types by it's MD5 and
-- take hash of result.
--
module Robotics.ROS.Msg.MD5 (
MD5Digest
, computeMD5
) where
import Data.Digest.Pure.MD5 (MD5Digest, md5)
import Data.ByteString.Lazy (fromStrict)
import Data.Text.Encoding (encodeUtf8)
import Data.Text (Text, replace, pack)
-- | Compute MD5 for given message with user type hashes
--
-- Recursively generate MD5 for subtype. Have to build up
-- dependency representation for subtype in order to
-- generate MD5.
computeMD5 :: [(Text, MD5Digest)] -- ^ List of pairs: user type name, message hash
-> Text -- ^ Full message source
-> MD5Digest -- ^ Message MD5
computeMD5 [] msg = md5 (fromStrict (encodeUtf8 msg))
computeMD5 ((dep, hash) : xs) msg = computeMD5 xs msg'
where msg' = replace dep (pack $ show hash) msg