make LTS branch pre-releases
[cabal.git] / Cabal-syntax / src / Distribution / Utils / Base62.hs
blob757528e750f0bbdba7feebd8e4e6e0c48bf78822
1 -- | Implementation of base-62 encoding, which we use when computing hashes
2 -- for fully instantiated unit ids.
3 module Distribution.Utils.Base62 (hashToBase62) where
5 import Data.Char (chr)
6 import GHC.Fingerprint (Fingerprint (..), fingerprintString)
7 import Numeric (showIntAtBase)
9 -- | Hash a string using GHC's fingerprinting algorithm (a 128-bit
10 -- MD5 hash) and then encode the resulting hash in base 62.
11 hashToBase62 :: String -> String
12 hashToBase62 s = showFingerprint $ fingerprintString s
13 where
14 showIntAtBase62 x = showIntAtBase 62 representBase62 x ""
15 representBase62 x
16 | x < 10 = chr (48 + x)
17 | x < 36 = chr (65 + x - 10)
18 | x < 62 = chr (97 + x - 36)
19 | otherwise = '@'
20 showFingerprint (Fingerprint a b) = showIntAtBase62 a ++ showIntAtBase62 b