tinyid (empty) → 0.1.0.0
raw patch · 4 files changed
+79/−0 lines, 4 filesdep +basedep +bytestringdep +entropy
Dependencies added: base, bytestring, entropy
Files
- CHANGELOG.md +5/−0
- LICENSE +21/−0
- src/Data/TinyID.hs +31/−0
- tinyid.cabal +22/−0
+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for tinyid++## 0.1.0.0 -- YYYY-mm-dd++* First version. Released on an unsuspecting world.
+ LICENSE view
@@ -0,0 +1,21 @@+MIT License++Copyright (c) 2022 Maxine D.++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.
+ src/Data/TinyID.hs view
@@ -0,0 +1,31 @@+module Data.TinyID + ( urlSafeAlphabet+ , nextRandomFromAlphabet+ , nextRandom+ ) where++import Data.ByteString (ByteString)+import qualified Data.ByteString as BS +import System.Entropy (getEntropy)+import Data.String (fromString)++-- | A URL-friendly alphabet.+--+-- See section 2.3 of [RFC3986](https://www.ietf.org/rfc/rfc3986.txt) for more information.+urlSafeAlphabet :: ByteString+urlSafeAlphabet = fromString $ ['a'..'z'] <> ['A'..'Z'] <> ['0'..'9'] <> "-_"++-- | Generates a (cryptographically) secure ID with a specified +-- alphabet at a given length from system entropy.+nextRandomFromAlphabet + :: ByteString -- ^ Alphabet + -> Int -- ^ Length of ID+ -> IO ByteString+nextRandomFromAlphabet alphabet n =+ BS.map (\x -> BS.index alphabet $ fromIntegral x `mod` BS.length alphabet) <$> getEntropy n++-- | Generates a (cryptographically) secure and URL-friendly ID from system entropy.+nextRandom + :: Int -- ^ Length of ID + -> IO ByteString+nextRandom = nextRandomFromAlphabet urlSafeAlphabet
+ tinyid.cabal view
@@ -0,0 +1,22 @@+cabal-version: 2.4+name: tinyid+version: 0.1.0.0+synopsis: A secure URL-friendly string ID generator+homepage: https://github.com/m4xine/tinyid+bug-reports: https://github.com/m4xine/tinyid/issues+license: MIT+license-file: LICENSE+author: m4xine+maintainer: maxined@pm.me+category: Data+extra-source-files: CHANGELOG.md++library+ exposed-modules: + Data.TinyID+ build-depends: + base >= 4.14.3 && < 4.15,+ bytestring >= 0.10.12 && < 0.11,+ entropy >= 0.4.1 && < 0.5+ hs-source-dirs: src+ default-language: Haskell2010