gonito/PersistSHA1.hs

27 lines
729 B
Haskell
Raw Normal View History

2015-08-29 13:13:16 +02:00
module PersistSHA1 where
import ClassyPrelude.Yesod
import Database.Persist.Sql
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC
import Numeric (showHex)
data SHA1 = SHA1 ByteString
deriving Show
toHex :: ByteString -> ByteString
2015-09-04 06:47:49 +02:00
toHex = BC.pack . concat . (map ("\\x"++)) . (map (flip showHex "")) . B.unpack
toSHA1 :: ByteString -> SHA1
toSHA1 x = SHA1 $ B.concat ["E'\\\\x", x, "'"]
2015-08-29 13:13:16 +02:00
instance PersistField SHA1 where
toPersistValue (SHA1 t) = PersistDbSpecific t
2015-09-04 06:47:49 +02:00
fromPersistValue (PersistDbSpecific t) = Right $ SHA1 t
2015-08-29 13:13:16 +02:00
fromPersistValue _ = Left "SHA1 values must be converted from PersistDbSpecific"
instance PersistFieldSql SHA1 where
sqlType _ = SqlOther "BYTEA"