module System.Executable.Hash.Internal where
import Crypto.Hash.SHA1 (hash)
import qualified Data.ByteString as BS
import Data.FileEmbed (dummySpaceWith, injectWith)
import System.Directory (doesFileExist)
injectedExecutableHash :: Maybe BS.ByteString
injectedExecutableHash
| BS.all (== toEnum (fromEnum '0')) bs = Nothing
| otherwise = Just bs
where
bs = $(dummySpaceWith "executable-hash" 20)
injectExecutableHash :: FilePath -> IO ()
injectExecutableHash fp = do
binary <- BS.readFile fp
let sha1 = hash binary
case injectWith "executable-hash" sha1 binary of
Nothing -> fail "Impossible: dummy space too small for executable-hash."
Just binary' -> do
BS.writeFile fp binary'
putStrLn $ "Successfully wrote " ++ fp ++ " with injected hash."
maybeInjectExecutableHash :: FilePath -> IO ()
maybeInjectExecutableHash fp = do
exists <- doesFileExist fp
if exists
then injectExecutableHash fp
else putStrLn $ concat
[ "Not injecting executable hash into "
, fp
, ", as it doesn't exist."
]