Skip to content

Instantly share code, notes, and snippets.

@pete
Created June 30, 2009 03:27
Show Gist options
  • Save pete/137970 to your computer and use it in GitHub Desktop.
Save pete/137970 to your computer and use it in GitHub Desktop.
-- Take *that*, retiman!
-- It's my general ignorance of Haskell, or the awfulness of
-- Haskell's SHA1 library, or something. This is the best way I
-- could find to turn whatever that SHA1 library returns into a
-- regular string to compare with other strings.
str2octets = concatMap ((toOctets 256) . ord)
hashString = hash . str2octets
octetsToNumber [] = 0
octetsToNumber (x:xs) =
((toInteger x) * (256 ^ (length xs))) + (octetsToNumber xs)
word160toInteger x =
let (Word160 a b c d e) = x in
(toInteger a) * (2 ^ (4 * 32)) +
(toInteger b) * (2 ^ (3 * 32)) +
(toInteger c) * (2 ^ (2 * 32)) +
(toInteger d) * (2 ^ (1 * 32)) +
(toInteger e)
word160toOctets x =
let (Word160 a b c d e) = x in
concatMap (toOctets 256) $ [a, b, c, d, e]
sha1sum :: String -> String
sha1sum s = printf "%x" $ word160toInteger $ hashString s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment