diff options
Diffstat (limited to 'Base58.hs')
-rw-r--r-- | Base58.hs | 25 |
1 files changed, 21 insertions, 4 deletions
@@ -1,6 +1,12 @@ | |||
1 | {-# LANGUAGE CPP #-} | ||
1 | module Base58 where | 2 | module Base58 where |
2 | 3 | ||
4 | #if !defined(VERSION_cryptonite) | ||
3 | import qualified Crypto.Hash.SHA256 as SHA256 | 5 | import qualified Crypto.Hash.SHA256 as SHA256 |
6 | #else | ||
7 | import Crypto.Hash | ||
8 | import Data.ByteArray (convert) | ||
9 | #endif | ||
4 | import qualified Data.ByteString as S | 10 | import qualified Data.ByteString as S |
5 | import Data.Maybe | 11 | import Data.Maybe |
6 | import Data.List | 12 | import Data.List |
@@ -28,7 +34,12 @@ base58_decode str = do | |||
28 | 34 | ||
29 | let (rcksum,rpayload) = splitAt 4 $ rbytes | 35 | let (rcksum,rpayload) = splitAt 4 $ rbytes |
30 | a_payload = reverse rpayload | 36 | a_payload = reverse rpayload |
37 | #if !defined(VERSION_cryptonite) | ||
31 | hash_result = S.take 4 . SHA256.hash . SHA256.hash . S.pack $ a_payload | 38 | hash_result = S.take 4 . SHA256.hash . SHA256.hash . S.pack $ a_payload |
39 | #else | ||
40 | hash_result = S.take 4 . convert $ digest | ||
41 | where digest = hash (S.pack a_payload) :: Digest SHA256 | ||
42 | #endif | ||
32 | expected_hash = S.pack $ reverse rcksum | 43 | expected_hash = S.pack $ reverse rcksum |
33 | (network_id,payload) = splitAt 1 a_payload | 44 | (network_id,payload) = splitAt 1 a_payload |
34 | 45 | ||
@@ -37,11 +48,17 @@ base58_decode str = do | |||
37 | return (network_id,payload) | 48 | return (network_id,payload) |
38 | 49 | ||
39 | base58_encode :: S.ByteString -> String | 50 | base58_encode :: S.ByteString -> String |
40 | base58_encode hash = replicate zcount '1' ++ map (base58chars !!) (reverse rdigits) | 51 | base58_encode hsh = replicate zcount '1' ++ map (base58chars !!) (reverse rdigits) |
41 | where | 52 | where |
42 | zcount = S.length . S.takeWhile (==0) $ hash | 53 | zcount = S.length . S.takeWhile (==0) $ hsh |
43 | cksum = S.take 4 . SHA256.hash . SHA256.hash $ hash | 54 | #if !defined(VERSION_cryptonite) |
44 | n = foldl' (\a b->a*256+b) 0 . map asInteger $ concatMap S.unpack [hash, cksum] | 55 | cksum = S.take 4 . SHA256.hash . SHA256.hash $ hsh |
56 | #else | ||
57 | cksum = S.take 4 (convert digest2 :: S.ByteString) | ||
58 | where digest2 = hash ( convert digest1 :: S.ByteString) :: Digest SHA256 | ||
59 | digest1 = hash hsh :: Digest SHA256 | ||
60 | #endif | ||
61 | n = foldl' (\a b->a*256+b) 0 . map asInteger $ concatMap S.unpack [hsh, cksum] | ||
45 | asInteger x = fromIntegral x :: Integer | 62 | asInteger x = fromIntegral x :: Integer |
46 | rdigits = unfoldr getdigit n | 63 | rdigits = unfoldr getdigit n |
47 | where | 64 | where |