mirror of
https://code.sup39.dev/repos/Wqawg
synced 2025-03-20 04:46:22 +09:00
Generate and keep permanent salt for generating hashids for URIs
This commit is contained in:
parent
9536d870e5
commit
c2bf470fb6
10 changed files with 190 additions and 40 deletions
src/Data
39
src/Data/Int/Local.hs
Normal file
39
src/Data/Int/Local.hs
Normal file
|
@ -0,0 +1,39 @@
|
|||
{- This file is part of Vervis.
|
||||
-
|
||||
- Written in 2019 by fr33domlover <fr33domlover@riseup.net>.
|
||||
-
|
||||
- ♡ Copying is an act of love. Please copy, reuse and share.
|
||||
-
|
||||
- The author(s) have dedicated all copyright and related and neighboring
|
||||
- rights to this software to the public domain worldwide. This software is
|
||||
- distributed without any warranty.
|
||||
-
|
||||
- You should have received a copy of the CC0 Public Domain Dedication along
|
||||
- with this software. If not, see
|
||||
- <http://creativecommons.org/publicdomain/zero/1.0/>.
|
||||
-}
|
||||
|
||||
module Data.Int.Local
|
||||
( toInts
|
||||
, fromInts
|
||||
)
|
||||
where
|
||||
|
||||
import Prelude
|
||||
|
||||
import Data.Int (Int64)
|
||||
import Data.List.NonEmpty (NonEmpty (..), (<|))
|
||||
|
||||
modbase :: Int64
|
||||
modbase = 2 ^ 29
|
||||
|
||||
toInts :: Int64 -> NonEmpty Int
|
||||
toInts n =
|
||||
let (d, m) = n `divMod` modbase
|
||||
m' = fromIntegral m
|
||||
in if d == 0
|
||||
then m' :| []
|
||||
else m' <| toInts d
|
||||
|
||||
fromInts :: NonEmpty Int -> Int64
|
||||
fromInts = foldr (\ i n -> fromIntegral i + modbase * n) 0
|
46
src/Data/KeyFile.hs
Normal file
46
src/Data/KeyFile.hs
Normal file
|
@ -0,0 +1,46 @@
|
|||
{- This file is part of Vervis.
|
||||
-
|
||||
- Written in 2019 by fr33domlover <fr33domlover@riseup.net>.
|
||||
-
|
||||
- ♡ Copying is an act of love. Please copy, reuse and share.
|
||||
-
|
||||
- The author(s) have dedicated all copyright and related and neighboring
|
||||
- rights to this software to the public domain worldwide. This software is
|
||||
- distributed without any warranty.
|
||||
-
|
||||
- You should have received a copy of the CC0 Public Domain Dedication along
|
||||
- with this software. If not, see
|
||||
- <http://creativecommons.org/publicdomain/zero/1.0/>.
|
||||
-}
|
||||
|
||||
module Data.KeyFile
|
||||
( KeyFile (..)
|
||||
, loadKeyFile
|
||||
)
|
||||
where
|
||||
|
||||
import Prelude
|
||||
|
||||
import Data.ByteString (ByteString)
|
||||
import System.Directory (doesFileExist)
|
||||
|
||||
import qualified Data.ByteString as B (readFile, writeFile)
|
||||
|
||||
class KeyFile a where
|
||||
generateKey :: IO a
|
||||
parseKey :: ByteString -> IO a
|
||||
renderKey :: a -> ByteString
|
||||
|
||||
loadKeyFile :: KeyFile a => Bool -> FilePath -> IO a
|
||||
loadKeyFile setup path = do
|
||||
e <- doesFileExist path
|
||||
if e
|
||||
then if setup
|
||||
then fail $ "loadKeyFile: Initial setup but file already exists: " ++ path
|
||||
else parseKey =<< B.readFile path
|
||||
else if setup
|
||||
then do
|
||||
k <- generateKey
|
||||
B.writeFile path $ renderKey k
|
||||
return k
|
||||
else fail $ "loadKeyFile: File not found: " ++ path
|
Loading…
Add table
Add a link
Reference in a new issue