summaryrefslogtreecommitdiff
path: root/Base58.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Base58.hs')
-rw-r--r--Base58.hs15
1 files changed, 15 insertions, 0 deletions
diff --git a/Base58.hs b/Base58.hs
index 26f1cb2..8adf60d 100644
--- a/Base58.hs
+++ b/Base58.hs
@@ -35,3 +35,18 @@ base58_decode str = do
35 guard (hash_result==expected_hash) 35 guard (hash_result==expected_hash)
36 return (network_id,payload) 36 return (network_id,payload)
37 37
38base58_encode :: S.ByteString -> String
39base58_encode hash = replicate zcount '1' ++ map (base58chars !!) (reverse rdigits)
40 where
41 zcount = S.length . S.takeWhile (==0) $ hash
42 cksum = S.take 4 . SHA256.hash . SHA256.hash $ hash
43 n = foldl' (\a b->a*256+b) 0 . map asInteger $ concatMap S.unpack [hash, cksum]
44 asInteger x = fromIntegral x :: Integer
45 rdigits = unfoldr getdigit n
46 where
47 getdigit d = do
48 guard (d/=0)
49 let (q,b) = d `divMod` 58
50 return (fromIntegral b,q)
51
52